2012-07-04 170 views
0

我有三個按鈕,在我的WPF窗口什麼是點擊時禁用按鈕,使另外兩個按鈕啓用禁用按鈕點擊,並在其他按鈕啓用

<Button Name="initialzeButton" 
        Width="50" 
        Height="25" 
        Margin="460,0,0,0" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Center" 
        Click="initialzeButton_Click" 
        Content="Start" 
        Cursor="Hand" /> 
      <Button Name="uninitialzeButton" 
        Width="50" 
        Height="25" 
        Margin="0,0,64,0" 
        HorizontalAlignment="Right" 
        VerticalAlignment="Center" 
        Click="uninitialzeButton_Click" 
        Content="Stop" 
        Cursor="Hand" /> 
      <Button Name="loadButton" 
        Width="50" 
        Height="25" 
        Margin="0,0,9,0" 
        HorizontalAlignment="Right" 
        VerticalAlignment="Center" 
        Click="loadButton_Click" 
        Content="Load" 
        Cursor="Hand" /> 

現在我在每次使用這種方式的最佳途徑按鈕:(

 private void uninitialzeButton_Click(object sender, RoutedEventArgs e) 
     { 

      this.uninitialzeButton.IsEnabled = false; 
      if (!this.initialzeButton.IsEnabled) 
      { 
       this.initialzeButton.IsEnabled = true; 
      } 
      if (!this.loadButton.IsEnabled) 
      { 
       this.loadButton.IsEnabled = true; 
      } 


     } 

回答

0

你可以在頁面加載時做財產以後這樣的 -

PostBackOptions postBackOptions = new PostBackOptions(Button1); 
      Button1.OnClientClick = "this.disabled=true;"; 
      Button1.OnClientClick += ClientScript.GetPostBackEventReference(postBackOptions); 
+0

這是'button1_clcik'。您可以爲其他按鈕添加相同的處理程序 – akhil

+0

我認爲這是web項目我的項目是wpf – tito11

+0

然後添加適當的標記。 – akhil

1

是什麼你對'最好的方式'的定義?它是快速的代碼或優雅或..幾行

幾種方法來進入我的腦海:
- 使用MVVM光:1個relaycommand的三個按鈕,3點依賴的對象(在視圖模型屬性)進行的IsEnabled其將全部設置爲false,只有在單擊的按鈕(可以作爲繼電器命令中的參數發送)中將isEnabled設置爲true。
- 在isEnabled屬性上使用booleanconverters/booleaninverterconverters。
- Restyle單選按鈕看起來像一個按鈕,用單選按鈕組替換三個按鈕。當選擇一個單選按鈕時,其他按鈕將被取消選擇,將其設置爲禁用。防止取消選擇的項目被點擊。

問候,

米歇爾

+0

+1「restyle單選按鈕」 –

0

對於純XAML解決方案將它們包裝在一個樣式剝離ListBox和領帶的點擊選擇,和選擇爲禁用狀態。