WPF UserControl:setter 未在多个控件中绑定一个属性并返回时调用 [英] WPF UserControl: setter not called on binding one property in multiple controls and back

查看:24
本文介绍了WPF UserControl:setter 未在多个控件中绑定一个属性并返回时调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WPF UserControl 在多个控件中绑定一个属性并返回时遇到问题.不会调用业务对象的设置器.我现在搜索了几个小时并尝试了一些不起作用的东西.

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.

我的代码

可以在这里下载:WpfApplicationUserControlProblem.zip

我的业务对象有 2 个要绑定的日期时间值.

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

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

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

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

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

我不明白

除了在更新 DependencyProperty 时不调用业务对象的 setter 之外,一切似乎都正常.但为什么?我还尝试了 DependencyProperties 或 MultiBindingConverters 的所有方法.每次尝试都失败了.

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.

有人可以帮忙吗?

推荐答案

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

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

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

你也可以声明你的 DateTimeValue 依赖属性默认为双向绑定:

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

<小时>

您可能会问为什么这对于 UserControl 的 XAML 中的两个内部"绑定也不是必需的,但是 DatePicker.SelectedDateTextBox.Text属性已注册到 BindsTwoWayByDefault.


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.

这篇关于WPF UserControl:setter 未在多个控件中绑定一个属性并返回时调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