绑定到 UserControl DependencyProperty [英] Binding to UserControl DependencyProperty

查看:36
本文介绍了绑定到 UserControl DependencyProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有一些 DependencyProperties 的 UserControl(在此示例中只有一个字符串属性).当我实例化 UserControl 时,我可以设置 UserControl 的属性并按预期显示.当我尝试通过绑定替换静态文本时,没有显示任何内容.

I have created a UserControl with some DependencyProperties (in the example here only one string property). When I instantiate the Usercontrol, I can set the property of the UserControl and it is shown as expected. When I am trying to replace the static text by Binding, nothing is displayed.

我的用户控件如下所示:

My UserControl looks as follows:

<User Control x:Class="TestUserControBinding.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="100">
    <Grid>
    <Label Content="{Binding MyText}"/>
  </Grid>
</UserControl>

背后的代码是:

namespace TestUserControBinding {

  public partial class MyUserControl : UserControl {
    public MyUserControl() {
      InitializeComponent();
      this.DataContext = this;
    }

    public static readonly DependencyProperty MyTextProperty = 
                   DependencyProperty.Register(
                         "MyText", 
                          typeof(string), 
                          typeof(MyUserControl));

    public string MyText {
      get {
        return (string)GetValue(MyTextProperty);
      }
      set {
        SetValue(MyTextProperty, value);
      }
    }// MyText

  }
}

当我在 MainWindow 中尝试此操作时,一切都按预期进行:

When I try this in my MainWindow, everything is as expected:

<Window x:Class="TestUserControBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestUserControBinding"
        Title="MainWindow" Height="350" Width="525">
  <StackPanel>
    <local:MyUserControl MyText="Hello World!"/>
  </StackPanel>
</Window>

但这不起作用:

<Window x:Class="TestUserControBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestUserControBinding"
        Title="MainWindow" Height="350" Width="525">
  <StackPanel>
    <local:MyUserControl MyText="{Binding Path=Text}"/>
    <Label Content="{Binding Path=Text}"/>
  </StackPanel>
</Window>

标签的行为是正确的,所以属性Text"没有问题

The behaviour of the label is correct, so there is no Problem with the Property "Text"

我的错误是什么?我思考了几个小时,但找不到任何我忘记的东西.

What is my mistake? I Ponder for hours, but can't find anything I have forgotten.

推荐答案

在您的 UserControl 中使用以下绑定:

With the following binding in your UserControl:

<Label Content="{Binding MyText}"/>

我不确定将文本直接设置为 MyText 属性是如何工作的.您必须在 UserControl 的某处设置 DataContext 才能使其工作.

I'm not sure how setting the text directly to the MyText property works. You must be setting the DataContext on the UserControl somewhere for this to work.

无论如何,这个绑定是问题 - 据我了解你的情况,你不想绑定到 UserControlDataContext 因为那不一定有一个 MyText 属性.您想绑定到 UserControl 本身,特别是您创建的 DependencyProperty.为此,您需要使用 RelativeSource 绑定,如下所示:

Regardless, this binding is the issue - as I understand your scenario, you don't want to bind to the DataContext of the UserControl because that will not necessarily have a MyText property. You want to bind to the UserControl itself, and specifically the DependencyProperty you created. To do that, you need to use a RelativeSource binding, like the following:

<Label Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MyUserControl}}, Path=MyText}"/>

这将向上导航可视化树到 MyUserControl,然后在那里找到 MyText 属性.它不会依赖于 DataContext,它会根据您放置 UserControl 的位置而改变.

This will navigate up the visual tree to MyUserControl and then find the MyText property there. It will not be dependent on the DataContext, which will change based on where you place the UserControl.

在这种情况下,local 指的是您需要在 UserControl 中定义的命名空间:

In this case, local refers to a namespace you'll need to define in the UserControl:

<UserControl x:Class="TestUserControBinding.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:TestUserControBinding"
         ...>

此时您的第二个示例应该可以工作.

And your second example should work at that point.

这篇关于绑定到 UserControl DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