WPF UserControl: setter not called on binding one property in multiple controls and back

谁都会走 提交于 2021-01-29 04:31:56

问题


I've got a problem with my WPF UserControl binding one property in multiple controls and back. The setters of the business object will not be called. I searched for hours now and tried serveral things which did not work.

My Code

Can be download here: WpfApplicationUserControlProblem.zip

My Business object has 2 DateTime Values to be bound.

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??
    }
}

My Main-Window has 2 user controls to bind the 2 values of my object

<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;
    }
}

My UserControl DateTimeUserControl has one DependencyProperty "DateTimeValue" for receiving the bound business value from the Main-Window. With the "DateTimeValuePropertyChangedCallback" I redirect the received value into a DateValue for the DatePicker and HourValue for the HourTextBox. Changing the DatePicker or HourTextBox should update the DependencyProperty "DateTimeValue" and therefore also the bounded business object. That was my plan.

<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)); }
    }
}

I don't get it

Everything seems to work fine except that the setter of the business object is not called when the DependencyProperty is updated. But why? I also tried everything with DependencyProperties or MultiBindingConverters. I failed on every try.

Can anybody help?


回答1:


The DateTimeValue Bindings should be declared as TwoWay, while UpdateSourceTrigger=PropertyChanged is certainly redundant:

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

You could also declare your DateTimeValue dependency property to bind two-way by default:

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

You may ask why this isn't also necessary on the two "internal" bindings in the UserControl's XAML, but both the DatePicker.SelectedDate and the TextBox.Text property are already registered with BindsTwoWayByDefault.



来源:https://stackoverflow.com/questions/42397319/wpf-usercontrol-setter-not-called-on-binding-one-property-in-multiple-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!