2016-02-14 104 views
1

我有一個只有一個文本塊的用戶控件。我有自定義依賴項屬性來設置文本塊的文本。不過,我在綁定工作方面遇到了一些問題。綁定到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 = 「的TestString」 的作品,但如果我嘗試做一個結合,它不會工作即使相同的綁定適用於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}"/> 

作品,但爲什麼我需要的ElementName,當TextBlock的綁定不?

回答

1

在你UserControl你編程的DataContext屬性設置爲UserControl本身。所以當你在一個窗口中使用你的UserControl時,它不能繼承窗口的DataContext。 您的用戶控件沒有TestValue屬性,因此您會收到一條「聯編」錯誤消息。

的simpiest解決方案是從你的用戶控件中刪除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 Self}}" /> 
</UserControl> 

我希望它可以幫助你。

+0

由於「Self」是TextBlock,所以不起作用,但無論如何要感謝。 {Binding ElementName = UserControlName,Path = TestProperty}在我從usercontrol中刪除datacontext行後工作。 – FINDarkside

+1

它當然應該是'RelativeSource = {RelativeSource AncestorType = UserControl}'。作爲一般規則,*從不*設置UserControl的DataContext到自身,因爲這將有效地防止從UserControl的Parent繼承「外部」DataContext。 – Clemens

+0

你是對的,我犯了一個錯誤。它指出你不能設置你的UserControl的DataContext。 –