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"/>
Related
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" >
In WPF, a button and a click event is:
<Button Click="Btn_Click" />
private void Btn_Click(object sender, RoutedEventArgs e)
{
....
}
In DevExpress, according to the documentation, I would guess the equivalent would be:
<dxb:BarButtonItem ItemClick="Btn_Click" />
private event ItemClickEventHandler Btn_Click(Item sender, RoutedEvent e)
{
....
}
But it is not compiling. I would guess it is very easy but I cannot find any example in the documentation.
The second parameter of the handler should be a ItemClickEventArgs:
private void Btn_Click(object sender, ItemClickEventArgs e)
{
....
}
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 ?
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)
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();
}
}