问题
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 this:
<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'd like to do is make the ColumnWidth dependency property control the width of the TextBlock in the EmployeeHeaderTemplate DataTemplate and the ListBox in the CallListTemplate DataTemplate. I know I can do this in C#, but I have a feeling it'd also be possible with pure XAML data-binding.
But, being relatively new to Silverlight and Expression Blend 3, I'm not sure how to go about this. Any suggestions?
回答1:
Try put a name on your CallBoard instance and then refer to that using ElementName in your Binding.
So the root of your page would look like:
<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 would look like:
Width="{Binding ElementName=callBoard, Path=ColumnWidth}"
回答2:
Does Width="{Binding ColumnWidth}"
not work?
来源:https://stackoverflow.com/questions/1384624/how-do-i-bind-to-this-custom-dependency-property