How can I define a variable in XAML? - wpf

I have the following two buttons in XAML:
<Button Content="Previous"
Margin="10,0,0,10"/>
<Button Content="Next"
Margin="0,0,10,10"/>
How can I define "10" to be a variable so I can change it in one place, something like this:
PSEUDO CODE:
<variable x:key="theMargin"/>
<Button Content="Previous"
Margin="{Variable theMargin},0,0,{Variable theMargin}"/>
<Button Content="Next"
Margin="0,0,{Variable theMargin},{Variable theMargin}"/>

Try this:
add to the head of the xamlfile
xmlns:System="clr-namespace:System;assembly=mscorlib"
Then Add this to the resource section:
<System:Double x:Key="theMargin">2.35</System:Double>
Lastly, use a thickness on the margin:
<Button Content="Next">
<Button.Margin>
<Thickness Top="{StaticResource theMargin}" Left="0" Right="0"
Bottom ="{StaticResource theMargin}" />
</Button.Margin>
</Button>
A lot of system types can be defined this way: int, char, string, DateTime, etc
Note:
You're right... Had to do some better testing.. changed to code so that it should work

Similiar to Sorskoot's answer, you can add a thickness resource to use, thereby defining each margin direction independently
<UserControl.Resources>
<Thickness x:Key="myMargin" Top="5" Left="10" Right="10" Bottom ="5"></Thickness>
</UserControl.Resources>
Then just use the Thickness as the Margin:
<Button Content="Next" Margin="{StaticResource myMargin}"/>

Why don't you try adding the value as a StaticResource?
Resources.Add("theMargin", 10);
Then you can get that value like this:
<Button Content="Previous"
Margin="{StaticResource theMargin},0,0,{StaticResource theMargin}"/>
<Button Content="Next"
Margin="0,0,{StaticResource theMargin},{StaticResource theMargin}"/>

You need invoke this before InitializeComponent or use INotifyPropertyChanged Interface after that

In .NET Core #Sorskoot's answer was working, but produced a warning. So instead I used this namespace declaration:
xmlns:system="clr-namespace:System;assembly=System.Runtime"

Related

Resources as nodes in XAML

Is there any way to use a XAML resource as a node instead of an attribute? Something like this?
<Window.Resources>
<Button x:Key="TestButton" x:Shared="False" Content="..." />
</Window.Resources>
<my:ButtonBar x:Name="ButtonBar1">
<my:ButtonBar.Buttons>
{StaticResource TestButton}
</my:ButtonBar.Buttons>
<my:ButtonBar>
<my:ButtonBar x:Name="ButtonBar2">
<my:ButtonBar.Buttons>
{StaticResource TestButton}
</my:ButtonBar.Buttons>
<my:ButtonBar>
Obviously this is just an example and obviously this won't work.
But how could I make this work?
This should work:
<my:ButtonBar x:Name="ButtonBar1">
<my:ButtonBar.Buttons>
<StaticResource ResourceKey="TestButton" />
</my:ButtonBar.Buttons>
<my:ButtonBar>

How can I make a multi panel on-screen keyboard? (XAML)

We have an Intermec touchscreen terminal (OS: Win7).
We use an application on it that uses an on-screen keyboard stored in xaml files (VTFKey, VTKey, VTKeyCaps, VTKeyShifted, VTKeyShiftedCaps).
I pasted the code from VTKey.xaml for the buttons that toggle between keyboard panels (the xaml files mentioned above):
<Button Canvas.Top="105.545" Height="49.995" Width="125" Command="{Binding Path=PressAndRelease}" CommandParameter="VK_CAPITAL" Content="Cap" />
<Button Canvas.Left="724" Canvas.Top="208.692" Height="50" Width="90" Command="{Binding Path=PressAndRelease}" CommandParameter="VK_TOGGLE" Content="Toggle" />
<Button Style="{DynamicResource ShiftKey}" Canvas.Left="836.583" Canvas.Top="156.651" Height="50" Width="163.317" Command="{Binding Path=PressAndHold}" CommandParameter="RSHIFT" Content="Shift" />
My question would be how do these commands (VK_CAPITAL, VK_TOGGLE, RSHIFT) know which xaml file to change to?
How could I create a custom keyboard consisting of 6 customized panels(xaml files)? Is it even possible?
Thank you!
My question would be how do these commands (VK_CAPITAL, VK_TOGGLE, RSHIFT) know which xaml file to change to?
You have XAML for the following three Buttons:
<Button Canvas.Top="105.545" Height="49.995" Width="125"
Command="{Binding Path=PressAndRelease}" CommandParameter="VK_CAPITAL"
Content="Cap" />
<Button Canvas.Left="724" Canvas.Top="208.692" Height="50" Width="90"
Command="{Binding Path=PressAndRelease}" CommandParameter="VK_TOGGLE"
Content="Toggle" />
<Button Style="{DynamicResource ShiftKey}" Height="50" Width="163.317"
Command="{Binding Path=PressAndHold}" CommandParameter="RSHIFT"
Content="Shift" />
Note the Command property... this is the name of the ICommand instance that performs the functionality for each Button. Two of these Buttons use the PressAndRelease Command. In this case, the CommandParameter is also used... inside the PressAndRelease Command code, you would probably see something like this:
if (parameter == "VK_CAPITAL") LoadVkCapitalView();
...
else if (parameter == "VK_TOGGLE") LoadVkToggleView();
As for the last part of your question, you'll have ask a more precise question for me to be able to answer it.

Is there a simple way to specify int value in xaml

