2012-08-06 32 views
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"); 
     } 
    } 

... 

xaml。 默認樣式...

<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" /> 

.... enter image description here

我需要改變的 「Horizo​​ntalFillLoaded」 寬,像

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

好看! :) ... – Pacane 2012-08-07 11:45:31

回答

0

假設XAML是模板的一部分中的默認樣式:

使用TemplateBinding而不是Binding

+0

當我在運行時更改數值寬度時它會工作嗎? – SevenDays 2012-08-07 09:19:29

+0

如果您在運行時更改寬度,則需要將轉換器添加到計算正確寬度的綁定中以保持它的比例。 – 2012-08-07 10:42:22

+0

你能幫我找到正確的方向嗎?我不明白我需要什麼轉換器,它將如何工作。 – SevenDays 2012-08-07 10:59:57

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); 
    }