代码之家  ›  专栏  ›  技术社区  ›  Pragmateek

绑定未按预期更新

  •  3
  • Pragmateek  · 技术社区  · 14 年前

    我正在建立一个简单的 用户控件 双重日期选择器 ,它定义了 关联属性 , 选定日期 :

    DoubleDatePicker.xaml文件 :

    <UserControl x:Class="TestWpfDoubleDatePicker.DoubleDatePicker"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit">    
    <StackPanel x:Name="LayoutRoot" Background="White">
        <toolkit:DatePicker x:Name="DateInput" SelectedDate="{Binding SelectedDate,Mode=TwoWay}" Margin="5,0,5,0" />
        <TextBlock Text="{Binding SelectedDate}" />
        <toolkit:DatePicker SelectedDate="{Binding SelectedDate,Mode=TwoWay}" Margin="5,0,5,0" />
    </StackPanel>
    

    DoubleDatePicker.xaml.cs :

    using System;
    using System.Windows;
    using System.Windows.Controls;
    
    
    namespace TestWpfDoubleDatePicker
    {
        public partial class DoubleDatePicker : UserControl
        {
            public static readonly DependencyProperty SelectedDateProperty =
            DependencyProperty.Register("SelectedDate", typeof(DateTime), typeof(DoubleDatePicker), null);
    
            public DateTime SelectedDate
            {
                get { return (DateTime)this.GetValue(SelectedDateProperty); }
                set { this.SetValue(SelectedDateProperty, value); }
            }
    
            public DoubleDatePicker()
            {
                this.InitializeComponent();
    
                this.DataContext = this;
            }
        }
    }
    

    我希望能把这本书装订起来 选定日期 但事情看起来并不是那么简单。 控件 :

    主窗口.xaml :

    <Window x:Class="TestWpfDoubleDatePicker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWpfDoubleDatePicker"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel x:Name="LayoutRoot" Background="White">
        <local:DoubleDatePicker x:Name="ddp" SelectedDate="{Binding SelectedDate}" />
        <Button Content="Update" Click="Button_Click" />
        <TextBlock Text="{Binding SelectedDate}" />
    </StackPanel>
    

    主窗口.xaml.cs :

    using System;
    using System.Windows;
    
    namespace TestWpfDoubleDatePicker
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public static readonly DependencyProperty SelectedDateProperty =
                DependencyProperty.Register("SelectedDate", typeof(DateTime), typeof(MainWindow), null);
    
            public DateTime SelectedDate
            {
                get { return (DateTime)this.GetValue(SelectedDateProperty); }
                set { this.SetValue(SelectedDateProperty, value); }
            }
    
            public MainWindow()
            {
                InitializeComponent();
    
                this.DataContext = this;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                this.SelectedDate = this.ddp.SelectedDate;
            }
        }
    }
    

    内部 双重日期选择器 属性在使用这两个属性中的任何一个进行更改时进行更新 日期选择器 控件 双重日期选择器

    但是,在外面 控件 不是自动更新的,也是获取 选定日期 财产 双重日期选择器 按钮 .

    我做错什么了?

    提前谢谢你的帮助。

    1 回复  |  直到 6 年前
        1
  •  5
  •   Arcturus    14 年前

    您所做的错误是使用以下内容覆盖控件内的DataContext:

    this.DataContext = this;
    

    现在,DatePicker不再绑定到预期的对象,而是绑定到DatePicker实例。我猜这不是你想要的日期选择器的工作方式;)。

    ElementName RelativeSource

    希望这能澄清问题;)

    我冒昧地使用ElementName绑定在DatePicker的XAML中重写了绑定:

    <UserControl x:Class="TestWpfDoubleDatePicker.DoubleDatePicker"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" 
    x:Name="Root">    
    <StackPanel x:Name="LayoutRoot" Background="White">
        <toolkit:DatePicker x:Name="DateInput" SelectedDate="{Binding ElementName=Root, Path=SelectedDate,Mode=TwoWay}" Margin="5,0,5,0" />
        <TextBlock Text="{Binding ElementName=Root, Path=SelectedDate}" />
        <toolkit:DatePicker SelectedDate="{Binding ElementName=Root, Path=SelectedDate,Mode=TwoWay}" Margin="5,0,5,0" />
    </StackPanel>