WPF Radiobutton equivalent - c#

What is the WPF equivalent for WinForms radio button CheckedChanged?
I have your basic 2 radio button set up, where when one is selected a textbox is enabled and when the other is selected it is disabled.
For the time being I was using RadioButton_Checked, except, I set IsChecked true for one button in the xaml. When I reference the textbox in that Checked method it throws NullReferenceException...
edit:
XAML:
<RadioButton Name="rb1" IsChecked="True" GroupName="1" Checked="rb1_Checked"></RadioButton>
<RadioButton Name="rb2" GroupName="1" Checked="rb2_Checked"></RadioButton>
C#:
private void rb2_Checked(object sender, RoutedEventArgs e)
{
txt.IsEnabled = false;
}
private void rb1_Checked(object sender, RoutedEventArgs e)
{
txt.IsEnabled = true; //null reference here on load
}

Can't you bind the enabled property of the textbox to the checked property of the appropriate radio button in your xaml?
<Textbox IsEnabled="{Binding ElementName=rb2, Path=IsChecked}" />

Related

XAML WPF CheckBox Validation

I have a list of CheckBox'es. I would like the user to select at least one before click the next button.
I would want the Button to remain Enabled, but use a TextBlock below the CheckBox to show the prompt to select at least one CheckBox.
How can I check that.
Code:
XAML
<CheckBox x:Name="CheckBox1" Content="CheckBox1" />
<CheckBox x:Name="CheckBox2" Content="CheckBox2" />
<CheckBox x:Name="CheckBox3" Content="CheckBox3" />
<CheckBox x:Name="CheckBox4" Content="CheckBox4" />
<Button x:Name="NextButton" Click="NextButton_Click"/>
Code Behind
private void NextButton_Click(object sender, RoutedEventArgs e) {
if (CheckBox1.IsChecked ?? false) {
// do something
}
// same for other checkBoxes
}
private void NextButton_Click(object sender, RoutedEventArgs e)
{
if (!CheckBox1.IsChecked && !CheckBox2.IsChecked && !CheckBox3.IsChecked && !CheckBox4.IsChecked)
{
// update TextBlock to alert the user
}
else
{
if (CheckBox1.IsChecked)
{
// do something
}
// same for other checkboxes
}
}
You can also do the following, based on the example of just one CheckBox:
XAML
<CheckBox x:Name="CheckBox1" Content="CheckBox1" Checked="CheckBox1_OnChecked"/>
// after all your CheckBoxes insert TextBlock below
// which is Visible by default (but invisible once any CheckBox is checked)
<TextBlock x:Name="TextBlock" Visibility="Visible" Text="Please, select at least 1 checkbox"/>
<Button x:Name="NextButton" Click="NextButton_Click" Height="Auto" Width="Auto" Content="Button"/>
Code Behind
private void NextButton_Click(object sender, RoutedEventArgs e)
{
// your code
}
// We make Visibility of TextBox hidden
// Think for yourself how to take into account
// several CheckBoxes checked vs unchecked
private void CheckBox1_OnChecked(object sender, RoutedEventArgs e)
{
TextBlock.Visibility = Visibility.Hidden;
}
Think for yourself how to take into account several CheckBoxes checked vs unchecked, you may also use CheckBoxes event handler for Unchecked event: Unchecked="CheckBox1_OnUnchecked"

C# / WPF Unmask password inside the passwordBox

How could I unmasked and masked the password inside the passwordBox whenever I click the checkBox? I'm using C# WPF template.
Here is my .XAML code:
<PasswordBox x:Name="passwordBox_password" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="5" Height="25" />
<CheckBox x:Name="checkBox_showPassword" Grid.Row="3" Grid.Column="1" Margin="5,0,5,5" Content="show password" Checked="checkBox_showPassword_Checked" Unchecked="checkBox_showPassword_Unchecked" />
Here is my .CS code:
private void checkBox_showPassword_Checked(object sender, RoutedEventArgs e)
{
// what to do here ?
}
private void checkBox_showPassword_Unchecked(object sender, RoutedEventArgs e)
{
// what to do here ?
}
Or is there another way to do it in WPF?
It's very simple to do that.
First you should to add the value PasswordChar in your PasswordBox:
<PasswordBox Name="PasswordHidden" PasswordChar="•"/>
Next under the PasswordBox tag you should to add a TextBox with Visibility value setted to Hidden:
<TextBox Name="PasswordUnmask" Visibility="Hidden"/>
And a trigger to show / hide the password, for example a simple text or a button. In my case I'm using a simple text.
<TextBlock Name="ShowPassword"/>
Next you need to add 3 different events in the trigger element, for example (this is valid for TextBlock or Image, if you want to use a Button you should to choose another events):
<TextBlock x:Name="ShowPassword" Text="SHOW" PreviewMouseDown="ShowPassword_PreviewMouseDown" PreviewMouseUp="ShowPassword_PreviewMouseUp" MouseLeave="ShowPassword_MouseLeave"/>
The events are PreviewMouseDown PreviewMouseUp and MouseLeave but you can choose the appropriate event for your situation.
Now in your code you need to program the functions:
private void ShowPassword_PreviewMouseDown(object sender, MouseButtonEventArgs e) => ShowPasswordFunction();
private void ShowPassword_PreviewMouseUp(object sender, MouseButtonEventArgs e) => HidePasswordFunction();
private void ShowPassword_MouseLeave(object sender, MouseEventArgs e) => HidePasswordFunction();
private void ShowPasswordFunction()
{
ShowPassword.Text = "HIDE";
PasswordUnmask.Visibility = Visibility.Visible;
PasswordHidden.Visibility = Visibility.Hidden;
PasswordUnmask.Text = PasswordHidden.Password;
}
private void HidePasswordFunction()
{
ShowPassword.Text = "SHOW";
PasswordUnmask.Visibility = Visibility.Hidden;
PasswordHidden.Visibility = Visibility.Visible;
}
The following link will bring you to the answer you are looking for my good sir. Mr Lamas did a great job of answering the how-to so I'd rather redirect you to the answer :)
showing password characters on some event for passwordbox
I recommend Using MahApps.Metro ... after installing it from nuget.org ... you must use it in the head of your xaml like this
xmlns:controls="http://metro.mahapps.com/winf/xaml/controls"
and then ... just use it's style for your PasswordBox control
<PasswordBox Style="{StaticResource MetroButtonRevealedPasswordBox}" />
you can even change the content for the show icon using the controls:PasswordBoxHelper.RevealButtonContent attached property

