WPF Toggle Button Checked/Uchecked event with one handler - wpf

I am using a ToggleButton in a WPF window:
<ToggleButton Height="37"
HorizontalAlignment="Left"
Margin="485.738,254.419,0,0"
VerticalAlignment="Top"
Width="109"
IsEnabled="True"
Checked="toggleAPDTimeoutErr_Checked"
Unchecked="toggleAPDTimeoutErr_Unchecked">Timeout</ToggleButton>
I have two events that I am monitoring, but this is done in two different code behind handlers. How can this be done in only one?
I will have many ToggleButtons, and the code can get large.

You can attach a single click event of your ToggleButton and in its handler you can check the ToggleButton IsChecked property by type casting the sender object in your handler like this -
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
if((sender as ToggleButton).IsChecked)
{
// Code for Checked state
}
else
{
// Code for Un-Checked state
}
}
Xaml:
<ToggleButton Height="37" HorizontalAlignment="Left" Margin="485.738,254.419,0,0" VerticalAlignment="Top" Width="109" IsEnabled="True" Click="ToggleButton_Click">Timeout</ToggleButton>

You should not use Click event as some answers suggest, because it will not work when the property IsChecked is changed by code or any other event than mouse (keyboard, animation..). This is simply a bug.
Instead you can use the same handler for both Checked and Unchecked and do action depending on IsChecked property.
<ToggleButton
Checked="toggleButton_IsCheckedChanged"
Unchecked="toggleButton_IsCheckedChanged" />

Try this
private void tBtn_super_Click(object sender, RoutedEventArgs e)
{
if (tBtn_super.IsChecked == true)
{
MessageBox.Show("True");
}
else
{
MessageBox.Show("False");
}
}

Related

WPF event trigger for clear TextBox

So i have the TextBox:
<TextBox Controls:TextBoxHelper.ClearTextButton="True"
LostFocus="textboxNewValueCell_LostFocus"
TextChanged="textboxNewValueCell_TextChanged"/>
And when press on Clear button i want to catch the event.
Is it possible ?
I did not find any event
The ClearTextButton simply calls Clear() on the TextBox. There is no specific event raised. The best you can do is to handle the TextChanged event:
private void textboxNewValueCell_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Text.Length == 0)
{
//the TextBox was cleared and the Button was maybe clicked...
}
}
First, give your TextBox a name. Then, create a click event on the Button. when the click event fires, handle the clearing of the TextBox in the CodeBehind.
<Grid>
<TextBox x:Name="MyTextBox" Text="Some Text"/>
<Button x:Name="ClearButton" Click="ClearButton_Click"/>
</Grid>
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
MyTextBox.Text = string.Empty;
}

WPF TextBox lostFocus event trigger

I am new to WPF,
In my mainWindow I have multiple TextBox, so whenever a user enters different inputs in these textbox I want to implement those changes in the code behind, as soon as user leaves the focus of the textbox.
For example, my textBox looks like this:
<TextBox Name="SpiralAngleTextBox"
Grid.Column="1" Grid.Row="4"
Margin="5,5,5,5" SelectedText="0"/>
I am not looking to do any kind of input validation. What I want is to trigger some calculations or call a function whenever the TextBox leaves focus after contents of TextBox is updated.
You can write an EventHandler
<TextBox Name="SpiralAngleTextBox"
Grid.Column="1" Grid.Row="4"
Margin="5,5,5,5" SelectedText="0" LostFocus="SpiralAngleTextBox_LostFocus"/>
and in the xaml.cs
private void SpiralAngleTextBox_LostFocus(object sender, RoutedEventArgs e)
{
foo();
}
If you just want it to do stuff when the textbox content changes you can try something like this:
<TextBox Name="SpiralAngleTextBox"
Grid.Column="1" Grid.Row="4"
Margin="5,5,5,5" SelectedText="0" LostFocus="SpiralAngleTextBox_LostFocus"
TextChanged="SpiralAngleTextBox_TextChanged"/>
and in the xaml.cs
bool hasChanged;
private void SpiralAngleTextBox_LostFocus(object sender, RoutedEventArgs e)
{
if(hasChanged)
foo();
hasChanged = false;
}
private void SpiralAngleTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
hasChanged = true;
}
All you need to do is to bind to TextBox.Text
<TextBox Text="{Binding MyProperty}" />
Where MyProperty is some property in your code-behind. This is because TextBox.Text updates on lost focus (UpdateSourceTrigger=LostFocus by default.) You can learn more here: https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-control-when-the-textbox-text-updates-the-source

Radio button : difference between user click and programatic selection

I have a group of 2 radio buttons and I need to know when the user clicks on one of them.
One of these 2 radio buttons must be selected by default (at page creation).
Why the check callback is called when I select the radiobutton from code? How can I differentiate them ? It bothers me since when the user perform the action, I must perform a web request, but when I select it from code at init time, it mustn't do any req.
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
Search(ContractTextBox.Text, true);
}
<ItemsControl ItemsSource="{Binding SelectedMainSearchItem.SubLevels}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="10">
<RadioButton Content="{Binding Name}"
GroupName="ExclusiveGroupL3"
IsChecked="{Binding IsSelected, Mode=TwoWay}"
Checked="RadioButton_Checked"
FontSize="18"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate
The usual way to deal with a situation like this is to define a bool property to use as a 'flag':
private bool isUserSelection = true;
...
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
if (isUserSelection) Search(ContractTextBox.Text, true);
}
And when you set the CheckBox value programmatically, set the flag to false:
isUserSelection = false;
SetCheckBoxValue(true);
isUserSelection = true;

How do I stop binding properties from updating?

I have a dialog that pops up over the main screen (it's actually a user control that appears on the page as per the application demo from Billy Hollis) in my application that has data from the main screen to be edited. The main screen is read only.
The problem I have is that when I change the data in the dialog, the data on the main screen updates as well. Clearly they are bound to the same object, but is there a way to stop the binding update until I click save in my dialog?
You could use a BindingGroup :
...
<StackPanel Name="panel">
<StackPanel.BindingGroup>
<BindingGroup Name="bindingGroup"/>
</StackPanel.BindingGroup>
<TextBox Text="{Binding Foo}"/>
<TextBox Text="{Binding Bar}"/>
<Button Name="btnSubmit" Content="Submit" OnClick="btnSubmit_Click"/>
<Button Name="btnCancel" Content="Cancel" OnClick="btnCancel_Click"/>
</StackPanel>
...
Code behind :
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
panel.BindingGroup.BeginEdit();
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
panel.BindingGroup.CommitEdit();
panel.BindingGroup.BeginEdit();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
panel.BindingGroup.CancelEdit();
panel.BindingGroup.BeginEdit();
}
Have a look at the Binding.UpdateSourceTrigger property.
You can set the Binding in your dialog like so
<TextBox Name="myTextBox"
Text={Binding Path=MyProperty, UpdateSourceTrigger=Explicit} />
And then call the UpdateSource method in your button save event
myTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
Once you've called UpdateSource the source object will be updated with the value from the TextBox
I also choose to use BindingGroup. But instead of BeginEdit() / CommitEdit() / CancelEdit() pattern I call UpdateSource() explicitly on all the bindings associated with BindingGroup. This approach allows me to add only one event handler instead of 3.
private void OkButton_Click(object sender, RoutedEventArgs e)
{
CommitChanges();
DialogResult = true;
Close();
}
private void CommitChanges()
{
foreach (var bindingExpression in this.BindingGroup.BindingExpressions)
{
bindingExpression.UpdateSource();
}
}

Is there a way to programatically close a menuitem in WPF

I have a menu in wpf that has an input box and a button on it. Once the user clicks the button I need to close the menu.
Is there a way to do this?
<Menu x:Name="MainMenu">
<MenuItem Header="Main">
<MenuItem Header="SubMenu" x:Name="SubMenu">
<StackPanel Orientation="Horizontal">
<TextBox Width="50" x:Name="TextBox" />
<Button Content="Click Me and Close" x:Name="Button" IsDefault="True"/>
</StackPanel>
</MenuItem>
</MenuItem>
Thanks,
Jon
Get hold of the MenuItem and do:
_menuItem.IsSubmenuOpen = false;
Easy way to get hold of it:
<Button x:Name="_button" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=2}"/>
Code-behind:
_button.Click += delegate
{
(_button.Tag as MenuItem).IsSubmenuOpen = false;
};
I find that using IsSubmenuOpen doesn't properly eliminate focus from the Menu containing the MenuItem (especially if the Menu is in a ToolBar - the top-level MenuItem remains Selected even though the menu is "Closed"). I find sending a MouseUp event to the MenuItem works better (in the button's, or nested control's, Click event handler):
private void button_Click(object sender, RoutedEventArgs e) {
Button b = sender as Button;
if (b == null || !(b.Parent is MenuItem))
return;
MenuItem mi = b.Parent as MenuItem;
mi.RaiseEvent(
new MouseButtonEventArgs(
Mouse.PrimaryDevice, 0, MouseButton.Left
)
{RoutedEvent=Mouse.MouseUpEvent}
);
}
Steve thanks for your solution. That is actually right answer, and finally something that really works beside of tons of bad answers over the internet. I have a shorter (and more safe) solution based on your anwser. Because direct parent (e.Parent) of the button is not always MenuItem (from original answer that is StackPanel), your solution will not work. So just set the Name property of the MenuItem (Name="MyMenuItem") and hook this handler on the Button:
private void Button_Click(object sender, RoutedEventArgs e) {
MyMenuItem.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) {
RoutedEvent = Mouse.MouseUpEvent
});
}

Resources