detect multitouch while pressed c# - c#

I want to detect a touch event and do something while my finger is touching down.
What is the event I need?
I tried Manipulation*, Pointer*, Touch*, Stylus*, Holding*, etc...
If you have a sample code, better.
The most accurate code I have is this. But only works with finger moving.
<Window x:Class="WpfApplication2.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>
<Rectangle Name="Rect1" IsManipulationEnabled="True" Fill="Blue" HorizontalAlignment="Center" Height="100" VerticalAlignment="Center" Width="100" Margin="108,50,309,169" ManipulationDelta="Rect1_ManipulationDelta" />
<Rectangle Name="Rect2" IsManipulationEnabled="True" Fill="Blue" HorizontalAlignment="Center" Height="100" VerticalAlignment="Center" Width="100" Margin="338,50,79,169" ManipulationDelta="Rect2_ManipulationDelta" />
<TextBox Name="Text1" Text="0" HorizontalAlignment="Left" Height="23" Margin="108,267,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="100"/>
<TextBox Name="Text2" Text="0" HorizontalAlignment="Left" Height="23" Margin="338,267,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="100"/>
</Grid>
namespace WpfApplication2
{
public partial class MainWindow : System.Windows.Window
{
System.Windows.Input.ManipulationModes currentMode = System.Windows.Input.ManipulationModes.All;
public MainWindow()
{
InitializeComponent();
}
private void Rect1_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
int variable = 0;
variable = Convert.ToInt32(Text1.Text);
variable++;
Text1.Text = variable.ToString();
}
private void Rect1_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
int variable = 0;
variable = Convert.ToInt32(Text1.Text);
variable++;
Text1.Text = variable.ToString();
}
private void Rect2_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
int variable = 0;
variable = Convert.ToInt32(Text2.Text);
variable++;
Text2.Text = variable.ToString();
}
private void Rect2_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
int variable = 0;
variable = Convert.ToInt32(Text2.Text);
variable++;
Text2.Text = variable.ToString();
}
}
}

Touch* events will give you just touch. Pointer* will give you touch and mouse and stylus.
Use *Down to know when a touch goes down and *Up to know when it goes up. If you want to do something repeatedly while the touch is down, create a DispatcherTimer on the *Down and stop it on the *Up

UIElement/ContentElement.TouchDown is the event you need to accomplish what you are after.
Example:
<Rectangle Name="Rect1" IsManipulationEnabled="True" Fill="Blue" HorizontalAlignment="Center" Height="100" VerticalAlignment="Center" Width="100" Margin="108,50,309,169" TouchDown="Rect1_TouchDown" />
private void Rect1_TouchDown(object sender, TouchEventArgs e)
{
// Do stuff
}
For further information, refer to:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.touchdown(v=vs.110).aspx and
http://msdn.microsoft.com/en-us/library/ms754010(v=vs.110).aspx

Related

My first project using C# & WPF - how to set a variable from .xaml.cs to appear in the textbox?

