How do I bind this custom dependency property?
I have a DependencyProperty in my custom UserControl that looks like this:
public static readonly DependencyProperty ColumnWidthProperty =
DependencyProperty.Register("ColumnWidth", typeof(int), typeof(CallBoard),
new PropertyMetadata(150));
public int ColumnWidth {
get { return (int)GetValue(ColumnWidthProperty); }
set { SetValue(ColumnWidthProperty, value); }
}
In Expression Blend 3 I have the following:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SilverlightTest.CallBoard"
d:DesignWidth="640" d:DesignHeight="480">
<UserControl.Resources>
<DataTemplate x:Key="EmployeeHeaderTemplate">
<TextBlock Text="{Binding Name}" TextAlignment="Center" FontWeight="Bold" FontSize="16"/>
</DataTemplate>
<DataTemplate x:Key="CallListItemTemplate">
<StackPanel >
<TextBlock Text="{Binding CustomerName}" FontWeight="Bold"/>
<TextBlock Text="{Binding Details}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="CallListTemplate">
<ListBox ItemTemplate="{StaticResource CallListItemTemplate}" ItemsSource="{Binding Calls}"/>
</DataTemplate>
</UserControl.Resources>
<StackPanel x:Name="stackPanel" DataContext="{Binding Source={StaticResource DummyDataSource}}">
<ItemsControl ItemsPanel="{StaticResource HorizontalItemsPanelTemplate}" ItemTemplate="{StaticResource EmployeeHeaderTemplate}" ItemsSource="{Binding}"/>
<ItemsControl ItemsPanel="{StaticResource HorizontalItemsPanelTemplate}" ItemTemplate="{StaticResource CallListTemplate}" ItemsSource="{Binding}"/>
</StackPanel>
</UserControl>
Now what I would like to do is make the dependency property ColumnWidth control the width of the TextBlock in the DataTemplate EmployeeHeaderTemplate and the ListBox in the CallListTemplate of the DataTemplate. I know I can do this in C #, but I have a feeling it would be possible with clean XAML data binding.
But being relatively new to Silverlight and Expression Blend 3, I'm not sure how. Any suggestions?
Try to put the name in your CallBoard instance and then refer to it using ElementName in your Binding.
So your page root will look like this:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightTest.CallBoard"
x:Name="callBoard"
...
>
...
and your binding will look like this:
Width="{Binding ElementName=callBoard, Path=ColumnWidth}"
Doesn't it work Width="{Binding ColumnWidth}"
?