绑定到用户控件的DependencyProperty [英] Binding to UserControl DependencyProperty

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

问题描述

我已经建立了一些DependencyProperties(在这里的例子只有一个字符串属性),一个用户控件。当我实例化用户控件,我可以设置用户控件的属性,并将其显示为预期。当我试图通过结合来代替静态文本,不会显示任何内容。

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.

我的用户看起来如下:

<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>

在code的背后是:

The Code Behind is:

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

  }
}

当我尝试这在我的主窗口,一切都如预期:

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>

但是,这并不工作:​​

But this doesn't work:

<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>

标签的行为是正确的,所以使用属性文本

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.

推荐答案

在以下绑定你的用户控件

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

我不知道如何直接设置文本的MYTEXT财产的作品。您必须设置的DataContext 用户控件这个地方工作。

不管如何,这种结合是问题 - 我了解你的情况,你不想绑定到的DataContext 用户控件<的/ code>,因为这将不一定有MYTEXT属性。你要绑定到用户控件本身,特别是的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 ,这将改变根据您放置用户控件

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.

在这种情况下,本地指的是,你需要在定义用户控件命名空间:

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.

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

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