1

我一直在尝试找到解决此问题的方法,但我对在 Internet 上找到的可能方法感到非常困惑。

我创建了一个包含 DateTime 滑块的 UserControl。

<UserControl x:Name="root">
    <Grid x:Name="gridPanel">
       <Slider x:Name="slider" HorizontalAlignment="Left" VerticalAlignment="Top" Height="34" Width="479"
                    Minimum="{Binding ElementName=root, Path=Minimum, Converter={StaticResource ResourceKey=dateToDoubleConverter}}"
                    Maximum="{Binding ElementName=root, Path=Maximum, Converter={StaticResource ResourceKey=dateToDoubleConverter}}"
                    Value="{Binding ElementName=root, Path=Value, Converter={StaticResource ResourceKey=dateToDoubleConverter}}" />
        <Label x:Name="lblStartTime" Content="{Binding ElementName=slider, Path=Minimum, Converter={StaticResource ResourceKey=doubleToStringConverter}}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,73,0,0" FontSize="10"/>
        <Label x:Name="lblStopTime" Content="{Binding ElementName=slider, Path=Maximum, Converter={StaticResource ResourceKey=doubleToStringConverter}}" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,73,10,0" FontSize="10"/>
    </Grid>
</UserControl>

后面的代码:

      public SliderPanel() {
         InitializeComponent();
         gridPanel.DataContext = this;
      }

      #region Dependency Property - Minimum
      public DateTime Minimum {
         get { return (DateTime)GetValue(MinimumProperty); }
         set { SetValue(MinimumProperty, value); }
      }

      public static readonly DependencyProperty MinimumProperty =
          DependencyProperty.Register("Minimum", typeof(DateTime), typeof(SliderPanel), new UIPropertyMetadata(DateTime.Now));
      #endregion

      #region Dependency Property - Maximum
      public DateTime Maximum {
         get { return (DateTime)GetValue(MaximumProperty); }
         set { SetValue(MaximumProperty, value); }
      }

      public static readonly DependencyProperty MaximumProperty =
          DependencyProperty.Register("Maximum", typeof(DateTime), typeof(SliderPanel), new UIPropertyMetadata(DateTime.Now.AddDays(1)));
      #endregion

      #region Dependency Property - Value
      public DateTime Value {
         get { return (DateTime)GetValue(ValueProperty); }
         set { SetValue(ValueProperty, value); }
      }

      public static readonly DependencyProperty ValueProperty =
          DependencyProperty.Register("Value", typeof(DateTime), typeof(SliderPanel), new UIPropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnValueChanged)));

      public static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
         SliderPanel sP = (SliderPanel)d;
         if (e.Property == SliderPanel.ValueProperty) {
            sP.slider.Value = ((DateTime)e.NewValue).Ticks;
         }
      }
      #endregion

在我的主窗口中,我使用 UserControl 并通过代码绑定一些属性,例如:

     System.Windows.Data.Binding bndPlayTime = new System.Windows.Data.Binding("CurrentPlayTime");
     bndPlayTime.Source = controller;
     bndPlayTime.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
     lblCurPlayTime.SetBinding(System.Windows.Controls.TextBox.TextProperty, bndPlayString);
     sliderPanel.SetBinding(SliderPanel.ValueProperty, bndPlayTime);

Controller 类实现 INotifyPropertyChanged:

  public DateTime CurrentPlayTime {
     get {
        return currentPlayTime;
     }

     set {
        if (DateTime.Compare(currentPlayTime, value) != 0) {
           currentPlayTime = value;
           NotifyPropertyChanged("CurrentPlayTime");
        }
     }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  private void NotifyPropertyChanged(string propertyName) {
     if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }

CurrentPlayTime 属性由计时器不断更新。现在我希望滑块相应地移动。绑定到标签有效。标签会定期更新。但是,绑定到 UserControl 的依赖属性不会导致滑块值更新(即使我已经实现了回调方法)。我错过了什么吗?

请多多包涵,我对 WPF 很陌生。我真的很感谢你的帮助。

4

1 回答 1

0

您是否尝试在您的绑定上将绑定模式设置为双向

于 2013-06-10T11:08:37.647 回答