2017-02-22 54 views
1

我遇到了一個問題,我的WPF UserControl綁定了多個控件中的一個屬性並返回。業務對象的setter將不會被調用。我現在搜索了幾個小時,嘗試了一些不起作用的東西。WPF UserControl:setter未調用綁定多個控件中的一個屬性並返回

我的代碼

可以是這裏下載:WpfApplicationUserControlProblem.zip

我的業務對象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; 
    } 
} 

我的用戶控件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

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的XAM中的兩個「內部」綁定也不是必需的L,但DatePicker.SelectedDateTextBox.Text財產已登記BindsTwoWayByDefault

+0

謝謝!你是我今天的英雄! Mode = TwoWay是訣竅。 – jaz

相關問題