0

我正在开发一个带有进度矩形的滑块[见图]。此代码不适用于动态设置宽度

public class AudioSlider : Slider, INotifyPropertyChanged {
    public AudioSlider() {
      DefaultStyleKey = typeof(AudioSlider);
    }
    #region PropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name) {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
        handler(this, new PropertyChangedEventArgs(name));
    }

    #endregion
    private double _trwidth;
    public double TrackProgressWidth {
      get { return _trwidth; }
      set {
        _trwidth = value; OnPropertyChanged("TrackProgressWidth");
      }
    }

...

xml。默认样式...

<Rectangle x:Name="HorizontalFillLoaded" Height="12" MaxWidth="{Binding TrackProgressWidthMax,ElementName=audio}" Width="{Binding TrackProgressWidth,ElementName=audio}" IsHitTestVisible="False" Grid.Column="1" Fill="Black" Margin="0,0,0,28" />

…… 在此处输入图像描述

我需要改变“Horizo​​ntalFillLoaded”的宽度,比如

var t=new AudioSlider();
t.SetDownloadedProgress(50);//50%
4

2 回答 2

0

假设 XAML 是默认样式的模板的一部分:

使用TemplateBinding代替Binding

于 2012-08-07T06:47:41.743 回答
0
 Rectangle TrackLoaded;
    public override void OnApplyTemplate() {
      base.OnApplyTemplate();
      Rectangle txtBoxValue = base.GetTemplateChild("HorizontalFillLoaded") as Rectangle;

      Binding valueBinding = new Binding("TrackProgressWidth");
      valueBinding.Mode = BindingMode.TwoWay;
      valueBinding.Source = this; txtBoxValue.SetBinding(Rectangle.WidthProperty,
       valueBinding);
    }
于 2012-08-07T11:09:18.893 回答