My WPF window has a Button and a ListView. On the Button's click event, it has the following code.
private void myButton1_Click(object sender, RoutedEventArgs e)
{
ListViewItem lvi = new ListViewItem()
{
Content = "Hello",
Focusable = true,
IsEnabled = true
};
this.listView1.Items.Add(lvi);
lvi.Focus();
}
Here is the problem, the focus cannot move to lvi(ListViewItem), after the user clicks the Button. The code lvi.Focus() does not have any effect. Can someone tell me why it happens, and how can I solve it?
Update:
Find the solution. call this code, otherwise the focus is still captured by the button.
this.Dispatcher.BeginInvoke(new Action(() => lvi.Focus()), System.Windows.Threading.DispatcherPriority.Input);
Try the following:
ListViewItem lvi = new ListViewItem();
lvi.IsSelected = true;
Hope that helps ;)
Related
I have a MenuItem where I set IsCheckable = True and a method that fires on the Click event that checks or unchecks the MenuItem.
<MenuItem x:Name="DebugWindowMenuItem" Header="Debug Window" IsCheckable="True" IsChecked="False" Click="DebugWindow_Click" />
Then my C# code looks like this...
private void DebugWindow_Click(object sender, RoutedEventArgs e) {
Console.WriteLine(DebugWindowMenuItem.IsChecked);
if (DebugWindowMenuItem.IsChecked == true) {
DebugWindow.Visibility = Visibility.Collapsed;
DebugWindowMenuItem.IsChecked = false;
} else {
DebugWindow.Visibility = Visibility.Visible;
DebugWindowMenuItem.IsChecked = true;
}
}
I'm clearly missing something because the XAML states that it's not checked but the Console output says it is everytime I click DebugWindowMenuItem.
Is there another parameter I'm missing?
UPDATE: Basically, don't set IsChecked. Treat a checkable MenuItem as a CheckBox.
Your problem is that you haven't thought about the inbuilt behaviour.
If you make a menuitem checkable like that.
What happens?
It acts like a checkbox.
What happens when you click a checkbox?
If you comment out your code so you have:
private void DebugWindow_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine(DebugWindowMenuItem.IsChecked);
}
When you click it, the menu item gets a tick appears and ischecked becomes true.
Click it again and it becomes false.
Don't set Ischecked.
How to prevent the special characters in datagridview in WPF using C#. Is there any any event for datagrid like KeyPress event.
if (!char.IsControl(e.Key))
{
Control editingControl = (Control)sender;
if (!Regex.IsMatch(editingControl.InputBindings + e.KeyChar, pattern))
e.Handled = true;
}
Am using this above code, but its througing error,any suggestion on this.
Is there any any event for datagrid like KeyPress event.
Try the PreviewTextInput event:
<DataGrid ... PreviewTextInput="dg_PreviewTextInput">
private void dg_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!Regex.IsMatch(e.Text, pattern))
e.Handled = true;
}
var regex = new Regex(#"[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]");
if (!regex.IsMatch(e.Text))
e.Handled = true;
This helped me, Thanks mm8...
Is there a way to get OnItemRightTapped event on ListView or GridView that works exactly like ItemClick, except obviously react only on right tap?
You can add an event handler for mouse down and then determine the click source in the code:
private void listView_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Right)
{
// Do what u need to do here
}
else
e.Handled = true;
}
A ListBox and a ContextMenu are created dynamicaly. The ListBox has some items.
How do I know the ListBoxItem Text that right mouse button clicked on?
private void Init2()
{
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItemOpen = new MenuItem();
menuItemOpen.Click += new RoutedEventHandler(menuItemOpen_Click);
contextMenu.Items.Add(menuItemOpen);
listBox1.ContextMenu = contextMenu;
}
void menuItemOpen_Click(object sender, RoutedEventArgs e)
{
//How do I know the listItem text that right mouse button clicked on?
}
When you right click, you actually also select. So that means you can just do:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
string selectedListBoxItemText = ((ListBoxItem)listBox1.SelectedItem).Content.ToString());
// do your thing
}
How can i disable a button until an item is selected in a listbox?
Initially you disable your button:
button1.Enabled = false;
Then you subscribe to the SelectedIndexChanged event of the listbox. Bellow is the handler:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}
You subscribe to the event from the Visual Studio IDE, or programatically like this:
listBox1.SelectedIndexChanged+=new EventHandler(listBox1_SelectedIndexChanged);
Since you mention winforms, one way is to set the property of the button IsEnabled=false in the property explorer. Then add an event for the listbox OnSelectionChanged. VS will automatically insert the code behind for you, then you can put this in the event handler:
button1.IsEnabled = listbox1.SelectedIndex > 0;
If you are using WPF you can do it the same way, but it would be better to use Databinding.