I'm trying to build a calculator with C# and WPF.
The purpose is to learn and test new things.
Here's the code:
XAML
<Window x:Class="calculator.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:calculator"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<Grid>
<Button Content="9" HorizontalAlignment="Left" Margin="295,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click9"/>
<Button Content="8" HorizontalAlignment="Left" Margin="166,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click8"/>
<Button Content="7" HorizontalAlignment="Left" Margin="37,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click7"/>
<Button Content="6" HorizontalAlignment="Left" Margin="295,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click6"/>
<Button Content="5" HorizontalAlignment="Left" Margin="166,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click5"/>
<Button Content="4" HorizontalAlignment="Left" Margin="37,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click4"/>
<Button Content="3" HorizontalAlignment="Left" Margin="295,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click3"/>
<Button Content="2" HorizontalAlignment="Left" Margin="166,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click2"/>
<Button Content="1" HorizontalAlignment="Left" Margin="37,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click1"/>
<Button Content="0" HorizontalAlignment="Left" Margin="37,316,0,0" VerticalAlignment="Top" Width="321" FontFamily="Arial" Click="Button_Click0"/>
<TextBox HorizontalAlignment="Left" Height="35" Margin="37,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="321" FontFamily="Arial" FontSize="20" TextAlignment="Right"/>
<Button Content="Calculate!" HorizontalAlignment="Left" Margin="154,10,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="+" HorizontalAlignment="Left" Margin="37,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Add"/>
<Button Content="/" HorizontalAlignment="Left" Margin="290,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Multiply"/>
<Button Content="*" HorizontalAlignment="Left" Margin="206,125,0,0" VerticalAlignment="Top" Width="69" Height="57" Click="Button_Click_Divide"/>
<Button Content="-" HorizontalAlignment="Left" Margin="122,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Subtract"/>
</Grid>
</Window>
XAML.CS
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 calculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<int> lista = new List<int>();
string number = "";
int res = 0;
private void Button_Click_Add(object sender, RoutedEventArgs e)
{
int numberCreated;
try
{
numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if(lista.Count != 0)
{
foreach(var element in lista)
{
res += element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Subtract(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch(Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res -= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Multiply(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res *= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Divide(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res /= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click9(object sender, RoutedEventArgs e)
{
number += "9";
}
private void Button_Click8(object sender, RoutedEventArgs e)
{
number += "8";
}
private void Button_Click7(object sender, RoutedEventArgs e)
{
number += "7";
}
private void Button_Click6(object sender, RoutedEventArgs e)
{
number += "6";
}
private void Button_Click5(object sender, RoutedEventArgs e)
{
number += "5";
}
private void Button_Click4(object sender, RoutedEventArgs e)
{
number += "4";
}
private void Button_Click3(object sender, RoutedEventArgs e)
{
number += "3";
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
number += "2";
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
number += "1";
}
private void Button_Click0(object sender, RoutedEventArgs e)
{
number += "0";
}
}
}
I'm open to any tips and ideas you might give to an absolute beginner.
The reason I'm posting this is because I don't know how to make the "res" variable appear in the TextBox after each operation.
I saw online many people talking about TextBox.text, but I don't know how to use it.
Probably I have to use it in .xaml.cs, but how can I define TextBox? How will my program know which TextBox I want the text to be applied to?
Also, I feel like there's a better approach to create a number using the buttons rather than creating 10 separate functions (from button_click0 to button_click9) and I'd like to hear some ideas.
Thanks!
There are several ways to do this, e.g. Data Binding.
But I guess for learning, since this is your very first step in WPF, you should start more easy.
To access a control via code, you have to give it a name in XAML:
<TextBox x:Name="TxtResult" HorizontalAlignment="Left" Height="35" Margin="37,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="321" FontFamily="Arial" FontSize="20" TextAlignment="Right"/>
By that, you can access your Textbox in Code:
private void Button_Click9(object sender, RoutedEventArgs e)
{
number += "9";
this.TxtResult.Text = number;
}
EDIT to answer your last question:
Also for this there are many ways how to make it better or easier. If you want, you can do some research about Commands, Command Parameters and Command Bindings.
But for this simple Demo case, you can simply use the same event handler for all number buttons:
private void ButtonNumberClick(object sender, RoutedEventArgs e)
{
number += ((Button) sender).Content;
this.TxtResult.Text = number;
}
The sender parameter contains the UI object that triggered the event handler, so you'll get a reference to the button that was clicked. And since the Content property of each button already contains the number you want to add to your string, you can grab it from there and append it to the number string.
Now in XAML you just have to assign that event handler to all ten buttons:
<Button Content="9" HorizontalAlignment="Left" Margin="295,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="8" HorizontalAlignment="Left" Margin="166,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="7" HorizontalAlignment="Left" Margin="37,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="6" HorizontalAlignment="Left" Margin="295,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="5" HorizontalAlignment="Left" Margin="166,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="4" HorizontalAlignment="Left" Margin="37,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="3" HorizontalAlignment="Left" Margin="295,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="2" HorizontalAlignment="Left" Margin="166,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="1" HorizontalAlignment="Left" Margin="37,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="0" HorizontalAlignment="Left" Margin="37,316,0,0" VerticalAlignment="Top" Width="321" FontFamily="Arial" Click="ButtonNumberClick"/>

send data from one window to another c# xaml wpf

i want to send Data from one Textbox on window One to a label on window Two.
starting with window two:
<StackPanel>
<StackPanel x:Name="ButtonStackPanel" Height="Auto" Width="Auto">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Button Style="{DynamicResource ButtonStyle}" Content="To The Dark Side" Click="OnClickToDarkSide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Gray" Click="OnClickToGraySide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Light Side" Click="OnClickToLightSide"/>
</StackPanel>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock" Content="{Binding Source=CodeText}"/>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Style Window" Name="StyleWindowButton" Click="OnClickOpenStyleWindow"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Text Window" Name="TextWindowButton" Click="OnClickOpenTextWindow"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
Codebehind of Window Two:
public MainWindow()
{
(App.Current as App).CodeText = _jediCode;
InitializeComponent();
}
private void OnClickToDarkSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Dark);
(App.Current as App).CodeText = _sithCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToLightSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Light);
(App.Current as App).CodeText = _jediCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToGraySide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Gray);
(App.Current as App).CodeText = _grayCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickOpenStyleWindow(object sender, RoutedEventArgs e)
{
if (StyleWindowButton.IsChecked == true)
{
styleWindow = new StyleWindow();
styleWindow.Show();
}
else
{
styleWindow.Close();
styleWindow = null;
}
}
private void OnClickOpenTextWindow(object sender, RoutedEventArgs e)
{
if (TextWindowButton.IsChecked == true)
{
textWindow = new InputWindow();
textWindow.Show();
}
else
{
textWindow.Close();
textWindow = null;
}
}
}
window one:
<Grid>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" TextWrapping="Wrap"
AcceptsReturn="True" AcceptsTab="True" Text="{Binding Path=CodeText, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Margin="10,10,0,0">
<!-- TODO: trigger change from this textbox to the textblock in mainwindow -->
</TextBox>
</Grid>
code behind of one is empty.
app.xaml.cs:
public string CodeText
{
get => _codeText;
set { _codeText = value; OnPropertyChanged(nameof(CodeText)); }
}
Ok, the current behavior is clicking on one of the buttons (Dark Side, Gray, Light Side) leads to changes in the CodeText Property, which leads to a change of the content of the label of Window Two and the text of TextBox of Window One. Changing the text of the TextBox, changes also the CodeText Property, but does not lead to a change in the label and thats confusing, why does it work the one way, but not the other.
hope you have a hint for me. :) Maybe i missed a trigger or a kind of refresh for the label
bindings in window One and Two are set differently. (window Two does it wrong, Content="{Binding Source=CodeText}" is not valid binding)
in fact, window Two removes the binding by assigning CodeText directly as local value:
theTextBlock.Content = (App.Current as App).CodeText;
you should remove that line, and use the same binding as in window One:
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock"
Content="{Binding Path=CodeText, Source={x:Static Application.Current}}"/>

C# WPF Setting Up F HotKeys

I'm trying to make a small application that executes specific functions when F keys are pressed (F1, F2 & F3 in the current context). I just recently started playing with hotkeys in C# but I can't seem to figure it out. I tried chaning System.Windows.Input.KeyEventArgs to System.Windows.Forms.KeyEventArgs but it does not work. I am not sure if this is the best/correct way to do it but yeah logically it makes sense to me. activeTracker acts like a trigger for my loop while other F keys send out text commands.
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
bool activeTracker = false;
private void btnActive_Click(object sender, RoutedEventArgs e)
{
while (activeTracker)
{
IntPtr WindowHandle = FindWindow(txtClassName.Text, txtWindowTitle.Text);
if (WindowHandle == IntPtr.Zero)
{
System.Windows.MessageBox.Show(txtWindowTitle.Text + " does not exist");
return;
}
SetForegroundWindow(WindowHandle);
SendKeys.SendWait(txtMessage1.Text + "{ENTER}");
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
SendKeys.SendWait(txtMessage2.Text + "{ENTER}");
}
}
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.KeyCode == "F1")
{
activateTracker = True;
return;
}else if(e.KeyCode == "F2")
{
activateTracker = False;
return;
}else if(e.KeyCode == "F3")
{
SendKeys.SendWait(txtMessage5.Text + "{ENTER}");
}
}
}
}
<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" KeyDown="Window_KeyDown">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="108*"/>
<ColumnDefinition Width="409*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="txtWindowTitle" HorizontalAlignment="Left" Height="23" Margin="176,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" Visibility="Hidden" Text="Value1" Grid.Column="1"/>
<TextBlock HorizontalAlignment="Left" Margin="33,130,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.Column="1"/>
<TextBox x:Name="txtClassName" HorizontalAlignment="Left" Height="23" Margin="176,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" Visibility="Hidden" Text="Value2" Grid.Column="1"/>
<Label Content="Message 1:" HorizontalAlignment="Left" Margin="10,37,0,0" VerticalAlignment="Top"/>
<Label Content="Message 2:" HorizontalAlignment="Left" Margin="10,68,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtMessage1" HorizontalAlignment="Left" Height="23" Margin="96,40,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" Grid.ColumnSpan="2"/>
<Label Content="Message 3:" HorizontalAlignment="Left" Margin="10,99,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtMessage2" HorizontalAlignment="Left" Height="23" Margin="96,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" Grid.ColumnSpan="2"/>
<TextBox x:Name="txtMessage3" HorizontalAlignment="Left" Height="23" Margin="96,102,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" Grid.ColumnSpan="2"/>
<Button x:Name="btnActive" Content="Activate" HorizontalAlignment="Left" Margin="56,237,0,0" VerticalAlignment="Top" Width="75" Click="btnActive_Click" Grid.ColumnSpan="2"/>
<TextBox x:Name="txtMessage5" HorizontalAlignment="Left" Height="23" Margin="96,146,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" Grid.ColumnSpan="2"/>
</Grid>
</Window>
Use the Key property:
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
switch (e.Key)
{
case Key.F1:
activeTracker= True;
break;
case Key.F2:
activeTracker= False;
break;
case Key.F3:
SendKeys.SendWait(txtMessage5.Text + "{ENTER}");
break;
}
I'm not sure how you're managing to compare the KeyCode property with a string as a) that's WinForms and b) it returns Keys value anyway.
Your KeyDown handler wouldn't compile because you had the wrong variable name activateTracker instead of activeTracker.
Change that and make sure you explicitly reference the System.Windows.Input version of KeyEventArgs and you should be good to go.
You don't need to set up them. You can use FrameworkElement.KeyDown event handler.
In case your application has only one window, you can directly add them to code.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
switch(e.Key)
{
// Add cases for F buttons.
}
}
But if your app uses windows more than one, you can use that:
public static class KeyF
{
public static void RunF(KeyEventArgs e)
{
switch(e.Key)
{
// Add cases for F keys.
// And do stuff.
}
}
}
Then just run it with KeyF.RunF(e);

