我有一个自定义的 UserControl (CustomControl1),用作 MainPage.xaml 中的数据容器。
作为 MainPage-CustomControl1 的内容,我放置了一些按钮。
我的错误/问题:
将显示按钮,但是当我尝试设置按钮属性时出现错误。
按钮对象将为空……。但为什么?
此外,当我在开发环境中单击嵌套对象时,不会从嵌套对象中引发任何事件。只有当我在 XAML 中选择嵌套对象时,我才能使用 [Object]->Properties->Events。
Silverlight 源:带有 TemplateBinding 和错误的
Vers.4 Vers.6
我必须改变什么?
我认为,这可能是我在 CustomControl1 中的 ContentPresenter 绑定错误。
我发现很多页面都存在这种问题......但我无法修复我的错误......
我的 CustomControl1.xaml:
<UserControl.Resources>
<Style x:Key="roundBorderStyle" TargetType="Border">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF5F0E8E" Offset="0"/>
<GradientStop Color="#FFA6CEF0" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="BorderBrush" Value="#FF0F2048"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
</Style>
</UserControl.Resources>
<Border Width="Auto" Style="{StaticResource roundBorderStyle}">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<sdk:Label x:Name="FrameCaption" Content="Caption" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Row="0" />
<Button x:Name="OpenButton" Content="Click" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,4,10,0" />
</Grid>
<ContentPresenter Grid.Row="1" x:Name="FrameContent"
Content="{Binding NewContent, ElementName=userControl}"
Margin="4" Width="Auto" Height="Auto"/>
</Grid>
</Border>
我的 CustomControl1.xaml.cs:
public UIElement NewContent
{
get { return (UIElement)GetValue(NewContentProperty); }
set { SetValue(NewContentProperty, value); }
}
public static readonly DependencyProperty NewContentProperty =
DependencyProperty.Register("NewContent", typeof(UIElement), typeof(CustomControl1), null);
public string Button2Content
{
set
{
// On run time myButton2 and Button2 are null
// Because LayoutRoot_Loaded isn't finished.
((Button)LayoutRoot.FindName("Button2")).Content = value;
}
get
{
return myButton2.Content.ToString();
}
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
//Next step would throw an error, because Button1 is null ... Why?
//Button1.Content = "NewContent";
//Maybe a problem with ContentPresenter binding in CustomControl1?
// This works ... But a little complicated...
myButton1 = (Button)LayoutRoot.FindName("Button1");
myButton2 = (Button)LayoutRoot.FindName("Button2");
myButton3 = (Button)LayoutRoot.FindName("Button3");
//myButton1.Content = "NewContent";
}
我的 MainPage.xaml:
<my:CustomControl1 x:Name="LayoutRoot" Button2Content="New2Content" Loaded="LayoutRoot_Loaded">
<StackPanel>
<Button Content="Button 1" x:Name="Button1"/>
<Button Content="Button 2" x:Name="Button2"/>
<Button Content="Button 3" x:Name="Button3"/>
</StackPanel>
</my:CustomControl1>