【问题标题】:Slider with progress of downloaded audio带有下载音频进度的滑块
【发布时间】:2012-08-03 21:51:25
【问题描述】:

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

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%

【问题讨论】:

    标签: c# windows-phone-7 xaml slider dependency-properties


    【解决方案1】:

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

    使用TemplateBinding 而不是Binding

    【讨论】:

    • 如果您在运行时更改宽度,您需要在绑定中添加一个转换器来计算正确的宽度以使其保持成比例。
    • 你能帮我找到正确的方向吗?我不明白我需要什么转换器,它是如何工作的。
    • 如果没有更好的规格,我无法创造任何东西。让你的问题更清楚。
    【解决方案2】:
     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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多