WPF Behavior MouseDown Doesn't Fire - c#

I'm learning Microsoft.Expression.Interactions.WPF. I'm attempting to attach a behavior to a grid's MouseDown event. The OnAttached method gets called, but my behavior never receives the MouseDown event.
XAML
<Window x:Class="SequenceDiagramViewer.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:SequenceDiagramViewer"
xmlns:ineractivity="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="Sequence Diagram Viewer" Height="450" Width="800">
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
<Grid>
<ineractivity:Interaction.Behaviors>
<local:MouseClick />
</ineractivity:Interaction.Behaviors>
</Grid>
</Window>
C#
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
namespace SequenceDiagramViewer
{
public class MouseClick : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
Console.WriteLine("OnAttached called");
AssociatedObject.MouseDown += AssociatedObject_MouseDown;
}
private void AssociatedObject_MouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("MouseDown called");
}
protected override void OnDetaching()
{
Console.WriteLine("OnDetaching called");
AssociatedObject.MouseDown -= AssociatedObject_MouseDown;
}
}
}
In my console output, I only get OnAttached called. In addition, when I put a breakpoint in AssociatedObject_MouseDown, it never gets hit, but OnAttached does. How do I get the event to go to my behavior?

My grid background was transparent. As soon as I changed my grid XAML to this:
<Grid Background="White">
<interactions:Interaction.Behaviors>
<local:MouseClick />
</interactions:Interaction.Behaviors>
</Grid>
I then started getting mouse down events.

Related

Navigating with "Enter" focuses Button on other page

I have one "login page" and one "content page". The login page consists of a PasswordBox and a Button. When the button is clicked or the Enter-key is pressed in the PasswordBox, I want the program to navigate to the next page. Navigating with the button works fine, but when the page is changed due to the pressed enter key, the Button on the next page is focused. How can I disable/avoid this behaviour?
"Focused" button:
MainPage.xaml
<Page
x:Class="TabNavigation.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TabNavigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<StackPanel>
<PasswordBox x:Name="textBox" KeyDown="HandleTextBox_KeyDown"/>
<Button x:Name="loginButton" Content="Einloggen" Click="HandleLoginButton_Click"/>
</StackPanel>
</Grid>
</Page>
MainPage.xaml.cs
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace TabNavigation {
public sealed partial class MainPage : Page {
public MainPage() {
this.InitializeComponent();
}
private void HandleTextBox_KeyDown(object sender, KeyRoutedEventArgs e) {
if(e.Key == Windows.System.VirtualKey.Enter) {
this.Frame.Navigate(typeof(ContentPage));
}
}
private void HandleLoginButton_Click(object sender, RoutedEventArgs e) {
this.Frame.Navigate(typeof(ContentPage));
}
}
}
ContentPage.xaml
<Page
x:Class="TabNavigation.ContentPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TabNavigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<StackPanel>
<Button>Test</Button>
</StackPanel>
</Grid>
</Page>
You can cancel the focus in GettingFocus:
<Button GettingFocus="Button_GettingFocus">Test</Button>
private void Button_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
args.Cancel = true;
}
Old answer:
You have few options:
Disable focus on button with IsTabStop=false
Change TabIndex to larger integer than other elements.
Set focus from your code to something else: otherControl.Focus(FocusState.Programmatic);

Viewmodel doesn't exist in namespace

