2013-08-01 46 views
0

我想創建一個UserControl,它本質上是帶有TextBox的Label。現在我想能夠將TextBox.Text綁定到不同的值。如何正確設置用於綁定的WPF UserControl

爲此,我在我的UserControl中創建了一個DependencyProperty,現在我試圖將某些東西綁定到新創建的DependencyProperty,但是文本似乎沒有更新。

我UserControl1.xaml看起來是這樣的:

<UserControl x:Class="WpfApplication1.UserControl1" 
     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="48" d:DesignWidth="200"> 
<Grid> 
    <WrapPanel Height="48" HorizontalAlignment="Left" Name="wrapPanel1" VerticalAlignment="Top" Width="200"> 
     <Label Content="Label" Height="48" Name="label1" Width="100" /> 
     <TextBox Height="48" Name="textBox1" Width="100" /> 
    </WrapPanel> 
</Grid> 

而且我UserControl1.xaml.cs看起來是這樣的:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Diagnostics; 

namespace WpfApplication1 
{ 


    /// <summary> 
    /// Interaction logic for UserControl1.xaml 
    /// </summary> 
    public partial class UserControl1 : UserControl 
    { 
     private string value; 
     public string Value 
     { 
      get { return value; } 
      set 
      { 
       this.value = value; 
       textBox1.Text = value; 
       Trace.TraceInformation("value set in UserControl1"); 
      } 
     } 
     public static readonly DependencyProperty ValueProperty = 
      DependencyProperty.Register("Value", typeof(string), typeof(UserControl1)); 
     public UserControl1() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

我使用這樣的用戶控件:

<my:UserControl1 x:Name="userControl11" Value="{Binding Path=Name}" /> 

DataContext設置爲具有Name屬性並實現此屬性的INotifyPropertyChanged的對象。

回答

1

您將TextBox的Text和UserControl的Value之間的連接放置在錯誤的位置。 CLR屬性用於方便,但不被綁定引擎使用。您需要綁定文本框的文本,以用戶控件的值明確對XAML或隱藏代碼,如(假設你給你的用戶控件的名稱叫根):

<TextBox x:Name="textBox1" Text="{Binding Path=Value, ElementName=root}"/> 
+0

我試過了,這讓我的用戶控件得到更新,但現在我似乎有一個不同的錯誤:每當我更改'userControl11.TextBox1.Text'它不會更新,當我的綁定數據再次更改。我嘗試在TextBox中放入'Text =「{Binding Path = Value,ElementName = root,Mode = TwoWay}」',但這並不幫助它。 – FlyingFoX

+1

這很棘手。默認情況下,您的自定義依賴屬性是OneWay綁定,如果您在使用時未指定BindingMode。您有兩種選擇,一種是使用時指定Mode = TwoWay,另一種是覆蓋FrameworkPropertyMetadata以指定默認綁定模式爲TwoWay。 –

2

不能添加額外的邏輯或代碼的得到設置訪問器,用於包裝依賴項屬性的屬性。它不會被執行。

原因是因爲WPF設計器實際上會生成直接使用DependencyProperty的代碼。如果您在代碼中使用get/set屬性,只是爲了方便。因爲您希望DependencyProperty和屬性的get/set執行相同的操作,所以只應在get/set訪問器中調用GetValue和SetValue,同時傳遞相關的依賴項屬性。

見這些教程:

Dependency Properties Dependency Properties Overview

2

看看這個實現。它使用非常簡單的MVVM設計來獲取數據綁定。 基本上這是它在做什麼:

  • 你有你的用戶控件(視圖),設置它的DataContext到對應的視圖模型。
  • 視圖上的文本框然後綁定到該視圖模型中的公共屬性通過實現INotifyPropertyChanged,您可以使ViewModel在任何時候改變Text屬性的值時有效地更新UI。

這可以重複任意數量的綁定並應用於許多不同的類型,如Lists,ObservableCollections,整數。

查看

<UserControl x:Class="WpfApplication1.UserControl1" 
     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="48" d:DesignWidth="200"> 
<Grid> 
    <WrapPanel Height="48" HorizontalAlignment="Left" Name="wrapPanel1" VerticalAlignment="Top" Width="200"> 
     <Label Content="Label" Height="48" Name="label1" Width="100" /> 
     <TextBox Height="48" Name="textBox1" Width="100" Text={Binding Text} /> 
    </WrapPanel> 
</Grid> 

查看代碼隱藏

namespace WpfApplication1 
{ 
    using System.Windows; 
    using System.Windows.Controls; 

    public partial class UserControl1: UserControl 
    { 
     public UserControl1() 
     { 
      DataContext = new UserControl1ViewModel(); 
      InitializeComponent(); 
     } 
    } 
} 

視圖模型

namespace WpfApplication1 
{ 
    using System.Collections.ObjectModel; 
    using System.Windows.Controls; 

    class UserControl1ViewModel : INotifyPropertyChanged 
    { 
     // Ultimately, this field (text) would be moved to a model along with 
     // any other data fields for this view but for the sake of space, 
     // I've put it in the ViewModel. 
     private string text = ""; 
     public string Text 
     { 
      get { return text; } 
      set 
      { 
       text = value; 
       RaisePropertyChanged("Text"); 
      } 
     } 

     public MainWindowViewModel() 
     { 
      Text = "Hello!"; 
     } 

     // This is the implementation of INotifyPropertyChanged that is used 
     // to inform the UI of changes. 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void RaisePropertyChanged(string propertyName) 
     { 
      var handler = PropertyChanged; 
      if (handler != null) 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

祝你好運!