0
我有以下CustomControl,我想作爲一個橫幅基本使用方法:在WinRT的XAML找不到元素
XAML:
<UserControl x:Name="userControl"
x:Class="Nova.WinRT.Controls.BannerPanel"
...>
<Grid Background="#BF8B8B8B">
<Grid Height="400" Background="#FF050A7C" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<TextBlock x:Name="_Title" Text="Title" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<ContentPresenter x:Name="_Content" Grid.Row="1"/>
<StackPanel Orientation="Horizontal" Grid.Row="2" HorizontalAlignment="Center">
<Button x:Name="_OK" Content="OK" HorizontalAlignment="Center" Click="OkPressed" FontSize="18" Width="100" Margin="20,0" />
<Button x:Name="_Cancel" Content="Cancel" HorizontalAlignment="Center" Click="CancelPressed" FontSize="18" Width="100" Margin="20,0" />
</StackPanel>
</Grid>
</Grid>
</UserControl>
C#:
public sealed partial class BannerPanel : UserControl
{
/// <summary>
/// Title of the Banner
/// </summary>
public string Title
{
get { return _Title.Text; }
set { _Title.Text = value; }
}
/// <summary>
/// The visibility of the OK button
/// </summary>
public Visibility OKVisibility
{
get { return _OK.Visibility; }
set { _OK.Visibility = value; }
}
/// <summary>
/// The visibility of the Cancel button
/// </summary>
public Visibility CancelVisibility
{
get { return _Cancel.Visibility; }
set { _Cancel.Visibility = value; }
}
/// <summary>
/// The inner content of the panel
/// </summary>
public FrameworkElement InnerContent
{
get { return (FrameworkElement)_Content.Content; }
set { _Content.Content = value; }
}
/// <summary>
/// Fires when the Ok button is clicked
/// </summary>
public event RoutedEventHandler OkClick;
/// <summary>
/// Fires when the Cancel button is clicked
/// </summary>
public event RoutedEventHandler CancelClick;
};
但是,當我用它(請參閱下面的XAML),自動生成的代碼沒有找到在內部內容的元素:
XA ML:
<Controls:BannerPanel x:Name="Banner" Title="Terms and Policy" CancelVisibility="Collapsed" OkClick="OnTermsAccepted">
<Controls:BannerPanel.InnerContent>
<ScrollViewer Width="500">
<TextBlock x:Name="TermsText" TextWrapping="Wrap" FontSize="12" />
</ScrollViewer>
</Controls:BannerPanel.InnerContent>
</Controls:BannerPanel>
C#:
public sealed partial class TermsBanner : UserControl
{
/// <summary>
/// Constructor
/// </summary>
public TermsBanner()
{
this.InitializeComponent();
// Why do I have to find TermsText Manually like this??????
TermsText = (Banner.InnerContent as ScrollViewer).Content as TextBlock;
TermsText.Text = TermsOfUse;
}
};
爲什麼我的變量TermsText手動指向正確的事?爲什麼不能像FindName()那樣自動找到它?
究竟如何我上面的XAML翻譯成風格/ Setter/Property格式? – 2013-03-02 23:58:01
更新了答案 – 2013-03-03 05:37:30
好吧,這一切似乎工作得很好。謝謝!最後一個問題,這到底是什麼呢? [TemplatePart(Name =「_OK」,Type = typeof(Button))] – 2013-03-03 18:31:27