2014-10-02 112 views
1

我有一個DataGrid和一個TextBox的自定義UserControl,我試圖使用DependencyProperties將這些元素綁定到數據綁定。該綁定適用於DataGrid,但不適用於TextBox。如何設置文本框與DependencyProperty的綁定

代碼:

public static readonly DependencyProperty BuiDataProperty = DependencyProperty.Register("BuiData", typeof(IEnumerable), typeof(BelastingTab), new PropertyMetadata(default(IEnumerable), BuiDataChanged)); 

private static void BuiDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    var Object = d as BelastingTab; 
    if (Object == null) return; 
    Object.BuiDataDataSourceChanged(d, e); 
} 

private void BuiDataDataSourceChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) 
{ 
    BuiDataTabel.ItemsSource = dependencyPropertyChangedEventArgs.NewValue as IEnumerable; 
} 

public IEnumerable BuiData 
{ 
    get { return (IEnumerable)GetValue(BuiDataProperty); } 
    set { SetValue(BuiDataProperty, value); } 
} 

,並在主XAML:

<src:BelastingTab BuiData="{Binding Path=Static.BuienRegulier[0].BuiTabel}"/> 

這是用於設置DataGrid的綁定代碼,我怎麼會去這樣做相同的文本框?

編輯: 這是我目前,

主要XAML:

<src:BelastingTab BuiData="{Binding Path=Static.BuienRegulier[0].BuiTabel}" HerhalingsTijd="{Binding Path=Static.BuienRegulier[0].HerhalingsTijd}"/> 

這是指一個字符串。在用戶控件XAML:

<TextBox Text="{Binding HerhalingsTijd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 

在用戶控件XAML CS:

public static readonly DependencyProperty HerhalingsTijdProperty = DependencyProperty.Register("HerhalingsTijd", typeof(string), typeof(BelastingTab), new PropertyMetadata(string.Empty)); 

public string HerhalingsTijd 
{ 
    get { return (string)GetValue(HerhalingsTijdProperty); } 
    set { SetValue(HerhalingsTijdProperty, value); } 
} 

回答

2

我看到做你想要的東西沒有問題。我創建了一個簡單的測試程序。我會在這裏提供代碼,希望它能幫助你以某種方式解決你錯誤的問題。

的代碼的UserControl1:

public partial class UserControl1 : UserControl 
{ 
    public static DependencyProperty TxtBoxValueProperty = DependencyProperty.Register("TxtBoxValue", typeof(String), typeof(UserControl1)); 

    public String TxtBoxValue 
    { 
     get { return (String)GetValue(TxtBoxValueProperty); } 
     set 
     { 
      SetValue(TxtBoxValueProperty, value); 
     } 
    } 

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    { 
     base.OnPropertyChanged(e); 
     if (e.Property == TxtBoxValueProperty) 
     { 
      // Do whatever you want with it 
     } 
    } 

    public UserControl1() 
    { 
     InitializeComponent(); 
    } 
} 

用戶控制的Xaml:

<StackPanel> 
    <TextBox Text="{Binding TxtBoxValue, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}, Mode=TwoWay}" Width="100" Height="50"/> 
    <TextBox></TextBox> 
</StackPanel> 

主窗口XAML:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:local="clr-namespace:WpfApplication1" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
<Grid> 
    <local:UserControl1 TxtBoxValue="{Binding TextBoxValue, Mode=TwoWay}"></local:UserControl1> 
</Grid> 

主窗口後面的代碼:

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    CancellationTokenSource cTS; 
    CancellationToken cT; 

    private String _textBoxValue; 
    public String TextBoxValue 
    { 
     get { return _textBoxValue; } 
     set 
     { 
      _textBoxValue = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("TextBoxValue")); 
      } 

      if (_textBoxValue.Contains("enough")) 
      { 
       cTS.Cancel(); 
      } 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     cTS = new CancellationTokenSource(); 
     cT = cTS.Token; 
     Task.Factory.StartNew(ChangeTextBoxValue, cT); 
    } 

    public void ChangeTextBoxValue() 
    { 
     while (true) 
     { 
      Random rnd = new Random(DateTime.Now.Millisecond); 
      TextBoxValue = (rnd.NextDouble() * 1000.0).ToString(); 
      Thread.Sleep(10000); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

請注意,我已經寫了這個非常快,做了我通常如何使用它(除了通知,我把一個ViewModelBase)。

如果這不起作用你的情況,它要麼我不明白的問題,或者你有一些非常具體的東西,但我懷疑這一點。

+0

我發佈了一個編輯與我有什麼,不工作 – user3692104 2014-10-02 17:01:58

+0

添加您的RelativeSource它的作品後,你可以向我解釋這是什麼? – user3692104 2014-10-02 17:04:52

+1

相對來源是必需的,因爲它是從用戶控件訪問類屬性後面的代碼的唯一方法。您在控件中沒有數據上下文,因此無論是將數據上下文設置爲Self,還是使用相對源代碼從代碼中訪問屬性,綁定都知道從何處獲取該屬性。你需要一點實驗和閱讀來充分理解這個 – XMight 2014-10-04 12:53:12