在 Silverlight 中使用多重绑定来定位自定义属性
我正在尝试使用 WPF MultiBinding 来定位我在 UserControl 上定义的自定义属性。我没有任何运气。我见过的所有示例都针对内置 .Net 属性(Text、ItemSource、IsEnabled)。我想知道您是否可以定位自定义属性。请帮忙。
///
public partial class ActivityStatusDisplay : UserControl
{
public ActivityStatusDisplay()
{
InitializeComponent();
}
/// <summary>
/// Gets or sets the value for the read/write property named
/// Status.
///
/// </summary>
public TaskStatuses Status
{
get
{
return (TaskStatuses)GetValue(StatusProperty);
}
set
{
SetValue(StatusProperty, value);
UpdateForStatus(value);
}
}
public static readonly DependencyProperty StatusProperty = DependencyProperty.
Register("Status", typeof(TaskStatuses), typeof(ActivityStatusDisplay), null);
用于绑定的 XAML 看起来像以下之一:
<my:DataGrid ItemsSource="{Binding PatientTaskCollection}">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Status" Width="Auto">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<my1:ActivityStatusDisplay Margin="3">
<mb:BindingUtil.MultiBindings>
<mb:MultiBindings>
<mb:MultiBinding TargetProperty="Status" Converter="{StaticResource StatusConverter}">
<mb:MultiBinding.Bindings>
<mb:BindingCollection>
<Binding Path="RecoveryTaskStatusID"/>
<Binding Path="Due"/>
</mb:BindingCollection>
</mb:MultiBinding.Bindings>
</mb:MultiBinding>
</mb:MultiBindings>
</mb:BindingUtil.MultiBindings>
</my1:ActivityStatusDisplay>
或类似:
<my:DataGrid ItemsSource="{Binding PatientTaskCollection}">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Status" Width="Auto">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<my1:ActivityStatusDisplay Margin="3">
<my1:ActivityStatusDisplay.Status>
<mb:MultiBinding Converter="{StaticResource StatusConverter}">
<Binding Path="RecoveryTaskStatusID"/>
<Binding Path="Due"/>
</mb:MultiBinding>
</my1:ActivityStatusDisplay.Status>
I'm trying to use WPF MultiBinding to target a custom property I defined on my UserControl. I'm not having any luck. All of the examples that I've seen target built-in .Net properties (Text, ItemSource, IsEnabled). I'm wondering if you can target a custom property. Please help.
///
public partial class ActivityStatusDisplay : UserControl
{
public ActivityStatusDisplay()
{
InitializeComponent();
}
/// <summary>
/// Gets or sets the value for the read/write property named
/// Status.
///
/// </summary>
public TaskStatuses Status
{
get
{
return (TaskStatuses)GetValue(StatusProperty);
}
set
{
SetValue(StatusProperty, value);
UpdateForStatus(value);
}
}
public static readonly DependencyProperty StatusProperty = DependencyProperty.
Register("Status", typeof(TaskStatuses), typeof(ActivityStatusDisplay), null);
with the XAML for binding looking like one of these:
<my:DataGrid ItemsSource="{Binding PatientTaskCollection}">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Status" Width="Auto">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<my1:ActivityStatusDisplay Margin="3">
<mb:BindingUtil.MultiBindings>
<mb:MultiBindings>
<mb:MultiBinding TargetProperty="Status" Converter="{StaticResource StatusConverter}">
<mb:MultiBinding.Bindings>
<mb:BindingCollection>
<Binding Path="RecoveryTaskStatusID"/>
<Binding Path="Due"/>
</mb:BindingCollection>
</mb:MultiBinding.Bindings>
</mb:MultiBinding>
</mb:MultiBindings>
</mb:BindingUtil.MultiBindings>
</my1:ActivityStatusDisplay>
or something like:
<my:DataGrid ItemsSource="{Binding PatientTaskCollection}">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Status" Width="Auto">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<my1:ActivityStatusDisplay Margin="3">
<my1:ActivityStatusDisplay.Status>
<mb:MultiBinding Converter="{StaticResource StatusConverter}">
<Binding Path="RecoveryTaskStatusID"/>
<Binding Path="Due"/>
</mb:MultiBinding>
</my1:ActivityStatusDisplay.Status>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是,您假设当绑定更改其值时,将调用
Status
属性的“setter”方法。这是不会发生的。绑定直接调用SetValue
方法。因此,当绑定结果导致值被修改时,不会发生对UpdateForStatus
的调用。下面是您的依赖属性的外观(我假设
TaskStatuses
是一个枚举类型)使用
Register
方法的PropertyMetaData
参数我们可以指定此属性应具有的默认值以及每当依赖项属性的值更改时要调用的静态回调方法。您希望通过该回调方法调用UpdateForStatus
方法。You problem is that you are assuming the "setter" method for the
Status
property gets called when the binding changes its value. This is doesn't happen. Binding calls theSetValue
method directly. Hence your call toUpdateForStatus
doesn't occur when value is modified as a result of binding.Here is how your dependency property should look, (I'll assume
TaskStatuses
is an enum type)Using the
PropertyMetaData
parameter of theRegister
method we can specify the default value this property should have and a static callback method to call whenever the value of the dependency property is changed. Its from this callback method that you would want to call yourUpdateForStatus
method.