Textblock lostfocus event doesnt work as expected

I have a popup with listbox in it and this listbox is associated with a textbox.
Now while handling the lostfocus event for textbox, it works correctly when i try to click on other controls but the lostfocus doesnt get called when I try to click on window.Can somebody help me with this.
<TextBox x:name="txtbox1"/>
<Popup StaysOpen="False"
PlacementTarget="{Binding ElementName=txtbox1}"
Placement="Bottom">
<ListBox x:Name="CompanyListBox">
</Popup>
In constructor, I have txtbox1.LostFocus+="txtbox1_OnLostFocus"
private void txtbox1_OnLostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(txtbox1.Text))
{
txtbox1.Text = DefaultFeature;
Popup.IsOpen = false;
}
}

Getting grid.column property of a control in a general styled event handler

i have a lot of text boxes in my app, and a style which sets the event handler:
<EventSetter Event="MouseEnter" Handler="GeneralTextBoxMouseEnter"/>
Text Boxes are Located in Grids so for example this is the xaml code for one of the text boxes:
<Grid>
<TextBox Name="sat6" Grid.Column="1" Style="{StaticResource anHourSatAm}" />
</Grid>
this is the GeneralTextBoxMouseEnter event handler
private void GeneralTextBoxMouseEnter(object sender, MouseEventArgs e)
{
TextBox tb = (TextBox)sender;
MessageBox.Show((String)(tb.Grid.Column);
}
i get an error that such a property doesn't exist. but in properties box of VS2010 it exists, how can i retrieve the value?
you need to use static method named GetColumn of Grid.
private void GeneralTextBoxMouseEnter(object sender, MouseEventArgs e)
{
TextBox tb = (TextBox)sender;
MessageBox.Show(Grid.GetColumn(tb));
}
hope it helps..

System.NullReferenceException (Radio buttons)

I have two radio buttons and if i put the attribute IsChecked in XAML, the program crashes with a System.NullReferenceException. Additional information: Object reference not set to an instance of an object.
My radio buttons:
<RadioButton IsChecked="True" Name="Mint" Checked="Mint_Checked_1"
Foreground="Red" FlowDirection="RightToLeft" VerticalAlignment="Top"
HorizontalAlignment="Right" Margin="10,30,10,0" GroupName="Update_When"
Content="A">
</RadioButton>
<RadioButton IsChecked="False" Name="Changet" Checked="Changet_Checked_1"
FlowDirection="RightToLeft" VerticalAlignment="Top" HorizontalAlignment="Right"
Margin="10,53,10,0" GroupName="Update_When" Content="B">
</RadioButton>
My event code:
private void Mint_Checked_1(object sender, RoutedEventArgs e)
{
Mint.Foreground = Brushes.Red;
Changet.Foreground = Brushes.Black;
}
private void Changet_Checked_1(object sender, RoutedEventArgs e)
{
Mint.Foreground = Brushes.Black;
Changet.Foreground = Brushes.Red;
}
You are getting that exception because you are calling the checked event before the element is initialized, one way to solve this is to move the IsChecked=True to a Loaded event:
private void MyWindow_Loaded (object sender, RoutedEventArgs e)
{
Mint.IsChecked = true;
}
This worked for me, I moved the xaml line for the RadioButton that was to be checked to the last line in the sequence as follows:
Original order in XAML file, (This order causes an exception to be thrown)
RadioButton x:Name="RadioButton1"
RadioButton x:Name="RadioButton2" // Radio button that is initialized as selected (Is_Checked)
RadioButton x:Name="RadioButton3"
RadioButton x:Name="RadioButton4"
NO exception:
RadioButton x:Name="RadioButton1"
RadioButton x:Name="RadioButton3"
RadioButton x:Name="RadioButton4"
RadioButton x:Name="RadioButton2" // Radio button that is initialized as selected (checked)
I believe this is because when a radio button changes to "Is_checked" (even on initialization) it calls the RadioButton_IsChecked method to which it is bound.
So, during initialization RadioButton1 was initialized to false, then RadioButton2 was initialized to true which called the bound method and RadioButton3 and RadioButton4 were still not initialized and were null which caused the exception.
Note, the above does not change the location of the Radio Buttons on the form.
Also, I have bound all 4 radio buttons to the same method and use if/else if/else if/else to set the form properties I want when a button is checked.

Categories

Resources