Custom MessageBox Button In WPF

Is there a way to customize button on WPF MessageBox instead "Yes" and "No", I want "Enter" or "Exit" or something like that . All web i search told it is difficult do it rather by creating a window and all but those were 3 or 4 year old answers. Right now is there any easy way to do it ?
You can customize WPF Toolkit Extende MessageBox. Or you can use a custom one WPFCustomMessageBox that ACB suggested.
I know this is a late answer but it can be useful.
I needed the same thing and I found this way and I wanted to share it.
I use a window for this.
In my window, I have a label for the caption and another for the message.
And three buttons (or more if needed).
Here is my window :
<Window x:Class="CapronCAD.LIB.Controls.CapronMessageBox"
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:CapronCAD.LIB.Controls"
mc:Ignorable="d" Height="450" Width="800" WindowStyle="None" WindowStartupLocation="CenterOwner" WindowState="Minimized" ResizeMode="NoResize" ShowInTaskbar="False" AllowsTransparency="True" Background="Transparent">
<Grid x:Name="grdMessageBox" Background="#33000B4B">
<Border BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Center" Height="250" Margin="0" VerticalAlignment="Center" Width="600" Background="White" CornerRadius="5">
<Grid>
<Button x:Name="btnMessageBoxYes" FontWeight="SemiBold" Content="Oui" HorizontalAlignment="Right" VerticalAlignment="Bottom" Style="{DynamicResource ResourceKey=Capron_Violet_ButtonStyle}" Margin="0,0,30,30" Width="60" Click="btnMessageBoxYes_Click"/>
<Button x:Name="btnMessageBoxNo" FontWeight="SemiBold" Content="Non" HorizontalAlignment="Right" VerticalAlignment="Bottom" Style="{DynamicResource ResourceKey=Capron_Violet_ButtonStyle}" Margin="0,0,103,30" Width="60" Background="#FFCFC9FF" Foreground="#FF6F5DF5" Click="btnMessageBoxNo_Click"/>
<TextBlock x:Name="tbMessageBoxCaption" FontWeight="SemiBold" Margin="30,43,10,0" TextWrapping="Wrap" Text="-" VerticalAlignment="Top" FontFamily="Poppins" FontSize="14"/>
<TextBlock x:Name="tbMessageBoxMessage" Margin="30,80,10,0" TextWrapping="Wrap" Text="-" VerticalAlignment="Top" FontFamily="Poppins"/>
<Image x:Name="imgMessageBoxCancel" HorizontalAlignment="Right" Height="18" Margin="0,19,30,0" VerticalAlignment="Top" Width="18" Source="/CapronCAD.LIB;component/Resources/CloseBlack.png" MouseLeftButtonUp="imgMessageBoxCancel_MouseLeftButtonUp"/>
<Button x:Name="btnMessageBoxClose" FontWeight="SemiBold" Content="Fermer" HorizontalAlignment="Center" VerticalAlignment="Bottom" Style="{DynamicResource ResourceKey=Capron_Violet_ButtonStyle}" Margin="0,0,0,30" Visibility="Collapsed" Click="btnMessageBoxClose_Click"/>
</Grid>
</Border>
</Grid>
Here is the simple method which shows the window as a dialog :
internal static System.Windows.Forms.DialogResult ShowCustomMessageBox(string message, string caption = "Default caption", bool dialog = true)
{
Controls.CapronMessageBox mb = new Controls.CapronMessageBox(caption, message, dialog);
mb.Topmost = true;
mb.WindowState = WindowState.Maximized;
mb.ShowDialog();
if (mb.DialogResult == null)
return System.Windows.Forms.DialogResult.None;
else if (mb.DialogResult == true)
return System.Windows.Forms.DialogResult.Yes;
else
return System.Windows.Forms.DialogResult.No;
}
Here is the code behind the window :
public partial class CapronMessageBox: Window
{
public CapronMessageBox(string caption, string message, bool dialog)
{
InitializeComponent();
tbMessageBoxCaption.Text = caption;
tbMessageBoxMessage.Text = message;
if (!dialog)
{
btnMessageBoxClose.Visibility = Visibility.Visible;
btnMessageBoxNo.Visibility = Visibility.Collapsed;
btnMessageBoxYes.Visibility = Visibility.Collapsed;
}
}
private void btnMessageBoxNo_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
private void imgMessageBoxCancel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.Close();
}
private void btnMessageBoxYes_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
private void btnMessageBoxClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
And here how I call it :
if (UserMethods.ShowCustomMessageBox("Do you want to save changes?", "Are you sure?", false) == DialogResult.Yes)
{
btnSave_Click(btnSave, null);
}
And here is how it seems :

