2011-11-02 38 views
3

我已經實現了一個自定義的DependencyProperty並想從XAML綁定到它。由於某些原因,當綁定源(MainWindow.Test)更新時它不會更新。 綁定源不是DP,但會觸發PropertyChanged事件。綁定到來自XAML的自定義Depenendy屬性

<TextBlock Text="{Binding Test}" /> 

不工作

<local:DpTest Text="{Binding Test}"/> 

任何想法 更新但是用非自定義依賴項屬性

作品作品?


這裏是DP實現

using System; 
using System.Windows; 
using System.Windows.Controls; 

namespace WpfApplication3 
{ 
public partial class DpTest : UserControl 
{ 
    public DpTest() 
    { 
     DataContext = this; 
     InitializeComponent(); 
    } 

    public string Text 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DpTest), new PropertyMetadata(string.Empty, textChangedCallBack)); 

    static void textChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args) 
    { 
     int x = 5; 
    } 
} 
} 

這裏是如何使用它:

<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:WpfApplication3" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525"> 
<StackPanel> 
    <TextBlock Text="{Binding Test}"></TextBlock> 
    <local:DpTest Text="{Binding Test}"></local:DpTest> 
    <Button Click="Button_Click">Update</Button> 
</StackPanel></Window> 

後面的代碼與綁定源:

using System; 
using System.Collections.Generic; 
using System.Windows; 
using System.ComponentModel; 

namespace WpfApplication3 
{ 
public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    public MainWindow() 
    { 
     DataContext = this; 
     InitializeComponent(); 
    } 
    string _test; 
    public string Test 
    { 
     get { return _test; } 
     set 
     { 
      _test = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("Test")); 
      } 
     } 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     Test = "Updatet Text"; 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
} 
} 
+0

你檢查在輸出窗口綁定錯誤? –

回答

3

待辦事項UserControls設置DataContext = this;,這樣的情況下,所有綁定會失敗,如果你承擔DataContext被繼承,因爲這阻止它,是相當無形了。在UserControl綁定中,您應該命名該控件並執行綁定或使用RelativeSource

例如

<UserControl Name="control" ...> 
    <TextBlock Text="{Binding Text, ElementName=control}"/> 
<UserControl ...> 
    <TextBlock Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}"/> 
0

給你的窗口的名稱和更改綁定到以下幾點: (否則使用用戶控件的DataContext的,這是不是你在你的情況下,想要的東西)

... 
x:Name="mainWindow"> 
     <Grid> 
     <StackPanel> 
      <TextBlock Height="20" Background="Yellow" Text="{Binding Test}"></TextBlock> 
      <local:DpTest Text="{Binding Path=Test, ElementName=mainWindow, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></local:DpTest> 
      <Button Click="Button_Click">Update</Button> 

在你不」噸有你的用戶控件綁定尚未然後綁定方式如下:

<TextBlock Text="{Binding ElementName=dpTest, Path=Text, 
Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock> 

這個工作對我來說..

HTH