Reputation: 6626
i have three buttons in my wpf window what is the best way to disable button when clicked and make other two button enabled
<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" />
now i use this way in each button :(
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;
}
}
Upvotes: 0
Views: 1377
Reputation: 1704
For a pure XAML solution wrap them in a style stripped ListBox and tie the click to selection, and selection to disabled state.
Upvotes: 0
Reputation: 2869
What is your definition of 'best way'? Is it quick and few lines of code or elegant or..
Several ways come into my mind:
- Use MVVM Light: 1 relaycommand for the three buttons, 3 dependency objects (properties in the viewmodel) for isEnabled which will all be set to false, only set isEnabled to true for the button clicked (which could be sent as a parameter in the relaycommand).
- Use booleanconverters/booleaninverterconverters on the isEnabled property.
- Restyle radiobutton to look like a button, replace the three buttons with a radiobutton group. When one radiobutton is selected, the other ones will be deselected, style them as disabled. Prevent deselected items from being clicked.
Regards,
Michel
Upvotes: 1
Reputation: 1222
you can do somthing like this on page load -
PostBackOptions postBackOptions = new PostBackOptions(Button1);
Button1.OnClientClick = "this.disabled=true;";
Button1.OnClientClick += ClientScript.GetPostBackEventReference(postBackOptions);
Upvotes: 0