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;
}
}
Related
I have a Button with a TextBlock embedded inside. When the Button is clicked, I want to be able to fetch the TextBlock inside it and modify it's members.
Here is how my button is setup:
<Button Click="Select_Click" Style="{StaticResource ButtonStyle}" HorizontalAlignment="Left" Padding="0,20,20,20">
<TextBlock Text="My text" FontSize="20" Style="{StaticResource TextBlockStyle}"/>
</Button>
In my code behind I want to be able to access the embedded TextBlock:
public void Select_Click(object sender, RoutedEventArgs e)
{
// Get the `TextBlock` from `sender` here
}
I've taken a look at the visual tree of the Button but I'm not seeing the TextBlock. I called GetVisualChildren() on the Button but I only see a Grid and no way to get to the Textblock.
The content of the Button is stored in its Content property and in your case, the TextBlock is the content of the Button.
public void Select_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
TextBlock textBlock = (TextBlock)button.Content;
}
Just do some casting and it's pretty simple
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Establish_handlers();
}
void Establish_handlers()
{
Mybutton.Click += Mybutton_Click;
}
private void Mybutton_Click(object sender, RoutedEventArgs e)
{
Button clicked_button = (Button)sender;
TextBlock desired_text = (TextBlock)clicked_button.Content;
Textbox_Show_Button_Content.Text = desired_text.Text;
}
}
<StackPanel>
<Button x:Name="Mybutton">
<TextBlock>Hello</TextBlock>
</Button>
<TextBox x:Name="Textbox_Show_Button_Content"></TextBox>
</StackPanel>
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
Considering that piece of code :
XAML:
<Grid x:Name="LayoutRoot">
<Border x:Name="brd1" Height="100" Width="100" Background="Blue"
MouseLeftButtonUp="brd1_MouseLeftButtonUp"
MouseLeave="brd1_MouseLeave" />
</Grid>
C# :
private void brd1_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
brd1.Visibility = System.Windows.Visibility.Collapsed;
}
private void brd1_MouseLeave(object sender, MouseEventArgs e)
{
MessageBox.Show("Mouse Leave");
}
Why is the MouseLeave not firing when setting Visibility = Collapsed (ie : when I click on the border)?
Is there a way to always catch the MouseLeave event even if the control disappears (or one of its parent)? I cannot listen to the MouseButtonUp event, since my control can appear/disappear asynchronously at any time.
(note : my application is far more complex than that, this was just a simple example of what I need to do)
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..
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}" />