make data grid visible on click

I have this data grid where I am placing all my buttons
<Grid x:Name="ButtonGrid" HorizontalAlignment="Left" Margin="0,90,0,4" Width="186">
<Button x:Name="B1" Content="B1" Height="18" Margin="73,0,59,16" VerticalAlignment="Bottom" Click="B1"/>
<Button x:Name="B2" Content="B2" Height="18" Margin="0,0,-2,16" VerticalAlignment="Bottom" Click="B2_Click" HorizontalAlignment="Right" Width="57"/>
</Grid>
I have the grid collapased on start. But when a button {testGrid} is clicked, I want the grid to ne visible.
Here is my code
namespace project.Test
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
EDUTED
private void testGrid_Click(object sender, System.Windows.RoutedEventArgs e)
{
FrameworkElement ButtonGrid = (sender as FrameworkElement).FindName("ButtonGrid") as FrameworkElement;
if ( ButtonGrid.Visibility == System.Windows.Visibility.Collapsed)
ButtonGrid.Visibility = System.Windows.Visibility.Visible;
else
ButtonGrid.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
I think if you move your Grid outside of your DataTemplate it will work. :)
However if you really need to put it in a DataTemplate, as long as your Button is at the same level as the Grid, you should still be able to find it.
Say your xaml code looks like this,
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="controlstoryboardactionrefissue.MainPage" Width="640" Height="480">
<UserControl.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid x:Name="myGrid" Height="128" Background="#FFE7C0C0" Width="333">
<Button x:Name="myButton" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="31,29,0,0" Click="myButton_Click" />
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ContentControl HorizontalAlignment="Left" VerticalAlignment="Top" Margin="175,198,0,0" ContentTemplate="{StaticResource DataTemplate1}" />
</Grid>
</UserControl>
Then the code behind,
private void myButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var myButton = (Button)sender;
var grid = myButton.Parent as Grid;
if (grid != null)
{
// do stuff
}
}
Hope it helps. :)

Categories

Resources