Touch event in wpf interfering with other events - wpf

Help!
Been wrestling with a problem using Touch events in a wpf application I am developing.
Let me set the scene...
I have a main window (fmMainWindow) that has a Textbox on it. The window is coded to be centred to the screen when open. On the TouchUp event of the Textbox, I have code that opens up another window (fmKeypad_Numeric) using the syntax ShowDialog. This window is also centred. It is a numeric keypad with numbered buttons on it.
My problem is that the Touch Event used on the main window is having an affect on the Click events of the buttons on the window it opens.
It looks like wherever on the screen the Touch Event occurred, that the button on the next window opened at the same position seems to initially have some sort of 'focus' (it appears as if the mouse is hovered over it).
If you click the button that initially has the 'focus', the click event fires as expected.
If however you first click one of the other buttons that doesn't have the 'focus' its click event does not fire. It takes a second click for it to fire??
I have used Snoop to see how the events fire and when you click one of the other buttons that doesn't initially have the 'focus', Events related to the button that has the 'focus' fire??
Anyone got any ideas? I've spent days on this one.
Code for the two windows is below
Many thanks in advance,
Rhyd.
fmMainWindow
<Window x:Class="fmMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" SizeToContent="WidthAndHeight" ResizeMode="NoResize" FontFamily="Century Gothic">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
<TextBox Name="tbTest" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="180,10,10,0" Width="150" Height="50" BorderBrush="#FF1B378B" Background="#FFEC1212"/>
</Grid>
Public Class fmMainWindow
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen
End Sub
Private Sub tbTest_TouchUp(sender As Object, e As TouchEventArgs) Handles tbTest.TouchUp
e.Handled = True
Dim myKeypad As New fmKeypad_Numeric()
myKeypad.ShowDialog()
myKeypad.Close()
End Sub
End Class
fmKeypad_Numeric
<Window x:Class="fmKeypad_Numeric"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Keypad_Numeric" SizeToContent="WidthAndHeight" ResizeMode="NoResize" FontFamily="Century Gothic" FontSize="30" Left="10">
<Window.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Height" Value="70" />
</Style>
</Window.Resources>
<Grid Name ="grKeypad" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBox Name="tbNumber" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalContentAlignment="Right" Text="" Width="230" IsReadOnly="True" Margin="10,10,0,0"/>
<Button Name="bu7" Content="7" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,70,0,0" Width="70" />
<Button Name="bu8" Content="8" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="90,70,0,0" Width="70" />
<Button Name="bu9" Content="9" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,70,10,0" Width="70" />
<Button Name="bu4" Content="4" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,150,0,0" Width="70" />
<Button Name="bu5" Content="5" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="90,150,0,0" Width="70" />
<Button Name="bu6" Content="6" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,150,10,0" Width="70" />
<Button Name="bu1" Content="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,230,0,0" Width="70" />
<Button Name="bu2" Content="2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="90,230,0,0" Width="70" />
<Button Name="bu3" Content="3" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,230,10,0" Width="70" />
<Button Name="bu0" Content="0" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,310,0,0" Width="70" />
</Grid>
Public Class fmKeypad_Numeric
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen
End Sub
Private Sub Button_Click(sender As Object, e As RoutedEventArgs) Handles bu0.Click, bu1.Click, bu2.Click, bu3.Click, bu4.Click, bu5.Click, bu6.Click, bu7.Click, bu8.Click, bu9.Click
Dim myButtonName As String
Dim myTextToAdd As String = ""
myButtonName = CType(sender, Button).Name
myTextToAdd = Replace(myButtonName, "bu", "")
Me.tbNumber.Text = Me.tbNumber.Text & myTextToAdd
End Sub
End Class

Related

Set binding on button tooltip with a declared instance of a user control as source

