如何绑定到这个自定义依赖属性?
我的自定义 UserControl 中有一个 DependencyProperty,如下所示:
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); }
}
在 Expression Blend 3 中,我有这样的:
<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>
现在,我想做的是使 ColumnWidth 依赖属性控制 EmployeeHeaderTemplate DataTemplate 和 ListBox 中 TextBlock 的宽度在 CallListTemplate 数据模板中。我知道我可以在 C# 中做到这一点,但我有一种感觉,使用纯 XAML 数据绑定也可以做到这一点。
但是,由于对 Silverlight 和 Expression Blend 3 相对较新,我不确定如何解决这个问题。有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在 CallBoard 实例上输入一个名称,然后在 Binding 中使用 ElementName 引用该名称。
因此,页面的根目录将如下所示:
并且您的绑定将如下所示:
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:
and your Binding would look like:
Width="{Binding ColumnWidth}"
不起作用吗?Does
Width="{Binding ColumnWidth}"
not work?