简体   繁体   English

我可以通过绑定到wpf父控件上的属性的依赖项属性在xaml中设置用户控件datacontext吗?

[英]Can I set my user control datacontext in xaml via a dependency property bound to a property on the parent control in wpf?

I have a user control with a dependency property called ViewModel. 我有一个用户控件,其依赖项名为ViewModel。 I want the value that this property is set to to be the data context for this control. 我希望将此属性设置为该控件的数据上下文的值。

Here is an example of its usage; 这是一个用法示例。

<uc:MyCustomControl ViewModel="{Binding CustomControlViewModel}"/>

Here is the code-behind for the ViewModel dependency property for "MyCustomControl" 这是“ MyCustomControl”的ViewModel依赖项属性的代码隐藏

    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
        "ViewModel", typeof(CustomControlViewModel), typeof(MyCustomControl), new PropertyMetadata(default(CustomControlViewModel), PropertyChangedCallback));

    public CustomControlViewModel ViewModel
    {
        get { return (CustomControlViewModel)GetValue(ViewModelProperty); }
        set
        {
            SetValue(ViewModelProperty, value);
        }
    }

    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        var control = (MyCustomControl) dependencyObject;
        if (control == null)
        {
            return;
        }
        control.DataContext = (CustomControlViewModel) dependencyPropertyChangedEventArgs.NewValue;
    }

This is the constructor for the "MyCustomControl"; 这是“ MyCustomControl”的构造函数;

    public MyCustomControl()
    {
        InitializeComponent();
    }

This doesn't seem to work, what might I be doing wrong? 这似乎不起作用,我可能做错了什么?

DataContext itself is a DependencyProperty . DataContext本身是一个DependencyProperty You can directly assign it with your viewmodel like this. 您可以像这样直接用您的viewmodel分配它。

<uc:MyCustomControl DataContext="{Binding CustomControlViewModel}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定到用户控件 WPF/XAML 的依赖项属性 - Binding to a dependency property of a user control WPF/XAML 具有依赖项属性WPF的用户控件DataContext /绑定问题 - User Control DataContext/Binding Issue with Dependency Property WPF WPF用户控件依赖项属性绑定到ViewModel属性时不起作用 - WPF User Control Dependency Property not working when bound to a ViewModel property 如何重用WPF用户控件但更改绑定属性? - How can I reuse a WPF user control but change a bound property? 通过XAML在自己的标记中设置的WPF自定义控件属性 - WPF Custom control property set via XAML in own tag WPF:通过依赖属性的用户控件访问样式 - WPF: User Control access Style via Dependency Property 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? 具有依赖项属性的WPF用户控件不起作用 - WPF User Control with Dependency Property not working 当绑定数据更改时,用户控件上的依赖项属性不会更新属性 - Dependency Property on a user control not updating the property when bound data changes 每次依赖属性设置为 true 时,都不会调用从另一个用户控件绑定到依赖属性的视图模型属性 - View model property bound to a dependency property from another user control is never called every time the dependency property is set to true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM