Disable button when clicked and make other buttons enabled - c#

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

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

you can do somthing like this on page load -
PostBackOptions postBackOptions = new PostBackOptions(Button1);
Button1.OnClientClick = "this.disabled=true;";
Button1.OnClientClick += ClientScript.GetPostBackEventReference(postBackOptions);

For a pure XAML solution wrap them in a style stripped ListBox and tie the click to selection, and selection to disabled state.

Related

Hide Button but keep function - XAML

First I've tried to implement a Click Event to my Textbox. Unfortunately, it doesn't work with XAML.
So, my plan is to add a Button and whenever you click this Button, the textfield below should change it's letter (back to 1).
My idea was to put the button over the first textbox and to hide it, so that you see the first textbox.
But, if I set the button as hidden, my function doesn't work anymore.
Is there a solution to hide the button, but, still keep the function for the second textbox?
Hidden means control is loaded, takes up space on the screen, but won't be operational(clickable), so it doesn't help you.
You could edit the button's ControlTemplate and make it a simple Grid with Transparent background, without the Hidden part of course.
And last thing, you could add MouseDown function on your TextBox so you won't need the button at all.
If you use bindings and commands you have two ways:
<Button Command="{Binding ClickCommand}">
<Button.Template>
<ControlTemplate TargetType="Button">
<TextBlock Text="Some text"/>
</ControlTemplate>
</Button.Template>
</Button>
or
<TextBlock Text="Some text">
<TextBlock.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding ClickCommand}"/>
</TextBlock.InputBindings>
</TextBlock>
Got it!
.xaml:
<TextBox MinWidth="90" x:Name="txtBoden" TextChanged="TxtBoden_TextChanged"
PreviewMouseDown="txtBoden_MouseDown"></TextBox>
.cs:
public void txtBoden_MouseDown(object sender, MouseButtonEventArgs e)
{
txtFach.Text = "1";
}

Is this concept of a button containing a textbox possible?

Recently I had been looking for a way to make the tabs in a TabControl editable and came across This example on telerik's website. That did exactly what I wanted but it got me thinking about a similar usage for buttons. I was wondering if it would be possible to use something like that and make a button that would show a textbox instead of the content presenter when say, you right click the button? I tried to make something like this work but so far have only ended up with a blank button.
<Button x:Name="SB" Height="222" Width="222" Click="SB_Click">
<ContentControl.ContentTemplate>
<DataTemplate>
<local:SuperButton Content="{Binding Path=x, Mode=TwoWay}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</Button>
Where x is a string variable and using the code behind from the link above (with a class name change, of course).
edit: This button will be in an itemscontrol, so I don't think naming the inner elements in xaml will work, but I do like the ease of Wolfgang's answer.
The WPF Content Model is really flexible and allows literally anything inside anything.
This is perfectly valid XAML:
<Button>
<TextBox/>
</Button>
Or even:
<Button>
<MediaElement Source="C:\Videos\WildLife.wmv"/>
</Button>
You can simply host a (e.g.) label (TextBlock) with the text AND a TextBox inside the Button and set their Visiblity properties.
That way, if you right click the button, the TextBox shows up.
<Button>
<Grid>
<TextBox Text=normal button caption" x:Name="label" />
<TextBox
x:Name="textbox"
Text="visible on right click"
MouseRightButtonDown="HandleRightClick"/>
</Grid>
</Button>
And then in your C# code create an event handler to set the Visiblity correctly.
void HandleRightClick(object sender, MouseButtonEventArgs e)
{
label.Visibility = Visibility.Collapsed;
textBlock.Visibility = Visibility.Visible;
}

Open Window Activate not working with button inside menu

I have a really strange behaviour, and I hope someone can help me out.
I have the following XAML layout:
<StackPanel Orientation="Horizontal">
<Menu>
<Menu.Items>
<MenuItem Padding="2,0,2,0">
<MenuItem.Header>
<Button Content="Details"
Click="Details_Click" />
</MenuItem.Header>
</MenuItem>
</Menu.Items>
</Menu>
<Button Content="Details"
Click="Details_Click" />
</StackPanel>
Please notice that both buttons have the same Event registered.
The Details_Click Event looks like this:
private void Details_Click(object sender, RoutedEventArgs e)
{
var viewer = new DictionaryViewer();
viewer.ShowActivated = true;
viewer.Show();
viewer.Topmost = true;
viewer.Topmost = false;
viewer.Activate();
viewer.Focus();
e.Handled = true;
return;
}
Now I am facing the problem, even with all the code from above the Window doesnt show up activated when I press the button inside the Menu but outside of it works with just .Activate();.
(How I know that window isnt activated: need 2 clicks to close/minimize/maximize it)
Why would my XAML Layout ruin the Activation of the DictionaryViewer(); window, with the button inside Menu?
(To your information the DictionaryViewer is totally empty, its a fresh window nothing implemented yet)
Edit:
Yes, I know there is the MenuItem_Click Event that may make it work, but I need/want the button inside the Menu how can I fix this issue?
THe reason this is happening is because the Button inside the MenuItem is gaining Focus after the Window has opened.
If you set the Focusable property of the button inside MenuItem, this fixes the issue.
E.g.
<StackPanel Orientation="Horizontal">
<Menu>
<Menu.Items>
<MenuItem Padding="2,0,2,0">
<MenuItem.Header>
<Button Content="Details"
Click="Details_Click"
Focusable="False" />
</MenuItem.Header>
</MenuItem>
</Menu.Items>
</Menu>
<Button Content="Details"
Click="Details_Click" />
</StackPanel>

Disable the drop down menu of a WPF ComboBox

I have a ComboBox as specified below:
<ComboBox Height="31" Margin="7,7,0,0" Name="callerID" IsEditable="True" Background="LightBlue" KeyDown="callerIDbar_KeyDown" Foreground="White" FontSize="17" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0" />
storedCalls is a collection of phone numbers that will be populated to the ComboBox.Items:
foreach (string call in storedCalls)
{
if (call != "Enter a number to dial")
callerID.Items.Add(call);
}
All this works fine. I populate the Items primary because I like the autocomplete that is driven by the values in the ComboBox's Items collections. Is there a way the XAML to disable the drop down error, and disable the drop down menu? I.e. make a simple auto complete textbox like control?
I have seen full on TextBox controls that include a bunch of code-behind and complicated markup, and this is not what I am looking to do. I just need to disable the ability of the drop down menu from showing.
You can handle the DropDownOpened event and then close it.
So in n the XAML you get:
<ComboBox x:Name="cb" DropDownOpened="cb_DropDownOpened"/>
And in Code Behind:
private void cbCategoria_DropDownOpened(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
cb.IsDropDownOpen = false;
}
I prefear this solution reather than set MaxDropDownHeight to 0.
Your choice.

Getting events from the scrollbar buttons?

how can I access the arrow buttons on the ends of scrollbars in silverlight? Are they button controls which can be accessesed through thescrollbar class or something else?
The reason I need to know this is so that when someone click on either of the arrow buttons I can run some custom functionality to the scrollbar
If you need to know what button of the scrollbar was clicked you can could access that via the ValueChanged property of the ScrollBar
For instance I've got a simple scroll bar with a textblock and on each click, the textblock displays which button was clicked.
<Grid x:Name="LayoutRoot" Background="White">
<ScrollBar Height="200" Orientation="Vertical" Width="20" ValueChanged="ScrollBar_ValueChanged" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="150,21,0,0" Name="textBlock1" Text="" Width="100" VerticalAlignment="Top" />
</Grid>
and the code behind would be
private void ScrollBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (e.NewValue > e.OldValue)
textBlock1.Text = "Down Arrow Clicked";
else
textBlock1.Text = "Up Arrow Clicked";
}
Solved this problem like so:
foreach( var o in horizontalBar.GetVisualDescendants( ) )
{
if(o is RepeatButton)
{
//set call back based on the name of the repeatbutton
}
}

Categories