WPF UserControl绑定

时间:2013-06-10 11:00:46

标签: wpf data-binding dependency-properties

我一直在努力寻找这个问题的解决方案,但我对我在互联网上找到的可能方法感到困惑。

我创建了一个包含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很新。我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您是否尝试在绑定上设置binding modeTwoWay