2014-09-23 106 views
10

我在用戶控件創建一個依賴屬性,但卻在用戶控件的變化並沒有通知視圖模型WPF用戶控件雙向綁定依賴項屬性

用戶控件

<UserControl x:Class="DPsample.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="300" d:DesignWidth="300"> 
<Grid> 
    <TextBox x:Name="txtName"></TextBox> 
</Grid> 

UserControl.cs

/// <summary> 
/// Interaction logic for UserControl1.xaml 
/// </summary> 
public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

    #region SampleProperty 

    public static readonly DependencyProperty SamplePropertyProperty 
              = DependencyProperty.Register("SampleProperty", 
              typeof(string), 
              typeof(UserControl1), 
              new PropertyMetadata(OnSamplePropertyChanged)); 


    public string SampleProperty 
    { 
     get { return (string)GetValue(SamplePropertyProperty); } 
     set { SetValue(SamplePropertyProperty, value); } 
    } 


    static void OnSamplePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
    { 
     (obj as UserControl1).OnSamplePropertyChanged(e); 
    } 
    private void OnSamplePropertyChanged(DependencyPropertyChangedEventArgs e) 
    { 
     string SamplePropertyNewValue = (string)e.NewValue; 

     txtName.Text = SamplePropertyNewValue; 
    } 

    #endregion 
} 

MainWindow

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:DPsample" x:Class="DPsample.MainWindow" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 

    <local:UserControl1 SampleProperty="{Binding SampleText,Mode=TwoWay}" HorizontalAlignment="Left" Margin="76,89,0,0" VerticalAlignment="Top" Width="99"/> 
    <Button Content="Show" HorizontalAlignment="Left" Margin="76,125,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 

</Grid> 

MainWindow.cs

public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new MainViewModel(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     var item = this.DataContext as MainViewModel; 
     MessageBox.Show(item.SampleText.ToString()); 
    } 

MainViewModel.cs

public class MainViewModel : NotifyViewModelBase 
{ 
    public MainViewModel() 
    { 
     this.SampleText = "test";   
    } 

    private string _sampleText; 
    public string SampleText 
    { 
     get { return _sampleText; } 
     set { _sampleText = value; OnPropertyChanged("SampleText"); } 
    } 
} 
+0

您還沒有編寫任何將「TextBox.Text」屬性的更改傳遞給「SampleProperty」的任何代碼。你還應該使用'RelativeSource = {RelativeSource AncestorType = UserControl}'來進行綁定。 – Clemens 2014-09-23 07:16:38

+0

@Clemens我需要使用viewmodel的usercontrol? – 2014-09-23 07:23:45

+0

不是這種情況。但是,UserControl也可以直接綁定到視圖模型的'SampleText'屬性。那麼就不需要'SampleProperty'。 – Clemens 2014-09-23 07:28:13

回答

26

綁定TextBox.Text屬性在用戶控件其SampleProperty這樣的:

<TextBox Text="{Binding SampleProperty, 
         RelativeSource={RelativeSource AncestorType=UserControl}}"/> 

現在,您可以簡單地刪除您的OnSamplePropertyChanged回調。


您可能還註冊SampleProperty雙向默認情況下,像這樣綁定:

public static readonly DependencyProperty 
    SamplePropertyProperty = DependencyProperty.Register(
     "SampleProperty", typeof(string), typeof(UserControl1), 
     new FrameworkPropertyMetadata(
      null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 
+1

不能相信我已經閱讀了很多關於自定義UserControls和DependencyProperties的教程和示例,並沒有提到解決將值傳遞迴模型的FrameworkPropertyMetadata。偉大的工作人! – 2016-04-05 11:14:16

2

我知道這一個古老的,回答的問題,但更簡單的方法來做到這一點,你可以綁定像這樣的屬性:
首先添加該屬性到用戶控件x:Name(例如x:Name="MyUC" 然後改變的結合:

<TextBox Text="{Binding ElementName=MyUC, Path=SampleProperty}"/> 

我看到這個解決方案的地方,但不記得在哪裏。我希望它有幫助。

相關問題