设置与 WPF 用户控件内的自定义 DependencyProperty 的绑定

发布于 2024-08-21 12:57:33 字数 685 浏览 2 评论 0原文

我有一个 WPF UserControl,其中包含一个名为 MyDP 的自定义 DependencyProperty。我想将其绑定到我的 ViewModel 上的属性(作为 UserControl 的 DataContext 注入)。我知道一种方法是通过在父窗口的 XAML 中的 UserControl 声明中设置绑定来实现此目的:

<Window x:Class="MyNamespace.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:views="clr-namespace:MyNamespace.Views">
    <StackPanel>
        <views:MyControl MyDP="{Binding Path=MyVMProperty, Mode=OneWayToSource}"/>
    </StackPanel>
</Window>

这工作正常,但作为替代方案,我可以在 UserControl 的 XAML 中设置绑定,类似于我设置绑定的方式UserControl 中的各个控件到 ViewModel 的其他属性?

I have a WPF UserControl containing a custom DependencyProperty named MyDP. I want to bind this to a property on my ViewModel (which is injected as the UserControl's DataContext). I know one way to do it by setting the binding in the UserControl's declaration in the parent window's XAML as such:

<Window x:Class="MyNamespace.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:views="clr-namespace:MyNamespace.Views">
    <StackPanel>
        <views:MyControl MyDP="{Binding Path=MyVMProperty, Mode=OneWayToSource}"/>
    </StackPanel>
</Window>

This works fine, but as an alternate could I set up the binding inside the UserControl's XAML, similar to how I set the bindings for the individual controls inside the UserControl to other properties of the ViewModel?

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

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

发布评论

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

评论(2

极度宠爱 2024-08-28 12:57:33

你无法直接按照你最初的想法去做。您可能尝试过并遇到一些编译错误。您无法在 UserControl 的根 XAML 中内联设置自定义属性,因为元素类型是 UserControl,因此编译器会根据该类型(而不是您的自定义类型)强制执行属性名称。您可以通过更改为附加属性来解决这个问题,但这实际上改变了 MyDP 的含义。相反,您可以在 UserControl 的样式中设置默认值,并获得一个额外的好处,即只需执行原始示例中的操作即可在任何声明的实例上覆盖它。在 UserControl 的根元素下设置它:

<UserControl.Style>
    <Style>
        <Setter Property="views:MyControl.MyDp" Value="{Binding Path=MyVMProperty, Mode=OneWayToSource}" />
    </Style>
</UserControl.Style>

You can't do what you were originally thinking directly. You probably tried and got some compile errors. You can't set a custom property inline in the UserControl's root XAML because the element type is UserControl so the compiler is enforcing property names based on that type, not your custom type. You could get around this by changing to an Attached Property but that actually changes the meaning of MyDP. Instead you can set a default in the Style for the UserControl and get an additional benefit of being able to override it on any declared instance by just doing what's in your original example. Set this under your UserControl's root element:

<UserControl.Style>
    <Style>
        <Setter Property="views:MyControl.MyDp" Value="{Binding Path=MyVMProperty, Mode=OneWayToSource}" />
    </Style>
</UserControl.Style>
蓝天 2024-08-28 12:57:33

您还可以在 MainWindow 的构造函数中定义绑定,如下所示:

public MainWindow()
{
    InitializeComponent();
    SetBinding(MyDPProperty, "MyVMProperty");
}

You could also define the binding in the constructor of MainWindow, like this:

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