WPF用户控件双向绑定依赖项属性

问题描述:

我在用户控件创建一个依赖属性,但却在用户控件的变化并没有通知视图模型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

绑定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

我知道这一个古老的,回答的问题,但更简单的方法来做到这一点,你可以绑定像这样的属性:
首先添加该属性到用户控件x:Name(例如x:Name="MyUC" 然后改变的结合:

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

我看到这个解决方案的地方,但不记得在哪里。我希望它有帮助。