2013-11-27 40 views
0

可能非常簡單,但我無法找到需要更改哪些代碼才能使其工作。我的媒體位看起來是這樣的:一個播放按鈕正在控制所有其他按鈕動作WPF

http://i.stack.imgur.com/0keGz.png

當我點擊任何其他所有的三個按鈕藏漢改變按鈕的發揮。

 public void ButtonPlay1_OnClick(object sender, RoutedEventArgs e) 
    { 
     var context = DataContext as PlayButton.SampleContext; 

     if (context == null) 
      return; 

     context.IsPlaying = !context.IsPlaying; 
    } 

    public void ButtonPlay2_OnClick(object sender, RoutedEventArgs e) 
    { 
     var context = DataContext as PlayButton.SampleContext; 

     if (context == null) 
      return; 

     context.IsPlaying = !context.IsPlaying; 
    } 

    public void ButtonPlay3_OnClick(object sender, RoutedEventArgs e) 
    { 
     var context = DataContext as PlayButton.SampleContext; 

     if (context == null) 
      return; 

     context.IsPlaying = !context.IsPlaying; 
    } 

有我的三個不同的按鈕按鈕點擊事件。我有一類是BoolToVisabilityConverter,也稱爲PLAYBUTTON類,它只是檢查是否按鈕被打,這是什麼在PLAYBUTTON類:

public partial class PlayButton : xamlNewAudit 
{ 
    public PlayButton() 
    { 
     InitializeComponent(); 
     DataContext = new SampleContext(); 
    } 

    public class SampleContext : INotifyPropertyChanged 
    { 
     private bool _isPlaying; 

     public bool IsPlaying 
     { 
      get { return _isPlaying; } 
      set 
      { 
       if (_isPlaying == value) 
        return; 

       _isPlaying = value; 

       OnPropertyChanged("IsPlaying"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      var handler = PropertyChanged; 

      if (handler != null) 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 

編輯:對不起了簡短的解釋,我只是想所以當我點擊其中一個按鈕時,其他兩個按鈕暫停播放。

編輯:按鈕的Xaml代碼是;

  <Button x:Name="ButtonPlay" Height="45" Click="ButtonPlay1_OnClick" Margin="-2,0,57,60" DockPanel.Dock="Top"> 
      <Button.Template> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Grid> 
         <Ellipse Stroke="Black" /> 

         <Image Source="Images/PlayButton.png" Visibility="{Binding IsPlaying, ConverterParameter={x:Static Visibility.Hidden}, Converter={Application:BoolToVisibilityConverter}}" /> 

         <Image Source="Images/pauseButton.png" Visibility="{Binding IsPlaying, ConverterParameter={x:Static Visibility.Visible}, Converter={Application:BoolToVisibilityConverter}}" /> 
        </Grid> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 

這是第一個按鈕的XAML代碼,其他按鈕的代碼是相同的,除了點擊事件鏈接到不同的方法。

+0

我想你可能想澄清你的問題。 – Inisheer

+2

我可以使用按鈕的xaml代碼嗎? –

+0

@Inisheer我在底部添加了編輯,很抱歉給出了簡要解釋。 –

回答

0

如果您對所有三個按鈕的綁定與IsPlaying相同,則所有三個按鈕將在IsPlaying更改時更改。最簡單的方法就是爲每個按鈕設置一個單獨的IsPlaying屬性。

+0

我爲PlayButton類添加了2個屬性,所以現在它們都指向不同的屬性,並且沒有任何更改。 –

+0

斷點設置者和獲取者,你將能夠看到發生了什麼 - 如果你的XAML正在正確地看待不同的屬性,那麼可能是設置了所有三個屬性。 –

1

你有三個Button控件。你需要三個不同的IsPlaying屬性,每個Button s。您需要三個不同的Click處理程序,它們使用三個不同的IsPlaying屬性。按照自己的方式進行操作,您還需要三個不同的ControlTemplate,它們也使用三個不同的IsPlaying屬性。


UPDATE >>>

有很多更容易的方式,以滿足您的要求。其中一種方法是使用ToggleButton而不是Button。利用這一點,你可以忘掉你的IsPlaying性質,只是使用ToggleButton.IsChecked值,而不是:

<ControlTemplate TargetType="{x:Type ToggleButton}"> 
    <Grid> 
     <Ellipse Stroke="Black" /> 
     <Image Source="Images/PlayButton.png" Visibility="{Binding IsChecked, 
ConverterParameter={x:Static Visibility.Hidden}, Converter={StaticResource 
Application:BoolToVisibilityConverter}}" /> 
     <Image Source="Images/pauseButton.png" Visibility="{Binding IsChecked, 
ConverterParameter={x:Static Visibility.Visible}, Converter={StaticResource 
Application:BoolToVisibilityConverter}}" /> 
    </Grid> 
</ControlTemplate> 

你似乎已經宣告你的Converter很奇怪......沒有,即使編譯?

+0

有沒有更簡單的方法來做我想做的事? –