附加属性和绑定

发布于 2024-08-20 17:27:33 字数 1220 浏览 6 评论 0原文

我正在创建一个附加行为,以便设置类的常规属性:

public class LookupHelper
{
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.RegisterAttached("ItemsSource", typeof(object), typeof(LookupHelper), new UIPropertyMetadata(null, OnItemsSourceChanged));

    private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as MyControl;
        if(control == null)
                return;

        control.ItemsSource = (IEnumerable)e.NewValue;
    }

    public static object GetItemsSource(GridColumn column)
    {
        return column.GetValue(ItemsSourceProperty);
    }

    public static void SetItemsSource(GridColumn column, object value)
    {
        column.SetValue(ItemsSourceProperty, value);
    }
}   

这里,MyControl 上的 ItemsSource 属性是常规属性,因此我无法在 Xaml 中绑定它,因此出现此附加行为。

现在,当我使用字符串或对象使用此附加属性时,它可以工作,并且会命中我设置的断点,但是当我使用绑定标记设置它时,它永远不会运行。为什么这不起作用?

<MyControl ctrl:LookupHelper.ItemsSource="DataSource"/>; //It works
<MyControl ctrl:LookupHelper.ItemsSource="{Binding Path=MyDataSource}"/>; //Does not work

我需要做的是将 ItemsSource 属性设置为 Binding 指定的值。

I'm creating an attached behavior in order to set a regular property of a class:

public class LookupHelper
{
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.RegisterAttached("ItemsSource", typeof(object), typeof(LookupHelper), new UIPropertyMetadata(null, OnItemsSourceChanged));

    private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as MyControl;
        if(control == null)
                return;

        control.ItemsSource = (IEnumerable)e.NewValue;
    }

    public static object GetItemsSource(GridColumn column)
    {
        return column.GetValue(ItemsSourceProperty);
    }

    public static void SetItemsSource(GridColumn column, object value)
    {
        column.SetValue(ItemsSourceProperty, value);
    }
}   

Here, ItemsSource property on MyControl is a regular property, so I can not bind it in Xaml, hence this attached behavior.

Now, when I use this attached property using string or objects it works and breakpoint I set is hit, but when I set it with Binding markup, it never runs. Why isn't this working?

<MyControl ctrl:LookupHelper.ItemsSource="DataSource"/>; //It works
<MyControl ctrl:LookupHelper.ItemsSource="{Binding Path=MyDataSource}"/>; //Does not work

What I need to do is to set the ItemsSource property to the value specified by the Binding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

空名 2024-08-27 17:27:33

在 Get 和 Set 方法中,您将接收对象定义为 GridColumn,而它应该是 DependencyObject。

您可能还想将 DP 的类型从 object 更改为 IEnumerable ,因为您在更改处理程序中转换为该类型。

In your Get and Set methods, you're defining the receiving object as GridColumn where it should be DependencyObject.

You might also want to change the type of your DP from object to IEnumerable since your casting to that in your change handler.

但可醉心 2024-08-27 17:27:33

您可以发布您正在使用的标记吗?另外,如果实际属性存在于对象上并且在那里有意义,那么我认为您应该在该对象上使用常规依赖属性,而不是辅助类上的附加属性。

编辑
来自 MSDN:
GetPropertyName 访问器的签名必须是:

public static object GetPropertyName(object target)

SetPropertyName 访问器的签名必须是:

public static void SetPropertyName(object target, object value)

在您的情况下,GridColumn 是正确的目标类型吗?

Can you please post the markup you are using? Also, If the actual property exists on an object and makes sense there then I think you should be using a regular dependency property on that object instead of an attached property on a helper class.

Edit
From MSDN:
The signature for the GetPropertyName accessor must be:

public static object GetPropertyName(object target)

and the signature for the SetPropertyName accessor must be:

public static void SetPropertyName(object target, object value)

In your case, is GridColumn the correct target type?

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