使用 DependencyProperty 访问 UserControl 代码中的绑定对象

发布于 2024-10-28 13:03:03 字数 1370 浏览 2 评论 0原文

我在通过父 UserControl 上的数据绑定使用 DependencyProperty 设置自定义用户控件的属性时遇到问题。

下面是我的自定义 UserControl 的代码:

public partial class UserEntityControl : UserControl
{
    public static readonly DependencyProperty EntityProperty =  DependencyProperty.Register("Entity",
        typeof(Entity), typeof(UserEntityControl));

    public Entity Entity
    {
        get
        {
            return (Entity)GetValue(EntityProperty);
        }
        set
        {
            SetValue(EntityProperty, value);
        }
    }

    public UserEntityControl()
    {
        InitializeComponent();
        PopulateWithEntities(this.Entity);
    }
}

我想要访问后面代码中的 Entity 属性,因为这将根据实体中存储的值动态构建用户控件。我遇到的问题是 Entity 属性从未设置。

以下是我在父用户控件中设置绑定的方法:

<ListBox Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding SearchResults}"     x:Name="SearchResults_List">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!--<views:SearchResult></views:SearchResult>-->
            <eb:UserEntityControl  Entity="{Binding}" ></eb:UserEntityControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我将 ListBox 的 ItemsSource 设置为 SearchResults,它是实体的可观察集合(与自定义 UserControl 上的实体类型相同)。

我在调试输出窗口中没有收到任何运行时绑定错误。我只是无法设置 Entity 属性的值。有什么想法吗?

I am having trouble setting a property of a custom user control using a DependencyProperty through databinding on the parent UserControl.

Here is the code for my custom UserControl:

public partial class UserEntityControl : UserControl
{
    public static readonly DependencyProperty EntityProperty =  DependencyProperty.Register("Entity",
        typeof(Entity), typeof(UserEntityControl));

    public Entity Entity
    {
        get
        {
            return (Entity)GetValue(EntityProperty);
        }
        set
        {
            SetValue(EntityProperty, value);
        }
    }

    public UserEntityControl()
    {
        InitializeComponent();
        PopulateWithEntities(this.Entity);
    }
}

I want access to the Entity property in the code behind because that will dynamically build the user control based on values stored in the Entity. The problem that I am having is that the Entity property is never set.

Here is how I am setting up the binding in the parent user control:

<ListBox Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding SearchResults}"     x:Name="SearchResults_List">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!--<views:SearchResult></views:SearchResult>-->
            <eb:UserEntityControl  Entity="{Binding}" ></eb:UserEntityControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I am setting the ItemsSource of the ListBox to SearchResults, which is an Observable Collection of Entities (The same type as Entity on the custom UserControl).

I am not getting any runtime binding errors in the debug output window. I just cannot set the value of the Entity property. Any ideas?

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

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

发布评论

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

评论(1

雄赳赳气昂昂 2024-11-04 13:03:03

您正尝试在 c-tor 中使用 Entity 属性,但这还为时过早。 c-tor 将在给出属性值之前被解雇。

您需要做的是将 propertyChanged Event HAndler 添加到 DependencyProperty,如下所示:

    public static readonly DependencyProperty EntityProperty = DependencyProperty.Register("Entity",
typeof(Entity), typeof(UserEntityControl), new PropertyMetadata(null, EntityPropertyChanged));

    static void EntityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var myCustomControl = d as UserEntityControl;

        var entity = myCustomControl.Entity; // etc...
    }

    public Entity Entity
    {
        get
        {
            return (Entity)GetValue(EntityProperty);
        }
        set
        {
            SetValue(EntityProperty, value);
        }
    }

You are trying to use the Entity property in the c-tor, which is too soon. the c-tor is going to be fired BEFORE the property value is going to be given.

What u need to do is to add a propertyChanged Event HAndler to the DependencyProperty, like so:

    public static readonly DependencyProperty EntityProperty = DependencyProperty.Register("Entity",
typeof(Entity), typeof(UserEntityControl), new PropertyMetadata(null, EntityPropertyChanged));

    static void EntityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var myCustomControl = d as UserEntityControl;

        var entity = myCustomControl.Entity; // etc...
    }

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