I want set Tag property with int value in xaml. But defining int in resources and then reference this resource as binding looks not a perfect way for me. It is easier just to convert string value to int from code.
So, is there some way to easy set int value in xaml?
Please try this.
Add namespace xmlns:sys="clr-namespace:System;assembly=mscorlib" in xaml
<sys:Int16 x:Key="IntNo">1</sys:Int16> or
<sys:Int32 x:Key="IntNo1" >1</sys:Int32>
Note : Similarly You can use for Double value also.
If not interested in declaring it as resource, you can declare it in-line somewhat like this:
<Button>
<Button.Tag>
<sys:Int32>5</sys:Int32>
</Button.Tag>
</Button>
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<Grid>
<Grid.Resources>
<sys:Int32 x:Key="IntValue" >1</sys:Int32>
</Grid.Resources>
<Button x:Name="Button" Tag="{StaticResource IntValue}"></Button>
</Grid>
Is it simple enough? The above sample will be suitable if you going to use your Value in several places. Otherwise:
<Button x:Name="Button" >
<Button.Tag>
<sys:Int32>1</sys:Int32>
</Button.Tag>
</Button>
In XAML 2009 you can simply use the "x" prefix, like x:Boolean, x:Decimal or x:Int32.
See Microsoft - Built-in types for common XAML language primitives
Example:
This example is from a WinUI 3 application (WinUI 3 XAML is very similar to UWP XAML and mostly similar to WPF XAML)
<Window
x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Button x:Name="MyButton" Click="MyButton_Click" Content="Print 'Tag' value to console">
<Button.Tag>
<x:Int32>42</x:Int32>
</Button.Tag>
</Button>
</Grid>
</Window>
Code behind:
private void MyButton_Click(object sender, RoutedEventArgs e)
{
int value = (int) MyButton.Tag;
Debug.WriteLine(value);
}
You can also spefify a command parameter that accepts an int in that way:
<Button Command="{x:Bind ViewModel.AddMinutesCommand}" Content="+ 30 Minutes">
<Button.CommandParameter>
<x:Int32>30</x:Int32>
</Button.CommandParameter>
</Button>
There seems to be a confusion about the availability of XAML 2009 in various technologies: Stackoverflow - Can XAML 2009-related markup extensions be used in WPF?
Honestly, I also do not understand why my working example code can just use xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml for the x namespace
instead of having to specify something like 2009/xaml here.
Feel free to change this answer, if you can clarify this.

Silverlight: How do I pass True to a CommandParameter?

How do I pass True to a CommandParameter?
Currently I am imperatively adding Boolean.True to the resource dictionary, but that seems like a clumsy way to do it.
Because command parameters are of type 'object' the XAML parser is unable to perform type conversion for you. If you pass 'true', the parser has no way of knowing that you want this converted to a boolean value. You will have to do this explicitly. You could use the property element syntax:
<Button>
<Button.CommandParameter>
<sys:Boolean>true</sys:Boolean>
</Button.CommandParameter>
</Button>
Where the sys namepsace is mapped:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
ColinE's answer is fine, but I think it's a bit neater to define the true/false as resources. You only have to do that once:
<UserControl.Resources>
<sys:Boolean x:Key="BoolTrue">True</sys:Boolean>
<sys:Boolean x:Key="BoolFalse">False</sys:Boolean>
</UserControl.Resources>
Then you can reference it as a StaticResource for the CommandParameter:
<Button CommandParameter="{StaticResource BoolTrue}" />
your XAML changes to this.
<Button
Command="{Binding Path=WhateverCommand}"
CommandParameter="{x:Static BooleanHelper.True}" />

Dialog form in wpf change content

I want to display a dialog form for new and edit actions... However title, buttons , and few other things should change.
I am wondering how i could implement this. Provide an enum value at constructor ? Like Mode.New or Mode.Edit ? Is there a way to avoid writing code like spNewButtons.Visibillity=Collapsed .. etc , and put it inside wpf ?
You can bind visibility with your mode property, and create a specific IValueConverter to convert the mode to a proper Visibility value. ie:
<StakPanel Visibility={Binding Mode,Converter={StaticResource myProperConverter}}></StackPanel>
Usually my WPF dialogs are all ContentControls that get displayed in a Popup.
My code usually looks like this:
<Grid Name="RootPanel">
<!-- Other Content -->
<!-- Popup is always last so it gets displayed on top of other contnet -->
<local:PopupPanel
local:PopupPanel.PopupParent="{Binding ElementName=RootPanel}"
local:PopupPanel.IsPopupVisible="{Binding IsPopupVisible}"
local:PopupPanel.PopupEnterKeyCommand="{Binding SaveCommand}"
local:PopupPanel.PopupEscapeKeyCommand="{Binding CancelCommand}">
<DockPanel>
<!-- Header -->
<Label DockPanel.Dock="Top" Content="{Binding PopupHeader}" />
<!-- Buttons -->
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="Save" Command="{Binding PopupSaveCommand}" />
<Button Content="Cancel" Command="{Binding PopupCancelCommand}" />
</StackPanel>
<!-- Actual content displayed is determined by DataTemplates -->
<ContentControl Content="{Binding PopupContent}" />
</DockPanel>
</local:PopupPanel>
</Grid>
I removed a lot of the styles to make this easier to read, but you can see the general idea of how it's put together. My ViewModel usually contains properties for IsPopupVisible, PopupContent, and PopupHeader, and commands for PopupSaveCommand and PopupCancelCommand
I use my own custom popup in most cases, although the same thing could be done with a WPF popup.

Resources