I'm sorry if this is a stupid question or doesn't even fall under what I'm asking, but I'm new to WPF and I can't seem to get the hang of it. Right now I'm doing something akin to https://www.c-sharpcorner.com/article/use-inotifypropertychanged-interface-in-wpf-mvvm/ and I've run into a problem. When I try to execute my code:
namespace DPS_Calculator_Prototype
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow() {
InitializeComponent();
}
}
public class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChange(string propertyName) {
PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}
public class Calculator: NotifyPropertyChanged
{
private string _damage;
public string Damage {
get { return _damage; }
set {
_damage = value;
RaisePropertyChange("Damage");
}
}
}
namespace UseOf_INotifyPropertyChanged
{
public class MainWindowViewModel : Calculator
{
public MainWindowViewModel() {
Damage = "7";
}
}
}
}
I get the error that "The type or namespace name 'ViewModel' does not exist in the namespace 'DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged' (are you missing an assembly reference?)"
and
"The name "MainWindowViewModel" does not exist in the namespace 'clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel'."
and
"The type 'VM:MainWindowViewModel' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built."
I get the first error twice, once in MainWindow.g.cs and another in MainWindow.xaml. The other two are all in MainWindow.XAML If anyone can tell me what I'm doing wrong then that would be great. Here's the XAML file:
<Window x:Class="DPS_Calculator_Prototype.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:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel"
xmlns:local="clr-namespace:DPS_Calculator_Prototype"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<VM:MainWindowViewModel x:Name="VMMainWindow">
</VM:MainWindowViewModel>
</Window.DataContext>
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap"
Text="{Binding Damage}" VerticalAlignment="Top" Width="120"
Margin="78,28,0,0" TextChanged="TextBox_TextChanged"/>
</Grid>
</Window>
That's one of the worst "copy and paste" jobs I've ever seen guy...
I don't know where to start.
Just to run the application you MUST:
change the namespace VM as follows;
xmlns:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged"
DPSCalculatorPrototype.ViewModel doesn't exist.
The TextBox_TextChanged doesn't exist inside the codebehind of the window. You must add the method
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
//Do your stuffs
}
in the MainWindow class.
In order to avoid headaches to you or who is reading your code, you
SHOULD
use one .cs file for each class.
Avoid to nest namespaces inside the same .cs file and create a folder tree that replicates the namespace structure. In your snippet just create a UseOf_INotifyPropertyChanged folder inthe root and create the MainWindowViewModel class inside that.
The purpose of a namespace must be clear reading the code. Create a DPS_Calculator_Prototype.ViewModels and put all application viewmodel inside it.
I just tried using different namespaces and keep them more simple. And it works.
DPSCalculatorPrototype.ViewModel
namespace DPSCalculatorPrototype.ViewModel
{
public class MainWindowViewModel : Calculator
{
public MainWindowViewModel()
{
Damage = "7";
}
}
}
DPSCalculatorPrototype
namespace DPSCalculatorPrototype
{
public class Calculator : NotifyPropertyChanged
{
private string _damage;
public string Damage
{
get { return _damage; }
set
{
_damage = value;
RaisePropertyChange("Damage");
}
}
}
public class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChange(string propertyName)
{
PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}
}
MainWindow.xaml
<Window x:Class="DPSCalculatorPrototype.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:VM="clr-namespace:DPSCalculatorPrototype.ViewModel"
xmlns:local="clr-namespace:DPSCalculatorPrototype"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<VM:MainWindowViewModel x:Name="VMMainWindow"></VM:MainWindowViewModel>
</Window.DataContext>
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="{Binding Damage}" VerticalAlignment="Top" Width="120" Margin="78,28,0,0" TextChanged="TextBox_TextChanged" />
</Grid>
</Window>
MainWindow.xaml.cs
namespace DPSCalculatorPrototype
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
}
}
}
The reason you are seeing these errors is that WPF is looking in the namespaces mentioned and cannot seem to find what you're looking for. If you take a look at your XAML code you can see the line that says:
xmlns:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel"
This is what is declaring to use the namespace, so we need to point it to the correct area. Change your XAML to look like the following:
<Window x:Class="DPS_Calculator_Prototype.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:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged"
xmlns:local="clr-namespace:DPS_Calculator_Prototype"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<VM:MainWindowViewModel x:Name="VMMainWindow">
</VM:MainWindowViewModel>
</Window.DataContext>
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="
{Binding Damage}" VerticalAlignment="Top" Width="120" Margin="78,28,0,0"
TextChanged="TextBox_TextChanged"/>
</Grid>
</Window>
You were getting these errors because you had "ViewModel" in the namespace declaration, and this namespace doesn't exist, and as such, nothing exists in it.

MainWindow gets closed when closing ChildWindow

