Wpf文本框UpdateSourceTrigger不更新源

时间:2016-09-23 11:55:49

标签: c# wpf

我有一个带有TextBox的简单窗口

XAML

<Window x:Class="Configurator.ConfiguratorWindow"
    x:Name="ConfigWindow" DataContext="{Binding RelativeSource={RelativeSource Self}}">

<TextBox x:Name="DescriptionTextBox" Text="{Binding Path=Description, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>

</Window>

中的代码

public partial class ConfiguratorWindow : Window
{
    public ConfiguratorWindow()
        {
            InitializeComponent();
        }

    private static DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(ConfiguratorWindow), new PropertyMetadata());

    public string Description
        {
            get { return GetValue(DescriptionProperty).ToString(); }
            set {
                    SetValue(DescriptionProperty, value);
                    _actual_monitor.Description = value;
                }
        }
}

图形正在更新,但是当我更改文本框中的文本并失去焦点时,它不会更新源属性。

有什么问题?

3 个答案:

答案 0 :(得分:1)

DependencyProperties用于UserControls而不是ViewModel类型绑定。

你应该

  1. 创建ConfigurationWindowViewModel(阅读MVVM)并实施INotifyPropertyChanged

  2. 使用INotifyPropertyChanged

  3. 创建属性说明
  4. 创建要设置为ConfigurationWindow的DataContext的视图模型的新实例。

答案 1 :(得分:1)

依赖项属性的CLR包装器的getter和setter不得包含除GetValue和SetValue之外的任何其他代码。原因在MSDN上的XAML Loading and Dependency Properties文章中进行了解释。

因此,从setter中删除_actual_monitor.Description = value;赋值并添加PropertyChangedCallback以响应属性值更改:

public partial class ConfiguratorWindow : Window
{
    public ConfiguratorWindow()
    {
        InitializeComponent();
    }

    private static DependencyProperty DescriptionProperty = DependencyProperty.Register(
        "Description", typeof(string), typeof(ConfiguratorWindow),
        new PropertyMetadata(DescriptionPropertyChanged));

    public string Description
    {
        get { return (string)GetValue(DescriptionProperty); }
        set { SetValue(DescriptionProperty, value); }
    }

    private static void DescriptionPropertyChanged(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ConfiguratorWindow obj = d as ConfiguratorWindow;
        obj._actual_monitor.Text = (string)e.newValue;
    }
}

答案 2 :(得分:0)

试试这个

<Window x:Class="Configurator.ConfiguratorWindow"
         xmlns:myWindow="clr-namespace:YourNamespace"
x:Name="ConfigWindow" DataContext="{Binding RelativeSource={RelativeSource Self}}">

<TextBox x:Name="DescriptionTextBox" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type myWindow}}, Path=Description, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>

public partial class ConfiguratorWindow : Window
{
public ConfiguratorWindow()
    {
        InitializeComponent();
    }

private static DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(ConfiguratorWindow), new PropertyMetadata(null, CallBack);

 private static void callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var foo = d as ConfiguratorWindow ;
       all you need to do, you can do here
    }

public string Description
    {
        get { return GetValue(DescriptionProperty).ToString(); }
        set { SetValue(DescriptionProperty, value);}
    }
}

但是,拥有一个View Model并绑定到那里的属性会更容易。

相关问题