无法绑定用户控件以查看模型属性

时间:2017-05-23 09:58:21

标签: c# wpf xaml

为简单起见,我做了以下用户控制:
XAML:

<UserControl x:Class="Ex2.testUC"
             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" 
             xmlns:local="clr-namespace:Ex2"
             x:Name="testuc"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
        <TextBox Text="{Binding Path=MyText, ElementName=testuc}" />
    </StackPanel>
</UserControl>

CS.XAML:

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

namespace Ex2
{
    public partial class testUC : UserControl
    {
        public testUC()
        {
            InitializeComponent();
        }
        public string MyText
        {
            get { return (string)GetValue(MyTextProperty); }
            set { SetValue(MyTextProperty, value); }
        }
        public static readonly DependencyProperty MyTextProperty =
            DependencyProperty.Register("MyText", typeof(string), typeof(testUC));
    }
}

现在我已经将他插入到具有简单视图模型的视图中:
XAML:

<Window x:Class="Ex2.testWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Ex2"
        xmlns:control="clr-namespace:Ex2"
        mc:Ignorable="d"
        Title="testWindow" Height="300" Width="300">
    <StackPanel>
        <control:testUC MyText="{Binding Path=Text}"/>
        <Button Height="100" Click="Button_Click" Content="CLICK HERE"/>
        <Label x:Name="lbl" Height="50"/>
    </StackPanel>
</Window>

XAML.CS:

using System.Windows;

namespace Ex2
{
    public partial class testWindow : Window
    {
        private testVMText vm;
        public testWindow()
        {
            InitializeComponent();
            vm = new testVMText();
            DataContext = vm;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            lbl.Content = vm.Text;
        }
    }
}

查看型号:

using System.ComponentModel;

namespace Ex2
{
    class testVMText : INotifyPropertyChanged
    {
        public string Text { get; set; }
        public testVMText()
        {
            Text = "default";
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

单击按钮时,我希望打印视图模型中的Text值。 我的目的是将用户控件文本的文本框绑定到视图模型中的Text属性。但是,当我更改文本框中的文本并单击按钮时,始终会打印字符串“default”。
我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

Binding的模式设为TwoWay

<local:testUC MyText="{Binding Path=Text, Mode=TwoWay}"/>

BindsTwoWayByDefault类中注册依赖项属性时,也可以将testUC属性设置为true:

public static readonly DependencyProperty MyTextProperty =
        DependencyProperty.Register("MyText", typeof(string), typeof(testUC), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });