Change textblock text on click of rectangle using XAML triggers - wpf

void rectForeHead_MouseDown(object sender, MouseButtonEventArgs e)
{
txtLableBlock.Text += rectForeHead.ToolTip;
}
void rectChest_MouseDown(object sender, MouseButtonEventArgs e)
{
txtLableBlock.Text += rectChest.ToolTip;
}
I am changing textblock text property on click of number of rectangles.
How can I achieve the same with WPF Event triggers

Related

Why is my WPF mousewheel/previewmousewheel event still getting through after setting e.handled?

I have a C# WPF application with a User Control displayed inside a ScrollViewer:
<ScrollViewer HorizontalScrollBarVisibility="Auto" PreviewMouseWheel="ScrollViewer_PreviewMouseWheel" MouseWheel="ScrollViewer_MouseWheel">
<Drawing:DrawingWindow Name="Drawing" MouseWheel="Drawing_MouseWheel" PreviewMouseWheel="Drawing_PreviewMouseWheel"/>
</ScrollViewer>
I've tried putting an event-handler for all scroll-related events both on the contained element and the ScrollViewer itself. Each of the handlers is basically just this:
private void Drawing_MouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
}
private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
}
private void Drawing_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
}
private void ScrollViewer_MouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
}
Even with all this, the vertical scroll-bar still responds to the mouse-wheel when I scroll on top of the Drawing element.
What am I doing wrong? I've seen a lot of posts where setting e.handled appears to be the solution:
Disable mouse wheel scrolling in scrollviewer

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"/>

Focus the controls in a Tabcontrol

How can focus the controls when select a tabitem in a tabcontrol?
You should capture the Selection changed event of the TabControl and inside that you focus the control you need. Just as
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
control1.Focus();
}
Not sure I understand what you're asking completely, but you probably want to capture the SelectionChanged event for the TabControl:
public Window1()
{
InitializeComponent();
TabControl1.SelectionChanged += TabControl1_SelectionChanged;
}
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
TabItem item = (TabItem)TabControl1.SelectedItem;
// Find the first control in the TabItem content and focus it?
}

Resources