![](/img/trans.png)
[英]WPF MVVM: How to disable clicked button and enable all other buttons?
[英]Disable button when clicked and make other buttons enabled
我在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;
}
}
您對“最佳方式”的定義是什么? 是快速的幾行代碼還是優雅的..
我想到了幾種方法:
-使用MVVM Light:三個按鈕使用1個中繼命令,isEnabled的3個依賴對象(視圖模型中的屬性)將全部設置為false,單擊的按鈕僅將isEnabled設置為true(可以作為參數發送給中繼命令)。
-在isEnabled屬性上使用booleanconverters / booleaninverterconverters。
-重新設置單選按鈕的樣式以使其看起來像一個按鈕,將三個按鈕替換為一個單選按鈕組。 選擇一個單選按鈕時,將取消選擇其他單選按鈕,將其設置為禁用狀態。 防止單擊取消選擇的項目。
問候,
米歇爾
您可以在頁面加載時執行類似的操作-
PostBackOptions postBackOptions = new PostBackOptions(Button1);
Button1.OnClientClick = "this.disabled=true;";
Button1.OnClientClick += ClientScript.GetPostBackEventReference(postBackOptions);
對於純XAML解決方案,請將它們包裝在剝離樣式的ListBox中,並將單擊綁定到選擇,然后將選擇綁定到禁用狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.