WPF UserControl:setter未调用绑定多个控件中的一个属性并返回

时间:2017-02-22 16:39:44

标签: c# wpf xaml user-controls

我的WPF UserControl在多个控件中绑定一个属性时遇到了问题。不会调用业务对象的setter。我现在搜索了几个小时,尝试了几件不起作用的东西。

我的代码

可以在这里下载:WpfApplicationUserControlProblem.zip

我的Business对象有2个要绑定的DateTime值。

public class BusinessObject
{
    private DateTime _value1 = DateTime.Today.AddHours(10);
    public DateTime Value1
    {
        get { return _value1; }
        set { _value1 = value; }        // will never be called but why??
    }

    private DateTime _value2 = DateTime.Today.AddDays(1).AddHours(15);
    public DateTime Value2
    {
        get { return _value2; }
        set { _value2 = value; }        // will never be called but why??
    }
}

我的主窗口有2个用户控件来绑定我的对象的2个值

<Window x:Class="WpfApplicationUserControlProblem.MainWindow"
    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:WpfApplicationUserControlProblem"
    mc:Ignorable="d"
    Title="MainWindow" Height="120.961" Width="274.489">
<Grid>
    <local:DateTimeUserControl DateTimeValue="{Binding Value1, UpdateSourceTrigger=PropertyChanged}" Margin="10,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="241"/>
    <local:DateTimeUserControl DateTimeValue="{Binding Value2, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="10,39,0,0" VerticalAlignment="Top" Width="241"/>
</Grid>

public partial class MainWindow : Window
{
    private BusinessObject _businessObject = new BusinessObject();
    public MainWindow()
    {
        InitializeComponent();
        DataContext = _businessObject;
    }
}

我的UserControl DateTimeUserControl有一个DependencyProperty“DateTimeValue”,用于从主窗口接收绑定的业务值。使用“DateTimeValuePropertyChangedCallback”,我将接收到的值重定向到DatePicker的DateValue和HourTextBox的HourValue。更改DatePicker或HourTextBox应更新DependencyProperty“DateTimeValue”,因此也更新有界业务对象。那是我的计划。

<UserControl x:Class="WpfApplicationUserControlProblem.DateTimeUserControl"
         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:WpfApplicationUserControlProblem"
         x:Name="_this"
         mc:Ignorable="d">
<Grid>
    <DatePicker SelectedDate="{Binding Path=DateValue, ElementName=_this, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Margin="0,0,61,0"/>
    <TextBox Text="{Binding Path=HourValue, ElementName=_this, UpdateSourceTrigger=PropertyChanged}" Height="24" VerticalAlignment="Top" HorizontalAlignment="Right" Width="56"/>
</Grid>

public partial class DateTimeUserControl : UserControl, INotifyPropertyChanged
{
    public DateTimeUserControl()
    {
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public static readonly DependencyProperty DateTimeValueProperty = DependencyProperty.Register(nameof(DateTimeValue), typeof(DateTime), typeof(DateTimeUserControl), new PropertyMetadata(DateTimeValuePropertyChangedCallback));
    public DateTime DateTimeValue
    {
        get { return (DateTime)GetValue(DateTimeValueProperty); }
        set { SetValue(DateTimeValueProperty, value); }
    }

    private static void DateTimeValuePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DateTimeUserControl control = d as DateTimeUserControl;
        control.FirePropertyChanged(d, new PropertyChangedEventArgs(nameof(DateValue)));
        control.FirePropertyChanged(d, new PropertyChangedEventArgs(nameof(HourValue)));
    }

    private void FirePropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        PropertyChanged?.Invoke(sender, args);
    }

    public DateTime DateValue
    {
        get { return DateTimeValue.Date; }
        set { DateTimeValue = value.Date.AddHours(DateTimeValue.Hour); }
    }

    public string HourValue
    {
        get { return DateTimeValue.Hour.ToString(); }
        set { DateTimeValue = DateTimeValue.Date.AddHours(int.Parse(value)); }
    }
}

我没理解

除非更新DependencyProperty时未调用业务对象的setter,否则一切似乎都能正常工作。但为什么?我还尝试使用DependencyProperties或MultiBindingConverters。我每次尝试都失败了。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

DateTimeValue绑定应声明为TwoWay,而UpdateSourceTrigger=PropertyChanged肯定是多余的:

<local:DateTimeUserControl DateTimeValue="{Binding Value1, Mode=TwoWay}" .../>

您还可以声明您的DateTimeValue依赖项属性默认绑定为双向:

public static readonly DependencyProperty DateTimeValueProperty =
    DependencyProperty.Register(
        nameof(DateTimeValue),
        typeof(DateTime),
        typeof(DateTimeUserControl),
        new FrameworkPropertyMetadata(
            default(DateTime),
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
            DateTimeValuePropertyChangedCallback));

您可能会问为什么在UserControl的XAML中的两个“内部”绑定上也不需要这样做,但DatePicker.SelectedDateTextBox.Text属性都已在BindsTwoWayByDefault注册