绑定到usercontrol依赖项属性不起作用

时间:2016-02-14 17:12:36

标签: c# wpf xaml data-binding

我的usercontrol里面只有一个文本块。我有自定义依赖项属性来设置文本块的文本。但是我在绑定工作方面遇到了一些问题。

这是用户控件:

<UserControl x:Class="TestWpf2.TestControl"
             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="300" d:DesignWidth="300"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <TextBlock Text="{Binding TestProperty}"></TextBlock>
</UserControl>

public partial class TestControl : UserControl
{
    public static readonly DependencyProperty TestPropertyTestDependencyProperty = DependencyProperty.Register("TestProperty", typeof(string), typeof(TestControl));

    public string TestProperty
    {
        get { return (string)GetValue(TestPropertyTestDependencyProperty); }
        set { SetValue(TestPropertyTestDependencyProperty, value); }
    }

    public TestControl()
    {
        InitializeComponent();
    }
}

主窗口:

<Window x:Class="TestWpf2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWpf2"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <StackPanel>
        <local:TestControl TestProperty="TestString"/> <!-- Works -->
        <local:TestControl TestProperty="{Binding TestValue}"/><!-- Does not work -->
        <TextBlock Text="{Binding TestValue}"/> <!-- Works -->
    </StackPanel>
</Window>

public partial class MainWindow : Window
{
    public string TestValue { get; set; }
    public MainWindow()
    {
        TestValue = "TestString";
        InitializeComponent();
    }
}

正如评论所说,设置TestProperty =&#34; TestString&#34;但是如果我尝试进行绑定,即使相同的绑定适用于TextBlock,它也不会起作用。

这是绑定错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'TestValue' property not found on 'object' ''TestControl' (Name='')'. BindingExpression:Path=TestValue; DataItem='TestControl' (Name=''); target element is 'TestControl' (Name=''); target property is 'TestProperty' (type 'String')

将名称设置为主窗口,然后像这样绑定:

<local:TestControl TestProperty="{Binding ElementName=MainWindowName, Path=TestValue}"/>

工作,但是当TextBlock的绑定没有时,为什么我需要ElementName?

1 个答案:

答案 0 :(得分:1)

在您的UserControl中,您以编程方式将其DataContext属性设置为UserControl。因此,当您在Window中使用UserControl时,它无法继承Window的DataContext。 您的Usercontrol没有TestValue属性,因此您会收到一条错误消息。

最简单的解决方案是从UserControl中删除DataContext设置,然后更改TextBlock中的绑定:

<UserControl x:Class="TestWpf2.TestControl"
             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="300" d:DesignWidth="300">
    <TextBlock Text="{Binding Path=TestProperty, RelativeSource={RelativeSource AncestorType=UserControl}}" />
</UserControl>

我希望它可以帮到你。