I have a customized UserControl (CustomControl1) which I used as data container in MainPage.xaml.
As content of the MainPage- CustomControl1 I placed some buttons.
My error/question:
The buttons would be displayed, but I get an error when I try to set a button property.
Button object would be null…. But why?
In addition, no events would be raise from the nested objects, when I click on it in the development environment. Only if I select the nested object in XAML, I can use the [Object]->Properties->Events.
Silverlight Sources:
Vers.4
Vers.6 with TemplateBinding and Error
What must I change?
I think, it could be an error of my ContentPresenter binding in CustomControl1.
I found many pages with this kind of problem... But I couldn't fix my error...
My 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>
My 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";
}
My 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>