将数据绑定到滑块不起作用

时间:2012-06-17 15:58:15

标签: c# windows-phone-7 data-binding slider

我的Windows Phone项目中存在绑定问题。我正在将类绑定到滑块,但它不会更新值。如果我第一次启动应用程序,正确建立绑定连接并且滑块确实具有正确的值。但是,如果我更改“位置”或“持续时间”值,滑块将不会随之更新。

你能帮帮我吗?我没有任何线索,我做错了什么:(

Class.cs:

public class Status : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  private void PropChanged(string propName)
  {
    if (PropertyChanged != null)
    {
      PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
  }

  public double duration
  {
    get { return _duration; }
    set
    {
      if (this._duration == value)
        return;

      _duration = value;
      PropChanged("duration");
    }
  }

  public double position
  {
    get { return _position; }
    set
    {
      if (this._position == value)
        return;

      _position = value;
      PropChanged("position");
    }
  }
}

MainPage.xaml.cs中:

public partial class MainPage : PhoneApplicationPage
{
  public MainPage()
  {
    this.ContentPanel.DataContext = _Status;
  }
}

MainPage.xaml中:

 ...
<Grid x:Name="ContentPanel" Margin="12,12,12,0" Grid.RowSpan="2">
    <Slider x:Name="sl_PlayBackProgress"
            Value="{Binding Path=position}"
            Maximum="{Binding Path=duration}"
            Height="91"
            HorizontalAlignment="Left"
            Margin="0,40,0,0"
            VerticalAlignment="Top"
            Width="408"/>
</Grid>
...

1 个答案:

答案 0 :(得分:1)

不知道它是否与普通的XAML / WPF不同。但在正常情况下,我建议先将Binding.Mode设置为TwoWay,接下来只需在position.set入口处中断 - 然后查看已通过的value