在 Silverlight 中使用多重绑定来定位自定义属性

发布于 2024-09-08 06:38:18 字数 3482 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2024-09-15 06:38:18

您的问题是,您假设当绑定更改其值时,将调用 Status 属性的“setter”方法。这是不会发生的。绑定直接调用 SetValue 方法。因此,当绑定结果导致值被修改时,不会发生对 UpdateForStatus 的调用。

下面是您的依赖属性的外观(我假设 TaskStatuses 是一个枚举类型)

public TaskStatuses Status
{
    get { return (TaskStatuses)GetValue(StatusProperty); }
    set { SetValue(StatusProperty, value); }
}


public static readonly DependencyProperty StatusProperty =
  DependencyProperty.Register(
    "Status", 
    typeof(TaskStatuses), 
    typeof(ActivityStatusDisplay),
    new PropertyMetaData(TaskStatuses.Default, OnStatusPropertyChanged));

private static void OnStatusPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
   ActivityStatusDisplay source = (ActivityStatusDisplay)d;
   TaskStatuses value = (TaskStatuses)e.NewValue;
   source.UpdateForStatus(value);
}

使用 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 the SetValue method directly. Hence your call to UpdateForStatus 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)

public TaskStatuses Status
{
    get { return (TaskStatuses)GetValue(StatusProperty); }
    set { SetValue(StatusProperty, value); }
}


public static readonly DependencyProperty StatusProperty =
  DependencyProperty.Register(
    "Status", 
    typeof(TaskStatuses), 
    typeof(ActivityStatusDisplay),
    new PropertyMetaData(TaskStatuses.Default, OnStatusPropertyChanged));

private static void OnStatusPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
   ActivityStatusDisplay source = (ActivityStatusDisplay)d;
   TaskStatuses value = (TaskStatuses)e.NewValue;
   source.UpdateForStatus(value);
}

Using the PropertyMetaData parameter of the Register 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 your UpdateForStatus method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文