I am developing a wpf application for the surface pro in tablet mode. I am coding on a different computer and there everything works fine. When testing the application on the surface pro something weird happens:
During the application a second window gets opened, in order to modify some settings. This window includes a button in order to close the window (see code below). When this button is clicked on the surface pro the whole application gets closed (including the main window). On the computer where I am writing the code this never happens, so in my opinion the problem lies with the hardware.
Thank you for your help!
Edit 21/03/2018
So as discussed in the comments I have tried several things and the problem remains. I have now deleted most of the application code such that only the essential feature remain and the problem still hasn't disappeared. I will post the code below:
App.xaml
<Application x:Class="TestNamespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestNamespace"
Startup="ApplicationStartup">
<Application.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
</Application.Resources>
</Application>
App.xaml.cs
public partial class App : Application
{
public MainWindow window = new MainWindow();
public Monitoring monitoring;
private void ApplicationStartup(object sender, StartupEventArgs e)
{
window.Show();
monitoring = new Monitoring();
window.Content = monitoring.gui;
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine("App1: There has been an unhandled exception");
throw new NotImplementedException();
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine("App2: There has been an unhandled exception");
throw new NotImplementedException();
}
}
GUI.xaml
<UserControl x:Class="TestNamespace.GUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:oxy="http://oxyplot.org/wpf"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:mi="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:local="clr-namespace:TestNamespace">
<Grid Background="Black">
<Button HorizontalAlignment="Center" VerticalAlignment="Center"
Width="300" Height="300"
Content="NewWindow" Click="SettingsButton_Click"/>
</Grid>
</UserControl>
GUI.xaml.cs
public partial class GUI : UserControl
{
private Monitoring monitoring;
public GUI(Monitoring monitoring)
{
this.monitoring = monitoring;
InitializeComponent();
this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine(" GUI: There has been an unhandled exception");
throw new NotImplementedException();
}
private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
monitoring.settingsWindow = new testWindow();
monitoring.settingsWindow.Show();
}
}
Monitoring.cs
public class Monitoring
{
private App currentApp = (App)Application.Current;
public GUI gui;
public Window settingsWindow;
public Monitoring()
{
gui = new GUI(this);
}
}
TestWindow.xaml
<Window x:Class="TestNamespace.testWindow"
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:TestNamespace"
mc:Ignorable="d"
Title="testWindow"
ResizeMode="NoResize" WindowState="Maximized" WindowStyle="None">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="245,200,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
</Window>
TestWindow.xaml.cs
public partial class testWindow : Window
{
public testWindow()
{
InitializeComponent();
this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine("TestWindow: There has been an unhandled exception");
throw new NotImplementedException();
}
private void button_Click(object sender, RoutedEventArgs e)
{
e.Handled = true;
this.Close();
}
}
MainWindow.xaml
<Window x:Class="TestNamespace.MainWindow"
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:TestNamespace"
mc:Ignorable="d"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowState="Maximized" WindowStyle="None">
<Grid Background="White">
<TextBox x:Name="textBox" HorizontalAlignment="Center" Height="47" TextWrapping="Wrap" Text="LOADING..." VerticalAlignment="Center" Width="198" FontWeight="Bold" FontSize="36" BorderBrush="{x:Null}" />
</Grid>
</Window>
MainWindow.xaml.cs
partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
This is the complete code for the Minimal, Complete, and Verifiable example. This code still produces the error on the tablet. Thank you for your help!
I finally managed to solve the problem! Instead of creating and showing a new window, I created a new UserControl (TestWindow) and changed the content of the mainWindow. Upon closing the TestWindow, the normal GUI is loaded as Content of the MainWindow and the TestWindow is set to null.
It might not be the best solution, but it works.
EDIT 1
A few weeks later I stumbled upon the same problem once again. I found a solution written by Erti-Chris Eelmaa that finally solved the problem. I needed to change the Application.ShutdownMode such that the application only shuts down when Application.Shutdown(); is explicitly called.
Application.ShutdownMode = ShutdownMode.OnExplicitShutdown;
I hope this helps!

CallMethodAction binding to Button fails

There are no errors. I am trying to learn MVVM. The setup is simple. I get no output when clicking on the button. Xaml should be ok since I have produced interactions part in Blend by dragging behavior to the button. Note: My intention to use methods, but not command, since commands cover only click, but not for example DoubleClick.
<Window
x:Class="MVVM_1.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:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:MVVM_1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d"
>
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<Button x:Name="button"
Width="75"
Margin="198,168,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="TestMethod">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown" SourceName="button">
<ei:CallMethodAction MethodName="MethodTesting"
TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
using System.Windows;
namespace MVVM_1
{
public class ViewModel
{
public static void MethodTesting()
{
MessageBox.Show("Success!");
}
}
}
You need to use a command to bind the control to the click event.
MVVM - Commands, RelayCommands and EventToCommand
Instead of:
public static void MethodTesting()
{
MessageBox.Show("Success!");
}
Use this example:
public ICommand ButtonClickCommand
{
get { return new DelegateCommand<object>(FuncToCall, FuncToEvaluate);}
}
private void FuncToCall(object context)
{
//this is called when the button is clicked
MessageBox.Show("Success!");
}
private bool FuncToEvaluate(object context)
{
//this is called to evaluate whether FuncToCall can be called
//for example you can return true or false based on some validation logic
return true;
}
Also under this How do you create an OnClick command in WPF MVVM with a programmatically created button? - is another great example.

WPF event handler in Visual Studio 2008?

When i went to add an event to the button which i dragged and dropped from toolbox to the window, the event handler on the properties window was not visible.. because of this reason, i added the event manually (by typing). but after when i built it and pressed F5, the button was not firing the event.
here is a little example that must be work ;-)
<Window x:Class="WpfStackOverflowSpielWiese.Window8"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window8"
Height="300"
Width="300">
<Grid>
<Button Click="Button_Click" />
</Grid>
</Window>
code behind
using System.Windows;
namespace WpfStackOverflowSpielWiese
{
/// <summary>
/// Interaction logic for Window8.xaml
/// </summary>
public partial class Window8 : Window
{
public Window8() {
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
// do something....
}
}
}
hope that helps you...

Categories

Resources