I've added a user control (with labels and textboxes) to my WPF project shown here:
<UserControl x:Class="MyProject.myControl"
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"
mc:Ignorable="d"
d:DesignHeight="384" Width="543">
<Grid x:Name="_LabelServerURL">
<Label x:Name="_LabelServerUrl" Content="Server Url:" HorizontalAlignment="Left" Margin="60,21,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<TextBox x:Name="_TextboxUrl" HorizontalAlignment="Left" Height="23" Margin="158,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="235"/>
<Label x:Name="_LabelURLExample" Content="(https://promodel.com)" HorizontalAlignment="Left" Margin="398,21,0,0" VerticalAlignment="Top" Width="139"/>
</Grid>
</UserControl>
and have created an instance of that user control in my main window code behind.
public partial class MainWindow : Window
{
private myControl _settings = new myControl();
public MainWindow()
{
InitializeComponent();
AddStackPanelChildren();
_ButtonEPSSettings.ToolTip = _epsSettings;
//SetButtonTootips();
}
In my main window I have a list of buttons, and I want to set their tooltip property (or bind the tooltip property) to the instance of my user control so that the tooltip dynamically updates when I add values to the textboxes in my user control. So far in my main window xaml I've been able to show my user control as the tooltip shown here:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls ="clr-namespace:MyProject" <!--Declare project namespace-->
Title="EPS Configuration Manager" Height="415" Width="766">
<Grid>
<Button x:Name="_ButtonSettings"
Content="EPS Settings"
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="199"
HorizontalContentAlignment="Left"
BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="_ButtonSettings_Click"
>
<Button.ToolTip>
<ToolTip>
<StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
<Controls:myControl/>
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
I am still not clear on setting that tooltip as the instance of my user control as declared in my main window code behind. Any help will go a long way. Thanks.
If you set tooltips in backgroud, don't use these code:
<Button.ToolTip>
<ToolTip>
<StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
<Controls:myControl/>
</StackPanel>
</ToolTip>
</Button.ToolTip>
If you want the tooltip dynamically updates:
private myControl mytooltip = new myControl();
public MainWindow()
{
InitializeComponent();
AddStackPanelChildren();
mytooltip._TextboxUrl.Text = "Hello";
_ButtonEPSSettings.ToolTip = mytooltip;
//SetButtonTootips();
}

Tabitem Header's button name from code behind

I have a TabControl, and I am adding TabItems dynamically to it . I have a predefined DataTemplate in the TabControl's resources. So, there is a TextBox and a Button in that template. I bind that template with the TabItem dynamically and add that tab item to tab control.
Here is the code :
<Window x:Class="Menu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="tab" Height="465" Width="869">
<Window.Resources>
</Window.Resources>
<Grid>
<TabControl Name="tab" Height="200" HorizontalAlignment="Stretch" verticalAlignment="Top" >
<TabControl.Resources >
<DataTemplate x:Key="TabHead" DataType="TabItem" >
<WrapPanel >
<Label Name="lbl1" Content="tab1" ></Label>
<Button Name="btn1" Content="X"></Button>
</WrapPanel>
</DataTemplate>
</TabControl.Resources>
</TabControl>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="55,246,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
this is XAML code and here is VB.NET code :
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim tabi As New TabItem
tabi.HeaderTemplate = TryCast(tab.Resources("TabHead"), DataTemplate)
tab.Items.Add(tabi)
End Sub
Now I want to have a unique name for each button which is in the DataTemplate of the TabControl's resources.
How can I set unique names for each Button in my DataTemplate?

Binding example

I have been trying for weeks to find a decent article that explains WPF Binding simply and with a working example that actually explains what is happening. Yes there are lots of examples out there but lots of those refer to older versions of everything involved.
I am trying to write an application, that is not web based, uses WPF and VB on VS2012.
In this bit of code i am updating a screen field directly, but i want to learn how to update a variable in memory and it update on the screen automatically, how do i get from here to there - In stages with explanations
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test Bind"
Height="299" Width="500" WindowStartupLocation="CenterScreen">
<DockPanel HorizontalAlignment="Stretch" LastChildFill="False" VerticalAlignment="Stretch">
<DockPanel DockPanel.Dock="Top" Height="64">
<Button Content="Button" Height="64" VerticalAlignment="Top" Width="75" Click="Button_Click" />
<TextBox Height="64" Width="120" Text="TextBox1" x:Name="clickcount" />
<TextBox Height="64" Width="120" x:Name="textBox2" Text="{Binding Test, Mode=Default}" />
</DockPanel>
</DockPanel>
</Window>
Class MainWindow
Public Shared clcount As Integer = 0
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
clcount = clcount + 1
Me.clickcount.Text = clcount.ToString
End Sub
End Class

Display VB.NET WPF window

Sorry for this extremely amateur question, but I cannot make this work. I want to make a custom font dialog window (just for the heck of learning how it would be done) and from what I have found using Google, I should create an instance of the window I want to show and then call the Show() or ShowDialog() methods. However the intellisense popup does not show such methods as available and indeed the code does not compile and complains that those methods don't exist. Is there something really simple I am missing or am I just way off ?
Imports System.IO
Class MainWindow
Public font_dialog As Window1 = New Window1
// ... Removed code that was not pertinent
Private Sub menu_font_Click(sender As System.Object, e As _
System.Windows.RoutedEventArgs) Handles menu_font.Click
// does not compile
font_dialog.Show()
End Sub
End Class
Here is the exact error message:
Error 1 'Show' is not a member of
'WpfApplication1.Window1'. C:\Users\notmyrealusername\documents\visual studio
2010\Projects\WpfApplication2\WpfApplication2\MainWindow.xaml.vb 24 9 WpfApplication2
XAML for Window1:
<UserControl x:Class="Window1"
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"
mc:Ignorable="d" Height="453" Width="600" DataContext="{Binding}">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="575*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TabControl Height="429" HorizontalAlignment="Left" Margin="12,12,0,0" Name="TabControl1" VerticalAlignment="Top" Width="576" Grid.ColumnSpan="2">
<TabItem Header="Paramètres généraux" Name="TabItem1">
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="53,14,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="213" />
<Label Content="Police" Height="28" HorizontalAlignment="Left" Margin="6,14,0,0" Name="Label1" VerticalAlignment="Top" />
<Label Content="Styles" Height="28" HorizontalAlignment="Left" Margin="6,43,0,0" Name="Label2" VerticalAlignment="Top" />
<ListBox Height="100" HorizontalAlignment="Left" Margin="53,43,0,0" Name="ListBox1" VerticalAlignment="Top" Width="213" SelectionMode="Multiple" />
</Grid>
</TabItem>
</TabControl>
</Grid>
</UserControl>
You must make Window1 inherit from Window, which has the Show method. In Visual Studio, you can right click the project you want to add the window to and click Add -> Window.
'Show' is not a member of 'WpfApplication1.Window1'.
That mans your Window1 is not a (valid) Window ...
Post the first lines of the XAML and the code behind.
Also, you probably want to call ShowDialog(), but that is a separate issue.

Object Reference not set error while trying to set the visiblity of grid

I was trying to set the visiblity of a Grid from code behind.
grdStopTimeOut.Visibility = Windows.Visibility.Visible
I have declared this grid in XAML and have set the visiblity to Visible.
<Grid Name="grdTimeTStopCondition" Visibility="Hidden" Margin="0,29,0,-6">
Somehow when the application runs the grid is coming as nothing and the exceptions is thrown.
Anybody have any idea why it is happening??
XAML File
<Page x:Class="Page1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Name="cmbStopConditions" VerticalAlignment="Top" Width="86" SelectedIndex="0">
<ComboBoxItem Content="Expression"></ComboBoxItem>
<ComboBoxItem Content="Manual"></ComboBoxItem>
</ComboBox>
<Grid Name="grdStopTimeOut" Visibility="Visible" >
<Label Content="Timeout" Height="28" HorizontalAlignment="Left" Name="lblTimeout_stopcond" VerticalAlignment="Top" Margin="0,29,0,0" />
<TextBox Height="23" HorizontalAlignment="Left" Name="txtStopTimeout" VerticalAlignment="Top" Width="30" Margin="60,29,0,0" />
<Label Content="secs" Height="28" HorizontalAlignment="Left" Name="lblTimeoutSec_stopCond" VerticalAlignment="Top" Width="39" Margin="105,24,0,0" />
</Grid>
</Grid>
</Page>
CodeBehind
Class Page1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub cmbStopConditions_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cmbStopConditions.SelectionChanged
Dim item As ComboBoxItem = TryCast(cmbStopConditions.SelectedItem, ComboBoxItem)
If item IsNot Nothing Then
If Convert.ToString(item.Content) = "Expression" Then
grdStopTimeOut.Visibility = Windows.Visibility.Visible
ElseIf Convert.ToString(item.Content) = "Manual" Then
grdStopTimeOut.Visibility = Windows.Visibility.Hidden
End If
End If
End Sub
End Class
regards,
SKB
Edit: The handler can occur before the grid is initialized, so a null-check is in order.
Are you calling this before InitializeComponent? That for one would explain the lack of a reference because all the fields are hooked up with the named controls in that method.

Resources