c# - 绑定(bind)到 UserControl DependencyProperty

标签 c# wpf binding user-controls dependency-properties

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

我的用户控件如下所示:

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

当我在我的主窗口中尝试这个时,一切都如预期的那样:

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

标签的行为是正确的,所以“文本”属性没有问题

我的错误是什么?

最佳答案

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

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

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

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

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

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

在这种情况下,local 指的是您需要在 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"
         ...>

此时您的第二个示例应该可以正常工作。

关于c# - 绑定(bind)到 UserControl DependencyProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16985382/

相关文章:

c# - 如何让子窗口与主窗口一起最小化和最大化

wpf - 将组合框绑定(bind)到数据网格的 MVVM 问题

wpf - VisualCollection 抛出超出范围异常,绑定(bind)到 Observable 集合

c# - 如何拼接很少重叠的图像?

c# - 在数据库中上传检查

WPF:绑定(bind)到自定义结构

wpf - 根据 WPF 中多个控件的值启用/禁用控件

c# - MVVMCross支持asp.net编程吗?

c# - ZedGraph如何检测放大或缩小?

wpf - 在样式中绑定(bind)命令