New to Powershell, Xaml and coding in general.
Thanks to StackOverflow, I've build a Grid in Powershell with 10 Columns and 8 Rows and fill it with 80 Buttons (From 1 to 80). Each button will trigger an action.
Now, I would like to have the chance to Click on 1 of these buttons and overlap new buttons (or a whole new Grid) on top of the principal Grid in orther to have more buttons available (From 81 to 160).
To make a clear example:
Let's imagine I'm building an App to make orders in the restaurant.
Grid 1 Show 80 buttons.
If I press the "Main Course" button, it will overlay the 80 buttons containing the 80 differents "Main Course". The same if I press "Dessert" buttons ect.
I understood to use the expression Panel.ZIndex="n" and actually work if I code it in Powershell.
<Button Name="Cell2" Grid.Row="0" Grid.Column="1" Opacity="1" Background="#eae0f5" FontFamily="Impact" FontSize="25" Foreground="White" Content="UP" Panel.ZIndex="1"/>
<Button Name="Cell81" Grid.Row="0" Grid.Column="1" Opacity="1" Background="#dfd1f0" FontFamily="Impact" FontSize="25" Foreground="White" Content="DOWN" Panel.ZIndex="0"/>
But when I want to trigger it with a click button like so:
$WPFCell1.add_Click({$WPFCell81.Panel.ZIndex ="2"})
it returns with the following error:
The property 'ZIndex' cannot be found on this object. Verify that the property exists and can be set.
At line:149 char:22
+ $WPFCell1.add_Click({$WPFCell81.Panel.ZIndex ="2"})
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Where should I add the Porperty ZIndex if this is the error?
I know probably there are different way to setup this things, but I would like to find a way to make it happen with this code couse again, I'm a noob, and I'm working with a Library that work with this kind of code.
Many thanks to all of you!
This is the whole code if can halp:
#MultiView click overlay
$inputXML = #"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
WindowStyle = "None" WindowState="Maximized"
ResizeMode = "NoResize" Title = "overlay" AllowsTransparency = "True" Background = "Transparent" Opacity = "1" Topmost = "True">
<Grid x:Name = "Grid" Background = "Transparent" ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="96" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="96" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="55" />
<RowDefinition Height="190" />
<RowDefinition Height="190" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="55" />
</Grid.RowDefinitions>
<Button Name="Cell1" Grid.Row="0" Grid.Column="0" Opacity="1" Background="#eae0f5" FontFamily="Impact" FontSize="25" Foreground="White" Content="MENU"/>
<Button Name="Cell2" Grid.Row="0" Grid.Column="1" Opacity="1" Background="#dfd1f0" FontFamily="Impact" FontSize="25" Foreground="White" Content="UP" Panel.ZIndex ="1"/>
<Button Name="Cell3" Grid.Row="0" Grid.Column="2" Opacity="1" Background="#d4c1eb" FontFamily="Impact" FontSize="25" Foreground="White" Content="3"/>
<Button Name="Cell4" Grid.Row="0" Grid.Column="3" Opacity="1" Background="#c9b2e6" FontFamily="Impact" FontSize="25" Foreground="White" Content="4"/>
<Button Name="Cell5" Grid.Row="0" Grid.Column="4" Opacity="1" Background="#bfa3e1" FontFamily="Impact" FontSize="25" Foreground="White" Content="4"/>
<Button Name="Cell6" Grid.Row="0" Grid.Column="5" Opacity="1" Background="#b493dc" FontFamily="Impact" FontSize="25" Foreground="White" Content="5"/>
<Button Name="Cell7" Grid.Row="0" Grid.Column="6" Opacity="1" Background="#a984d7" FontFamily="Impact" FontSize="25" Foreground="White" Content="6"/>
<Button Name="Cell8" Grid.Row="0" Grid.Column="7" Opacity="1" Background="#9f74d2" FontFamily="Impact" FontSize="25" Foreground="White" Content="7"/>
<Button Name="Cell9" Grid.Row="0" Grid.Column="8" Opacity="1" Background="#9465cd" FontFamily="Impact" FontSize="25" Foreground="White" Content="8"/>
<Button Name="Cell10" Grid.Row="0" Grid.Column="9" Background="#f75590" FontFamily="Impact" FontSize="25" Foreground="White" Content="9" />
<Button Name="Cell11" Grid.Row="1" Grid.Column="0" Opacity="1" Background="#dfd1f0" FontFamily="Impact" FontSize="25" Foreground="White" Content="10"/>
<Button Name="Cell12" Grid.Row="1" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell13" Grid.Row="1" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell14" Grid.Row="1" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell15" Grid.Row="1" Grid.Column="4" Opacity="1" Background="#8956c8" FontFamily="Impact" FontSize="25" Foreground="White" Content="14"/>
<Button Name="Cell16" Grid.Row="1" Grid.Column="5" Opacity="1" Background="Yellow" FontFamily="Impact" FontSize="25" Foreground="White" Content="15"/>
<Button Name="Cell17" Grid.Row="1" Grid.Column="6" Opacity="0.01"/>
<Button Name="Cell18" Grid.Row="1" Grid.Column="7" Opacity="0.01"/>
<Button Name="Cell19" Grid.Row="1" Grid.Column="8" Opacity="0.01"/>
<Button Name="Cell20" Grid.Row="1" Grid.Column="9" Opacity="1" Background="#8956c8" FontFamily="Impact" FontSize="25" Foreground="White" Content="20"/>
<Button Name="Cell21" Grid.Row="2" Grid.Column="0" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="28" Foreground="White" Content="21"/>
<Button Name="Cell22" Grid.Row="2" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell23" Grid.Row="2" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell24" Grid.Row="2" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell25" Grid.Row="2" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell26" Grid.Row="2" Grid.Column="5" Opacity="0.01"/>
<Button Name="Cell27" Grid.Row="2" Grid.Column="6" Opacity="0.01"/>
<Button Name="Cell28" Grid.Row="2" Grid.Column="7" Opacity="0.01"/>
<Button Name="Cell29" Grid.Row="2" Grid.Column="8" Opacity="0.01"/>
<Button Name="Cell30" Grid.Row="2" Grid.Column="9" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="28" Foreground="White" Content="30"/>
<Button Name="Cell31" Grid.Row="3" Grid.Column="0" Opacity="1" Background="#c9b2e6" FontFamily="Impact" FontSize="30" Foreground="White" Content="31"/>
<Button Name="Cell32" Grid.Row="3" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell33" Grid.Row="3" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell34" Grid.Row="3" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell35" Grid.Row="3" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell36" Grid.Row="3" Grid.Column="5" Opacity="0.01"/>
<Button Name="Cell37" Grid.Row="3" Grid.Column="6" Opacity="0.01"/>
<Button Name="Cell38" Grid.Row="3" Grid.Column="7" Opacity="0.01"/>
<Button Name="Cell39" Grid.Row="3" Grid.Column="8" Opacity="0.01"/>
<Button Name="Cell40" Grid.Row="3" Grid.Column="9" Opacity="1" Background="#6a37a9" FontFamily="Impact" FontSize="30" Foreground="White" Content="40"/>
<Button Name="Cell41" Grid.Row="4" Grid.Column="0" Opacity="1" Background="#bfa3e1" FontFamily="Impact" FontSize="30" Foreground="White" Content="41"/>
<Button Name="Cell42" Grid.Row="4" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell43" Grid.Row="4" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell44" Grid.Row="4" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell45" Grid.Row="4" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell46" Grid.Row="4" Grid.Column="5" Opacity="0.01"/>
<Button Name="Cell47" Grid.Row="4" Grid.Column="6" Opacity="0.01"/>
<Button Name="Cell48" Grid.Row="4" Grid.Column="7" Opacity="0.25" Background="#0000ffff" FontFamily="Impact" FontSize="20" Foreground="White" Content="48"/>
<Button Name="Cell49" Grid.Row="4" Grid.Column="8" Opacity="0.25" Background="#0000ffff" FontFamily="Impact" FontSize="20" Foreground="White" Content="49"/>
<Button Name="Cell50" Grid.Row="4" Grid.Column="9" Opacity="1" Background="#61329a" FontFamily="Impact" FontSize="25" Foreground="White" Content="50"/>
<Button Name="Cell51" Grid.Row="5" Grid.Column="0" Opacity="1" Background="#b493dc" FontFamily="Impact" FontSize="30" Foreground="White" Content="51"/>
<Button Name="Cell52" Grid.Row="5" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell53" Grid.Row="5" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell54" Grid.Row="5" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell55" Grid.Row="5" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell56" Grid.Row="5" Grid.Column="5" Opacity="1" Background="#a984d7" FontFamily="Impact" FontSize="28" Foreground="White" Content="56"/>
<Button Name="Cell57" Grid.Row="5" Grid.Column="6" Opacity="1" Background="#9f74d2" FontFamily="Impact" FontSize="25" Foreground="White" Content="57"/>
<Button Name="Cell58" Grid.Row="5" Grid.Column="7" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="35" Foreground="White" Content="58"/>
<Button Name="Cell59" Grid.Row="5" Grid.Column="8" Opacity="1" Background="#f75590" FontFamily="Impact" FontSize="30" Foreground="White" Content="59"/>
<Button Name="Cell60" Grid.Row="5" Grid.Column="9" Opacity="1" Background="#4d287b" FontFamily="Impact" FontSize="20" Foreground="White" Content="60"/>
<Button Name="Cell61" Grid.Row="6" Grid.Column="0" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="60" Foreground="White" Content="61"/>
<Button Name="Cell62" Grid.Row="6" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell63" Grid.Row="6" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell64" Grid.Row="6" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell65" Grid.Row="6" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell66" Grid.Row="6" Grid.Column="5" Opacity="1" Background="#bfa3e1" FontFamily="Impact" FontSize="25" Foreground="White" Content="66"/>
<Button Name="Cell67" Grid.Row="6" Grid.Column="6" Opacity="1" Background="#a984d7" FontFamily="Impact" FontSize="25" Foreground="White" Content="67"/>
<Button Name="Cell68" Grid.Row="6" Grid.Column="7" Opacity="1" Background="#9465cd" FontFamily="Impact" FontSize="25" Foreground="White" Content="68"/>
<Button Name="Cell69" Grid.Row="6" Grid.Column="8" Opacity="1" Background="#a984d7" FontFamily="Impact" FontSize="30" Foreground="White" Content="69"/>
<Button Name="Cell70" Grid.Row="6" Grid.Column="9" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="45" Foreground="White" Content="70"/>
<Button Name="Cell71" Grid.Row="7" Grid.Column="0" Opacity="1" Background="#9f74d2" FontFamily="Impact" FontSize="25" Foreground="White" Content="71"/>
<Button Name="Cell72" Grid.Row="7" Grid.Column="1" Opacity="1" Background="#9465cd" FontFamily="Impact" FontSize="25" Foreground="White" Content="72"/>
<Button Name="Cell73" Grid.Row="7" Grid.Column="2" Opacity="1" Background="#8956c8" FontFamily="Impact" FontSize="25" Foreground="White" Content="73"/>
<Button Name="Cell74" Grid.Row="7" Grid.Column="3" Opacity="1" Background="#773dbd" FontFamily="Impact" FontSize="25" Foreground="White" Content="74"/>
<Button Name="Cell75" Grid.Row="7" Grid.Column="4" Opacity="1" Background="#6a37a9" FontFamily="Impact" FontSize="25" Foreground="White" Content="75"/>
<Button Name="Cell76" Grid.Row="7" Grid.Column="5" Opacity="1" Background="#61329a" FontFamily="Impact" FontSize="25" Foreground="White" Content="76"/>
<Button Name="Cell77" Grid.Row="7" Grid.Column="6" Opacity="1" Background="#4d287b" FontFamily="Impact" FontSize="25" Foreground="White" Content="77"/>
<Button Name="Cell78" Grid.Row="7" Grid.Column="7" Opacity="1" Background="#44236c" FontFamily="Impact" FontSize="25" Foreground="White" Content="78"/>
<Button Name="Cell79" Grid.Row="7" Grid.Column="8" Opacity="1" Background="#3a1e5c" FontFamily="Impact" FontSize="25" Foreground="White" Content="79"/>
<Button Name="Cell80" Grid.Row="7" Grid.Column="9" Opacity="1" Background="#30194d" FontFamily="Impact" FontSize="25" Foreground="White" Content="80"/>
<Button Name="Cell81" Grid.Row="0" Grid.Column="1" Opacity="1" Background="#8956c8" FontFamily="Impact" FontSize="25" Foreground="White" Content="DOWN" Panel.ZIndex ="0"/>
</Grid>
</Window>
"#
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Window=[Windows.Markup.XamlReader]::Load( $reader )}
catch [System.Management.Automation.MethodInvocationException] {
Write-Warning "We ran into a problem with the XAML code. Check the syntax for this control..."
write-host $error[0].Exception.Message -ForegroundColor Red
if ($error[0].Exception.Message -like "*button*"){
write-warning "Ensure your <button in the `$inputXML does NOT have a Click=ButtonClick property. PS can't handle this`n`n`n`n"}
}
catch{#if it broke some other way :D
Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
}
# Create PowerShell object for each WPF button
$xaml.SelectNodes("//*[#Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Window.FindName($_.Name)}
# Map multiview window click to actual
function buttonclicked($btnNAme){
write-host $btnNAme
switch($btnNAme){
0{$Script:Window.close();break
}
}
}
$WPFCell1.add_Click({$WPFCell16.Panel.ZIndex ="1"})
# Turn on WPF overlay display
$Window.ShowDialog() | out-null
Just found a workaround to get the same results with .Visibility "Hidden" or "Visible".
Same structure, same use of Panel.Zindex="n" but instead of modifying the ZIndex value with the button, I hide or show the layer on top.
But I'm sure there is a more convinient way to act, so if you have the solution, pleaseeee :).
Cheers,
M
Related
In my serial port WPF application , the labels (BaudRate,Parity,DataBits and stopBit) and the StatusBar (for displaying PortStatus) are in wrong place when I maximise the window.
I would like to have suggestion from expertise to resolve my issue.
So please let me know if I can make any changes/edit on my xaml file to have these orientation on the same place as it is in Normal window mode.
<Window
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:SerialReadAndWrite"
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="ATR220ReadAndWrite.MainWindow"
mc:Ignorable="d"
Title="ATR220ReadAndWrite" Height="450" Width="800">
<Grid Margin="0,0,-71,-107">
<ComboBox x:Name="ComPortComboBox" HorizontalAlignment="Left" Height="14" Margin="115,20,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="ComPort_SelectionChanged" DropDownOpened="ComPort_DropDownOpened" VerticalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="False" FontSize="9" FontFamily="Arial"/>
<Label Content="PortNumber :" HorizontalAlignment="Left" Margin="21,14,0,0" VerticalAlignment="Top"/>
<Label Content="PortSettings :" HorizontalAlignment="Left" Margin="21,58,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.546,3.143" FontSize="10" FontFamily="Arial"/>
<Image HorizontalAlignment="Left" Height="26" Margin="359,14,0,0" VerticalAlignment="Top" Width="29" RenderTransformOrigin="-0.23,0.36"/>
<ComboBox x:Name="BaudRateComboBox" HorizontalAlignment="Left" Margin="115,84,0,0" VerticalAlignment="Top" Width="98" Height="17" SelectionChanged="BaudRate_SelectionChanged" IsEnabled="False" IsReadOnly="True" FontSize="9" FontFamily="Arial">
<System:String>115200</System:String>
<System:String>57600</System:String>
<System:String>38400</System:String>
<System:String>19200</System:String>
<System:String>14400</System:String>
<System:String>9600</System:String>
<System:String>4800</System:String>
</ComboBox>
<ComboBox x:Name="ParityComboBox" HorizontalAlignment="Left" Height="17" Margin="308,86,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="Parity_SelectionChanged" IsEnabled="False" FontSize="9" FontFamily="Arial">
<System:String>Even</System:String>
<System:String>Mark</System:String>
<System:String>None</System:String>
<System:String>Odd</System:String>
<System:String>Space</System:String>
</ComboBox>
<ComboBox x:Name="DataBitsComboBox" HorizontalAlignment="Left" Height="17" Margin="115,125,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="DataBits_SelectionChanged" IsEnabled="False" FontSize="8" FontFamily="Arial">
<System:String>5</System:String>
<System:String>6</System:String>
<System:String>7</System:String>
<System:String>8</System:String>
</ComboBox>
<ComboBox x:Name="StopBitsComboBox" HorizontalAlignment="Left" Height="17" Margin="308,123,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="StopBits_SelectionChanged" IsEnabled="False" FontSize="10" FontFamily="Arial">
<System:String>One</System:String>
<System:String>Two</System:String>
<System:String>OnePointFive</System:String>
</ComboBox>
<Label Content="Baudrate :" HorizontalAlignment="Center" Margin="47,81,753,423" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="63"/>
<Label Content="Parity :" HorizontalAlignment="Center" Margin="234,81,573,423" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="56"/>
<Label Content="DataBits :" HorizontalAlignment="Center" Margin="48,123,753,382" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="62"/>
<Label Content="StopBit :" HorizontalAlignment="Center" Margin="238,123,567,382" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="58"/>
<Button x:Name="ConnectButton" Content="Connect" HorizontalAlignment="Left" Margin="308,20,0,0" VerticalAlignment="Top" Width="75" Click="ConnectButton_Click" IsEnabled="False" Height="14" FontSize="10" FontFamily="Arial"/>
<Label Content="ProtocolCustomWndow :" HorizontalAlignment="Left" Margin="21,172,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.546,3.143" FontSize="10" FontFamily="Arial"/>
<ComboBox x:Name="ProtocolTypeComboBox" HorizontalAlignment="Left" Height="14" Margin="141,177,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="ProtocolTypeComboBox_SelectionChanged" VerticalContentAlignment="Stretch" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" FontSize="9" FontFamily="Arial" IsEnabled="False">
<System:String>WLink</System:String>
</ComboBox>
***<StatusBar HorizontalAlignment="Left" Height="28" Margin="0,396,0,0" VerticalAlignment="Top" Width="794">
<StatusBarItem FontSize="9" FontFamily="Arial" TextOptions.TextHintingMode="Fixed">
<TextBlock Name= "PortStatus"/>
</StatusBarItem>
</StatusBar>***
</Grid>
</Window>
Images of the correct and wrong pics are attached.
wrong position
Wrong Position
Correct position
Correct position
Try something like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="PortNumber :" HorizontalAlignment="Center"/>
<ComboBox FontSize="9" FontFamily="Arial" Grid.Column="1">
<ComboBoxItem IsSelected="True">COM 3</ComboBoxItem>
</ComboBox>
<Button Content="Connect" Grid.Row="0" Grid.Column="3"></Button>
<TextBlock Text="PortSettings :" FontSize="10" FontFamily="Arial" Grid.Row="1" HorizontalAlignment="Center"/>
<TextBlock Text="Baudrate :" FontStyle="Italic" Grid.Row="2" HorizontalAlignment="Center"/>
<ComboBox IsEnabled="False" IsReadOnly="True" FontSize="9" FontFamily="Arial" Grid.Row="2" Grid.Column="1">
<System:String>115200</System:String>
<System:String>57600</System:String>
<System:String>38400</System:String>
<System:String>19200</System:String>
<System:String>14400</System:String>
<System:String>9600</System:String>
<System:String>4800</System:String>
</ComboBox>
<TextBlock Text="Parity :" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Center"/>
<ComboBox Grid.Row="2" Grid.Column="3"/>
</Grid>
Pay special attention to use of Grid.Row and Grid.Column, you will need to set the Horizontal Alignment on these elements but this will show you how to position things in WPF.
I have this code:
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="300*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label x:Name="labelMultiplicador" Grid.Row="0" Grid.Column="0" Content="Multiplicador:" FontWeight="Bold" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100" />
<Slider x:Name="sliderMultiplicador" Grid.Row="0" Grid.Column="1" Orientation="Horizontal" SmallChange="1" IsSnapToTickEnabled="True" Minimum="1" VerticalAlignment="Center" Height="1" MinHeight="30" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" MinWidth="300" Margin="5" TickPlacement="TopLeft" />
<TextBox x:Name="textBoxMultiplicador" Grid.Row="0" Grid.Column="2" Width="50" Text="{Binding ElementName=sliderMultiplicador,Path=Value}" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="1,0,0,0" Margin="5" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" MinHeight="30" />
<Label x:Name="labelMultiplicando" Grid.Row="1" Grid.Column="0" Content="Multiplicando:" FontWeight="Bold" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100" />
<Slider x:Name="sliderMultiplicando" Grid.Row="1" Grid.Column="1" Orientation="Horizontal" SmallChange="1" IsSnapToTickEnabled="True" Minimum="1" VerticalAlignment="Center" MinHeight="30" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" MinWidth="300" Margin="5" Height="1" TickPlacement="TopLeft" />
<TextBox x:Name="textBoxMultiplicando" Grid.Row="1" Grid.Column="2" Width="50" Text="{Binding ElementName=sliderMultiplicando,Path=Value}" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="1,0,0,0" Margin="5" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" MinHeight="30" />
<Label x:Name="labelProduto" Grid.Row="2" Grid.Column="0" Content="Produto:" FontWeight="Bold" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100" />
<TextBox x:Name="textBoxProduto" Grid.Row="2" Grid.Column="2" Width="50" Text="" HorizontalAlignment="Right" VerticalAlignment="Center" Padding="1,0,0,0" Margin="5" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" MinHeight="30" />
</Grid>
I want to calculate the product (textBoxProduto) using the values from textBoxMultiplicador and textBoxMultiplicando.
I'm new to WPF so this might be a "stupid" question!!!
If i try to calculate the product using the textBoxMultiplicador_TextChanged event, for example, e get a "'System.NullReferenceException'".
What is the best approach in this case?
Thanks
Full example here: https://www.dropbox.com/s/e8w9t59vjhnqjbb/tabuada.zip?dl=0
The textbox top and bottom border is not showing.
I have provided the entire XAML code for the main window. I have tried increasing the BorderThickness and also changed the BorderBrush. That did not work.
XAML:
<Window x:Class="WpfApplication6.Window1"
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:WpfApplication6"
mc:Ignorable="d"
Title="Activation Window" Height="300" Width="518.797">
<Grid>
<TextBox x:Name="txtFirstDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="23,171,0,76" BorderThickness="1" BorderBrush="Black" />
<TextBox x:Name="txtSecondDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="81,171,0,76"/>
<TextBox x:Name="txtThirdDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="138,171,0,76"/>
<TextBox x:Name="txtFourthDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="195,171,0,76"/>
<TextBox x:Name="txtFifthDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="252,171,0,76"/>
<Label x:Name="label" Content="Product key:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="23,142,0,0"/>
<TextBox x:Name="txtFirstDigit_Copy" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="274" Margin="23,111,0,136" />
<Label x:Name="label_Copy" Content="Email:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="23,81,0,0"/>
<TextBox x:Name="txtFirstDigit_Copy1" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="130" Margin="23,55,0,192" />
<Label x:Name="label_Copy1" Content="First Name:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="23,25,0,0"/>
<TextBox x:Name="txtFirstDigit_Copy2" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="130" Margin="167,55,0,192" />
<Button x:Name="button" Content="Next" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="142,220,0,0"/>
<Button x:Name="button1" Content="Cancel" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="222,220,0,0" Click="button1_Click"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Margin="350,147,0,0"/>
</Grid>
</Window>
I'd try something like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<Label x:Name="label_Copy1" Grid.Row="0" Content="First Name:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="23,0,0,0" />
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox x:Name="txtFirstDigit_Copy1" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="130" Margin="23,0,0,0" />
<TextBox x:Name="txtFirstDigit_Copy2" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="130" Margin="11.5,0,0,0" />
</StackPanel>
<Label x:Name="label_Copy" Grid.Row="2" Content="Email:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="23,0,0,0" />
<TextBox x:Name="txtFirstDigit_Copy" Grid.Row="3" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="270" Margin="23,0,0,0" />
<Label x:Name="label" Grid.Row="4" Content="Product key:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="23,0,0,0" />
<StackPanel Orientation="Horizontal" Grid.Row="5">
<TextBox x:Name="txtFirstDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="23,0,0,0" BorderThickness="1" />
<TextBox x:Name="txtSecondDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="11.5,0,0,0" />
<TextBox x:Name="txtThirdDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="11.5,0,0,0" />
<TextBox x:Name="txtFourthDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="11.5,0,0,0" />
<TextBox x:Name="txtFifthDigit" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="45" Margin="11.5,0,0,0" />
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="6">
<Button x:Name="button" Content="Next" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="132,0,0,0" />
<Button x:Name="button1" Content="Cancel" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="11.5,0,0,0" />
</StackPanel>
</Grid>
I am new with WPF please keep that in mind. I am trying to Make the Window Responsive to Resize, Is it possible that my controls such as textboxes and buttons are Resized as window grows or shrinks?
<Window x:Class="WPF_Working_Experimenet.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="216.586" Width="459.256">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="156*"/>
<ColumnDefinition Width="295*"/>
</Grid.ColumnDefinitions>
<Label Content="username :" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="66,27,0,0" Height="30" Width="63" FontFamily="B Nazanin" FontSize="15" Grid.Column="1"/>
<Label Content="password :" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="74,63,0,0" Height="30" Width="55" FontFamily="B Nazanin" FontSize="15" Grid.Column="1"/>
<Label x:Name="lblwrong" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,150,0,0" ClipToBounds="True" Grid.Column="1" Height="26" Width="275"/>
<Button x:Name="btnLogin" Content="Login" HorizontalAlignment="Left" VerticalAlignment="Top" Width="60" Grid.Column="1" Margin="211,103,0,0" Height="37" Background="White" Click="btnLogin_Click" BorderBrush="Red" FontFamily="B Nazanin" FontSize="15"/>
<Image HorizontalAlignment="Left" Height="117" Margin="4,23,0,0" VerticalAlignment="Top" Width="178" Source="img/Ticket_5523675581838074942.png" Grid.ColumnSpan="2" RenderTransformOrigin="0.5,0.128"/>
<Button x:Name="btnExit" Content="Exit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="60" Grid.Column="1" Margin="146,103,0,0" Height="37" Background="#FFFDFDFD" BorderBrush="Red" Click="Button_btnExit" FontFamily="B Nazanin" FontSize="15"/>
<TextBox x:Name="txtUsername" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="125" Grid.Column="1" Margin="146,34,0,0" BorderBrush="#FFF7311E"/>
<PasswordBox x:Name="txtPassword" Grid.Column="1" HorizontalAlignment="Left" Margin="146,70,0,0" VerticalAlignment="Top" Width="125" Height="23" BorderBrush="#FFFD3306"/>
</Grid>
</Window>
Any Reference/hint or Tutorial/link would be lovely. thank you in advance
You have to set Margin / Padding instead of explicit Width / Height if you want your controls to grow / shrink.
Few links to help you :
Layout with Absolute and Dynamic Positioning
How to make all controls resize accordingly proportionally when window is maximized?
Controls do not resize if you set their Width and Height properties explicitly. Try the following, I removed the Width and Height properties and changed the HorizontalAlignment and VerticalAlignment values to Stretch.
<Window x:Class="WPF_Working_Experimenet.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="216.586" Width="459.256">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="156*"/>
<ColumnDefinition Width="295*"/>
</Grid.ColumnDefinitions>
<Label Content="username :"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
FontFamily="B Nazanin"
FontSize="15"
Grid.Column="1"/>
<Label Content="password :"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
FontFamily="B Nazanin"
FontSize="15"
rid.Column="1"/>
<Label x:Name="lblwrong"
Content=""
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ClipToBounds="True"
Grid.Column="1"/>
<Button x:Name="btnLogin"
Content="Login"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Column="1"
Background="White"
Click="btnLogin_Click"
BorderBrush="Red"
FontFamily="B Nazanin"
FontSize="15"/>
<Image HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Source="img/Ticket_5523675581838074942.png"
Grid.ColumnSpan="2"
RenderTransformOrigin="0.5,0.128"/>
<Button x:Name="btnExit"
Content="Exit"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Column="1"
Background="#FFFDFDFD"
BorderBrush="Red"
Click="Button_btnExit"
FontFamily="B Nazanin"
FontSize="15"/>
<TextBox x:Name="txtUsername"
HorizontalAlignment="Stretch"
TextWrapping="Wrap"
VerticalAlignment="Stretch"
Grid.Column="1"
BorderBrush="#FFF7311E"/>
<PasswordBox x:Name="txtPassword"
Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderBrush="#FFFD3306"/>
</Grid>
</Window>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Source="View/Images/LoginImg.jpg" Stretch="Fill" Grid.Column="0" Grid.ColumnSpan="3">
<Image.Effect>
<BlurEffect Radius="8"/>
</Image.Effect>
</Image>
<TextBox Grid.Column="0" Grid.ColumnSpan="3" BorderBrush="Transparent" BorderThickness="0" Background="Transparent" Foreground="White" HorizontalAlignment="Center"
Height="78" Margin="122,10,45.6,0" TextWrapping="Wrap" Text="Library Management System " VerticalAlignment="Top" Width="594" FontSize="40" FontWeight="Bold" FontStyle="Normal">
<TextBox.VerticalContentAlignment>Center</TextBox.VerticalContentAlignment>
<TextBox.IsReadOnly>True</TextBox.IsReadOnly>
</TextBox>
<Canvas Grid.Column="1" HorizontalAlignment="Stretch" Width="auto" Height="300" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" Background="White">
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="0.627"/>
<TranslateTransform/>
</TransformGroup>
</Canvas.RenderTransform>
<Label Content="Login" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Background="BurlyWood" FontSize="15" Height="52" Width="254">
<Label.FontWeight>Bold</Label.FontWeight>
</Label>
<TextBox Height="24" Canvas.Left="20" TextWrapping="Wrap" Text="User Name" TextAlignment="Center" Canvas.Top="84" Width="120" Foreground="Gray"/>
<PasswordBox Canvas.Left="20" Canvas.Top="136" Height="34" Width="120"/>
</Canvas>
</Grid>
Setting the horizontal and vertical alignment to stretch instead of setting width and height of the widget will do the trick. But still you can set a margin if you want. Tried this out and it ensured that when the window size is changed the widget will adjust respectively.
i had made a user control numeric pad in wpf. i want to know when i placed a control over the form. when i pressed any numeric button then it should be entered in the focused area. how to do it
this is my xaml script
enter code here
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Name="btn2" Content="2" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row=" 1" />
<Button Name="btn1" Content="1" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" BorderBrush="DarkGray" BorderThickness="4,2,2,8" />
<Button Name="btn3" Content="3" Focusable="False" FontSize="26" Grid.Column="2" Foreground="White" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" />
<Button Name="btn4" Content="4" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn5" Content="5" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn6" Content="6" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn7" Content="7" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn8" Content="8" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn9" Content="9" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn0" Content="0" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn00" Content="00" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_dot" Content="." Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_temp1" Content="Button" Focusable="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp2" Content="Button" Focusable="False" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp3" Content="Button" Focusable="False" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
``
You should attach an event handler to the UIElement.PreviewKeyDown Event to handle key presses in WPF in your UserControl constructor, or Loaded event handler:
PreviewKeyDown += YourKeyEventHandler;
...
private void YourKeyEventHandler(object sender, KeyEventArgs e)
{
// Do something here with e.Key (the pressed key)
}
You can find out more from the linked page on MSDN.