Wpf, I need to hide and show a groupbox? - wpf

How I can show or hide a groupbox from xaml.cs. I try to do this in the checkbox's event:
private void cbDaily_Checked(object sender, RoutedEventArgs e)
{
gbCalendar.Visibility = Visibility.Visible;
}
But this don't work.

It must be work on checked/unchecked events of checkbox like this way :
private void chkTest_Checked(object sender, RoutedEventArgs e)
{
grpTest.Visibility = System.Windows.Visibility.Visible;
}
private void chkTest_Unchecked(object sender, RoutedEventArgs e)
{
grpTest.Visibility = System.Windows.Visibility.Hidden;
}
It is working fine in my sample application. Can you please give more details of your problem, so I can have better idea. Is event fired properly ? Make sure name of groupbox in code behind is correct or not ?

Related

TextBox (PasswordBox) SelectAll method doesn't work

I have some concept question here. I know how to select all text in TextBox or in PasswordBox. Via GotKeyboardFocus and PreviewMouseLeftButtonDown events, you know. This works fine.
XAML:
PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"
GotKeyboardFocus="SelectAllPassword"
CodeBehind
private void SelectAllPassword(Object sender, RoutedEventArgs e)
{
var pb = (sender as PasswordBox);
if (pb != null)
pb.SelectAll();
}
private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
var pb = (sender as PasswordBox);
if (pb != null)
if (!pb.IsKeyboardFocusWithin)
{
e.Handled = true;
pb.Focus();
}
}
But question is - why this doesn't work?
XAML:
PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"
CodeBehind:
private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
_txtPassword.SelectAll();
e.Handled = true;
}
Where _txtPassword - TextBox or PasswordBox control. So why I'm enforsed to Focus text control?
Actually, the selection is working.
You may feel that the text is not selected because it's not visually highlighted, but that's because the TextBox is not focused.
Try giving focus to your TextBox with the Tab key, you'll see the whole text highlighted.

How to select first/previous/next/last row in wpf DataGrid

I've got a WPF application which contains a standard DataGrid showing a lot of data.
Now I want to add buttons to select the next/first/previous/last row.
Is there a way to tell the DataGrid to change the selection for me?
I tried to use
private void SelectNext_Click(object sender, RoutedEventArgs e)
{
datagrid.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
but it does not work for me (does not change the selected item, sets just the cell focus to the first item instead).
private void SelectNext_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.Items.Count - 1 > datagrid.SelectedIndex)
{
datagrid.SelectedIndex++;
}
}
private void SelectPrevious_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedIndex > 0)
{
datagrid.SelectedIndex--;
}
}
private void SelectFirst_Click(object sender, RoutedEventArgs e)
{
dataGrid.SelectedIndex = 0;
}
private void SelectLast_Click(object sender, RoutedEventArgs e)
{
dataGrid.SelectedIndex = dataGrid.Items.Count - 1;
}
If you fill your datagrid with a datasource by databinding, you can do that
Example of button "next"
private void btnNext_Click(object sender, RoutedEventArgs e)
{
ICollectionView view = CollectionViewSource.GetDefaultView(yourDataSource);
view.MoveCurrentToNext();
}
The interface ICollectionView has all the methods you need for moving the current item
In XAML remember also to set
<DataGrid ..... IsSynchronizedWithCurrentItem="True" >

textBox1_Enter in wpf

What is the equivalent Following code in wpf
code in winapp :
private void textBox1_Enter(object sender, EventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
There is no Enter event on a WPF textbox - you could use the GotFocus event to the same effect though.
private void textbox1_GotFocus(object sender, System.Windows.RoutedEventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
this is accessed in the XAML as follows:
<TextBox GotFocus="textbox1_GotFocus"/>

textBox1_Leave in wpf

What is the equivalent Following code in wpf
code in winapp :
private void textBox1_Leave(object sender, EventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
(actually you could even reuse the same signature as before, all you need to do is hook the method to the LostFocus event instead of the Leave event)

Problems with event bubbeling (ScrollViewer)

I have a problem with bubbeling events. I manage to bubble events in borders, grid, stackpanel, but not in a ScrollViewer
If you look at the example below you will notice that when you click the TextBlock the event is bubbeled up to the Grid. But when I include the ScrollViewer the event stops here and is not sent up to the Grid.
Does anyone now whay this happends and if it can be fixed? I really need to be able to bubble events through a ScrollViewer as I use it all the time.
<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
<!--<ScrollViewer MouseLeftButtonDown="ScrollViewer_MouseLeftButtonDown">-->
<StackPanel Orientation="Vertical" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
<TextBlock Text="Click me to bubble an event" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>
</StackPanel>
<!--</ScrollViewer>-->
</Grid>
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("LayoutRoot clicked");
}
private void ScrollViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("ScrollViewer clicked");
e.Handled = false;
}
private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("StackPanel clicked");
e.Handled = false;
}
private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Textblock clicked");
e.Handled = false;
}
}
use AddHandler(yourDelegate, True); syntax for adding event handlers, which will ignore Handled flag set by other controls in the visual tree.
I had this problem and the fix posted by user572559 fixed my issue. For those that need it, below is what I did (modified for posting):
_scrollViewer = new ScrollViewer();
_scrollViewer.AddHandler(
ScrollViewer.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(OnMouseLeftButtonDown),
true);
_scrollViewer.AddHandler(
ScrollViewer.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(OnMouseLeftButtonUp),
true);
...
void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
...
}
void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
...
}
Also note that if you hare handling these you may be handling MouseMove as well. MouseMove worked for me without needing to do this, and it also does not seem to be supported in this fashion (not a bubbling event).
You can prevent e.Handled on MouseButtonEventArgs by overriding ScrollViewer like this
public sealed class ClickScrollViewer : ScrollViewer
{
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
this.Focus();
}
}

Resources