I have noticed in my WPF application a strange behavior. If I edit or remove a text in any text box and try to click afterwards on a canvas, the first MouseLeftButtonDown event is often swallowed. If I try a second time, it works.
For demonstrating the problem, I have created a minimalist demo.
XAML:
<Window x:Class="WPF_Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Click Demo" Height="300" Width="400">
<Grid>
<TextBox x:Name="textBox" Margin="10,10,10,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
<Canvas Margin="10,38,10,10" MouseLeftButtonDown="Click" Background="Black"/>
</Grid>
</Window>
C#:
using System.Windows;
using System.Windows.Input;
namespace WPF_Demo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Click(object sender, MouseButtonEventArgs e)
{
System.Diagnostics.Trace.WriteLine("Click!");
}
}
}
Edit:
Seems to be a problem of my Windows. I can reproduce it even with Explorer.
Related
Sorry if this has been asked before and I have spent about a week trying to find a similar question to point me in the right direction. I am teaching myself C# with WPF, XAML etc and am playing around with user controls. I made a simple app with a user control to load on top of other windows or user controls. The UC in question has two buttons and I need to get to the click events for each button in main window once the control is loaded. The main window has a button that loads the control.
Through some research I was able to find a solution from user SWilko (https://stackoverflow.com/a/28949666/10659981) but I can't figure it out for each button separately (click button a and show "clicked btn a", click button b and show "clicked button b"). I did try calling by sender using name and that will not work either. I feel like I am close with the help from the answer by SWilko but stuck.
Here is the code so far:
Basic main screen loading user control
<Window x:Class="UCBTN_TEST.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UCBTN_TEST"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="435">
<Grid>
<Button Content="Load Button" HorizontalAlignment="Left" Margin="18,23,0,0" VerticalAlignment="Top" Width="74" Click="Button_Click"/>
<Grid x:Name="GridLoad" HorizontalAlignment="Left" Height="300" Margin="120,23,0,0" VerticalAlignment="Top" Width="300" Background="#FFF1CBCB"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UCBTN_TEST
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
GridLoad.Children.Clear();
GridLoad.Children.Add(new WindowControl());
}
}
}
The button user control
<UserControl x:Name="UCMain" x:Class="UCBTN_TEST.Controls.ButtonControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UCBTN_TEST.Controls"
mc:Ignorable="d" d:DesignWidth="300" Height="40.333">
<Grid Background="#FFE7EEA7">
<Button x:Name="ButtonA" Content="Button A" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonA_Click" Background="Red"/>
<Button x:Name="ButtonB" Content="Button B" HorizontalAlignment="Left" Margin="215,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonA_Click" Background="Green"/>
</Grid>
</UserControl>
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UCBTN_TEST.Controls
{
/// <summary>
/// Interaction logic for ButtonControl.xaml
/// </summary>
public partial class ButtonControl : UserControl
{
public ButtonControl()
{
InitializeComponent();
}
private void ButtonA_Click(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs(ClickEvent1, this));
}
public static readonly RoutedEvent ClickEvent1 = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ButtonControl));
public event RoutedEventHandler Click
{
add { AddHandler(ClickEvent1, value); }
remove { RemoveHandler(ClickEvent1, value); }
}
}
}
A second user control which would ultimately have some other controls once the buttons work correctly. But the button UC will load on top, simple button features related to WindowControl.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UCBTN_TEST"
xmlns:Controls="clr-namespace:UCBTN_TEST.Controls" x:Class="UCBTN_TEST.WindowControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="#FFE7CFEE">
<Controls:ButtonControl HorizontalAlignment="Left" Height="37" VerticalAlignment="Top" Width="300" Click="Click1"/>
</Grid>
</UserControl>
I understand the behind code and why this is happening. My problem is that I need to have the buttons be unique in their events. I have tried calling by sender and name and that just kills the event all together.
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UCBTN_TEST.Controls;
namespace UCBTN_TEST
{
/// <summary>
/// Interaction logic for WindowControl.xaml
/// </summary>
public partial class WindowControl : UserControl
{
public WindowControl()
{
InitializeComponent();
}
private void Click1(object sender, RoutedEventArgs e)
{
MessageBox.Show("This triggers both");
}
}
}
I was going to add a bunch of comments but really this is kind of answering the question and there's a lot to explain.
You should look into MVVM and mostly be thinking in terms of binding commands rather than which button was clicked. There are exceptions to this. For example, if you were building an on screen keyboard. The reason this is different because it's purpose can be encapsulated. The user presses a button which has "A" in it. Whatever textbox is focussed should be sent the character "A". They press a button showing "B" and similarly "B" should be sent. That functionality can be encapsulated in the control.
As it is, you have two buttons.
You put them in a usercontrol and encapsulate them.
By doing this you created a boundary.
This then creates a complication - which was clicked?
The usercontrol is also not particularly re-use friendly. If you add two then there are two buttonA and two button B. You could potentially improve that with a custom event args on your custom routed event and a dependency property on your usercontrol. Pass some usercontrol identifier along with which button was pressed.
This would be an unusual way to work though. I've rarely seen Custom routed events used in commercial apps.
All in all I would suggest the usercontrol mainly adds complexity.
Say you wanted to have 20 sets of 2 buttons.
Or 20 sets of 5 radiobuttons for a set of multiple choice questions.
The way to do that sort of thing is to use an itemscontrol and template out the multiple controls. One template giving 2 buttons ( or a textblock question and 5 radiobuttons for answers ) per row.
A click event is already a routed event and would bubble to the window. You may as well remove your custom routed event and the handler out the usercontrol... and the usercontrol. Just handle click in the window.
Code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var btn = e.OriginalSource as Button;
if(btn == null)
{
return;
}
MessageBox.Show($"Button clicked was {btn.Tag}");
}
Markup:
ButtonBase.Click="Button_Click"
Title="MainWindow"
>
<Grid>
<StackPanel>
<Button x:Name="ButtonA" Content="Button A" Tag="A" Background="Red"/>
<Button x:Name="ButtonB" Content="Button B" Tag="B" Background="Green"/>
</StackPanel>
</Grid>
</Window>
I have text boxes that are getting URL inside, when you put the URL (long) in it, I want it to go down one row in order to see the last character of the URL.How can I achieve it instead
of changing the width size?
The TextBlock class features the TextBlock.TextTrimming Property, which enables users to add an ellipsis (...) at the end of text that is too long to be displayed in the TextBlock. If your TextBox is not being used for text input, then you can simply use a TextBlock control instead.
If you really need to use a TextBox, then unfortunately that has no such property. One alternative is to use a custom TextBox that does have this property. You can find an example of that in the WPF TextBox With Ellipsis page on CodeProject.
UPDATE >>>
As you have not shown any code, nobody can tell you what you did wrong. Either way, this is a simple issue that I'm sure that you can fix yourself. Add this to a different view somewhere else:
<TextBlock Text="123456789012345678901234567890123456789012345678901234567890"
Width="100" TextTrimming="WordEllipsis" />
Now you should be able to see the ellipsis at the end of the TextBlock. That's how simple it is. If you example is not working, then you have made it not work by adding something else.
Try scrolling the text box to the beginning of the text when focus lost (not sure how to do that with data binding):
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).ScrollToHome();
}
You can also create a Behavior to avoid direct event handling:
Add reference to System.Windows.Interactivity (installed with Expression Blend).
Add a Behavior class:
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace WpfApplication2
{
public class AutoScrollToHomeBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
AssociatedObject.LostFocus += (tb, args) =>
{
(tb as TextBox).ScrollToHome();
};
}
}
}
Attach a Behavior to your text box:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:e="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Vertical">
<TextBox HorizontalAlignment="Left" Height="23" Width="120">
<e:Interaction.Behaviors>
<local:AutoScrollToHomeBehavior />
</e:Interaction.Behaviors>
</TextBox>
<TextBox HorizontalAlignment="Left" Height="23" Width="120">
<e:Interaction.Behaviors>
<local:AutoScrollToHomeBehavior />
</e:Interaction.Behaviors>
</TextBox>
</StackPanel>
</Grid>
</Window>
I have Page, and it loses focus when I tap on an empty part of it. I tried putting a Border as the background, but that loses focus too when I tap it. Why does this happen?
What I really need to do is disable a WebView when the user opens the AppBar or the Settings Charm
Some example code to demonstrate the problem (watch the output window):
XAML:
<Page
x:Name="Pagey"
x:Class="FocusTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:FocusTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" GotFocus="Focus" LostFocus="LoseFocus">
<Grid x:Name="RootGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" GotFocus="Focus" LostFocus="LoseFocus">
<StackPanel>
<Button x:Name="Clicky" Content="Clicky" GotFocus="Focus" LostFocus="LoseFocus" HorizontalAlignment="Center"></Button>
<Border x:Name="Border" Width="100" Height="100" Background="Red" GotFocus="Focus" LostFocus="LoseFocus"></Border>
<Button x:Name="Clicky2" Content="Clicky2" GotFocus="Focus" LostFocus="LoseFocus" HorizontalAlignment="Center"></Button>
</StackPanel>
</Grid>
</Page>
Code behind:
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace FocusTest
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
void Focus(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Focus({0})", (sender as FrameworkElement).Name);
}
void LoseFocus(object sender, RoutedEventArgs e)
{
Debug.WriteLine("LoseFocus({0})", (sender as FrameworkElement).Name);
}
}
}
Look like your Border is inside the RootGrid So that every time when you tap Border's tap event will occur and LostFocus Event fired. Better you can set LostFocus event on the same RootGrid. Then it may work fine. Please try it.
Thanks.
I'm having issues with dynamically handling the MouseUp event. Test code with issue:
WPF:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="Button1" Content="Button1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<Button x:Name="Button2" Content="Button2" HorizontalAlignment="Left" Margin="10,37,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
C# behind:
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Button2.MouseLeftButtonUp += something;
}
private void something(object sender, MouseEventArgs e)
{
MessageBox.Show("TEST");
Button2.MouseLeftButtonUp -= something;
}
}
}
Now, I want the MessageBox with the text "TEST" to show only the first time after I click Button2, after I've clicked Button1. It doesn't show up. The same code works with the Click event instead, but I need MouseUp to get the mouse position. I've confirmed that the first function fires, but the second doesn't no matter what I try. Help?
See the Remarks section in ButtonBase.Click:
The ButtonBase marks the MouseLeftButtonDown event as handled in the
OnMouseLeftButtonDown method and raises the Click event. Hence, the
OnMouseLeftButtonDown event will never occur for a control that
inherits from ButtonBase. Instead, attach an event handler to the
PreviewMouseLeftButtonDown event, or call AddHandler(RoutedEvent,
Delegate, Boolean) with handledEventsToo set to true.
Since MouseLeftButtonDown is handled internally, you will also get no
MouseLeftButtonUp event. You could however use the PreviewMouseLeftButtonUp event instead.
I'm creating a UserControl consisting of a TextBox and a ListView. I want keyboard focus to remain with the TextBox as long as the control has keyboard focus (selection changes in the ListView shouldn't remove keyboard focus from the TextBox).
I've tried catching GotKeyboardFocus in the ListView and passing keyboard focus back to the TextBox using Keyboard.Focus(), but this seems to cancel any selection operation in the ListView. The below code shows the problem. Does anyone know how to achieve this functionality?
<Window x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBox x:Name="TextBox1" />
<ListView x:Name="ListBox1" Keyboard.GotKeyboardFocus="ListBox1_GotKeyboardFocus">
<ListViewItem Content="Able" />
<ListViewItem Content="Baker" />
<ListViewItem Content="Charlie" />
</ListView>
</StackPanel>
</Window>
using System.Windows;
using System.Windows.Input;
namespace WpfApplication5
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void ListBox1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
Keyboard.Focus(TextBox1);
}
}
}
Instead, have you considered just capturing keystrokes and putting those keystrokes into your TextBox?
<Window PreviewKeyDown="Window_PreviewKeyDown" >
<Grid>
<TextBox x:Name="TextBox1" />
<ListBox />
</Grid>
</Window>
Then in your window's code-behind:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
TextBox1.Text += e.Key.ToString();
}
You'll have to do extra work for anything like special characters (backspace, etc), and obviously a Key handler for your "Enter" or "Post" operation, but it gives you the ability to just free-form type while the Window has focus and to properly handle the keystrokes as necessary.
It looks like it's possible to change focus in the MouseUp event. I think if you do it too early, like in the GotKeyboardFocus event, you'll steal focus before the listview can handle the event and select the chosen item.
<StackPanel>
<TextBox x:Name="TextBox1" />
<ListView x:Name="ListBox1" MouseUp="ListBox1_MouseUp">
<ListViewItem Content="Able" />
<ListViewItem Content="Baker" />
<ListViewItem Content="Charlie" />
</ListView>
</StackPanel>
private void ListBox1_MouseUp(object sender, MouseButtonEventArgs e)
{
TextBox1.Focus();
}
If you are calling your WPF window from a WinForm you must use this:
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(wpfWindow);
wpfWindow.show();
from the MSDN documentation.
Thats how I solved my keyboard problem.
IceX
This is a hack, but what if instead of listening to the GotKeyboardFocus event, you listen to the SelectionChanged event on the ListBox?
Put Focusable=false on your ListView.
Ok, this was driving me crazy. Even though set focus to UserControl every time lost focus, still couldn't get my command hot keys to work. All I had to do was to set the property Focusable to true, and voilĂ , it's working!