Is there a simple way to specify int value in xaml - wpf

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.

Related

<i: Interaction.Behavior> option is not coming for applying beahviour

I have been trying to implement a behavior on a wpf window therefore I have added reference to System.Winodws.Interactivity in my current solution and then wrote the desired behavior. but in order to apply this behavior, I have to write something like this in Windows XAML.
<Window x:Class="WpfApplication5.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"
xmlns:behav ="clr-namespace:WpfApplication5"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity;assembly=System.Windows.Interactivity">
<Window.Resources>
<i:Interaction.Behaviors>
<behav:DialogIconRemoveBehavior></behav:DialogIconRemoveBehavior>
</i:Interaction.Behaviors>
</Window.Resources>
<Grid>
</Grid>
</Window>
but this is not valid tag because I might have to add reference to any other assembly apart form System.Windows.Interactivity, So, please suggest what else
I have to do in order to use tag in XAML
After wasting my one hour I came to know that only I had to include System.Windows.Interactivity as reference and which I had already done.
the issue was that Behaviors can not be declared in the Resource part of any control.
the moment I took the following
<i:Interaction.Behaviors>
</i:Interaction.Behaviors>
out of the resources then it worked fine.
So, conclusion only System.Windows.Interacivity is the only required.
Never declare that behavior part in the resources or style.

Split one big XAML in number of Sub-XAML files

In my WPF4 Desktop-based application there is a big block with sidebar menu that repeats in each window and takes about 70 lines of XAML. In order to improve code reuse, I would like to split XAML file in two files:
XAML-file that contains code for sidebar menu (≈70 lines)
Base XAML file that contains «include/reference» to XAML-file with sidebar menu code
As I understood, there are two ways to implement my problem:
Use ResourceDictionary
Use UserControl/CustomControl
My questions:
What is the difference between ResourceDictionary and UserControl? Could you give me examples where I have to use UserControl and where ResourceDictionary?
Could you give a full code example how to include/import content of one XAML-file to other?
P.S. Here is an example of code that I want to export to separate XAML-file:
<Border Style = "{StaticResource Body_SideBarMenu_Border_Settings}">
<StackPanel Style = "{StaticResource Body_SideBarMenu}">
<TextBlock Style = "{StaticResource Body_SideBarMenu_Title}"
Text = "{x:Static res:Resources.WinApp_SideBarMenu_Title}" />
<TextBlock x:Name = "SideBar_WinReports"
Style = "{StaticResource Body_SideBarMenu_Item}"
Text = "{x:Static res:Resources.DashListMarker}">
<Hyperlink KeyboardNavigation.TabIndex = "12"
Style = "{StaticResource Body_SideBarMenu_Item_Hyperlink}"
Click = "Call_WinReports_Click">
<TextBlock Text = "{x:Static res:Resources.WinApp_ModuleName_Reports}" />
</Hyperlink>
</TextBlock>
</StackPanel>
</Border>
ResourceDictionary is just a container for your styles/templates etc. So you really have a choice between using a style (and referencing it through a ResourceDictionary) or a UserControl.
In order to differentiate between the two, ask yourself a question: are you implementing just another look for some existing control, or you are implementing something really new, which is more than just a ListView (or a Border, or a ComboBox etc.)? In the former case, use a style; in the latter, create a new UserControl.
Specifically for your case, I would go for a UserControl.
Code example (although not full)
(Please note that a template for the following code can be inserted with VS's "add new UserControl")
Xaml:
<UserControl x:Class="SomeNamespace.SidebarMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources> <!-- you can define local styles here -->
<Style x:Key="SidebarMenuTextblock" TargetType=TextBlock>
...
</Style>
</UserControl.Resources>
<Border Background=...>
<StackPanel>
<TextBlock
x:Name="Put_a_name_if_you_want_to_reference_this_item_in_code_behind"
Style="{StaticResource SidebarMenuTextblock}"
Text="{x:Static res:Resources.WinApp_SideBarMenu_Title}" />
...
</StackPanel>
</Border>
</UserControl>
.cs:
using System;
using System.Windows;
using System.Windows.Controls;
namespace SomeNamespace
{
public partial class SidebarMenu : UserControl
{
public NumericUpDown()
{
InitializeComponent();
}
...
// define here your properties etc,
}
}
Now, you can use the control like that:
<Window
x:Class="SomeOtherNamespace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SomeNamespace">
<Grid>
<controls:SidebarMenu PropertyIfYouDefinedOne="SomeValue"/>
...
</Grid>
</Window>
If you can get your hands on Expression Studio, in Expression Blend, you can simply right click on any control and convert it to an user control. As easy as that.
User controls are good for splitting the XAML file. In essence, it is used to redefine the behavior of an existing control.
However, with User Controls, you can define whole WPF Layout Controls and convert them to an User Control, with the children content inside them. This is very helpful for a project spread across multiple developers, and can also be used to emulate the behavior of an MDI, which is kind of absent in WPF.

WPF: Changing MediaElement source with ResourceDictionary

I want to use background music in my WPF Application.
Like you can see here: How to do background music for my WPF-Application?
So I use a MediaElement.
Now I want to change the source of it while running the Application.
I'm already doing something similar with some background pictures. There I have different ResourceDictionaries that I'm switching to show different "themes".
One of my dictionaries looks like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ImageBrush x:Key="Backgroundpic" ImageSource="picture.png"/>
...
</ResourceDictionary>
So I can use it in the xaml like this:
...
<Grid x:Name="Bg" Background="{DynamicResource Backgroundpic}"/>
...
But HOW can I do that with my MediaElement-Source that I can use it like this:
<MediaElement x:Name="myMediaElement" Source="{DynamicResource ???}" />
I just don't know what to write into my ResourceDictionary.
Source is a Uri, so you need your resource to be a Uri. (Note that System.Uri is in the System assembly, not mscorlib, so it needs a different XML namespace than you would use for types like String):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=System">
<sys:Uri x:Key="mediaSource">something.mp3</sys:Uri>
Then you can reference it with Source={DynamicResource mediaSource}.

What is the standard convention for defining nested view:viewmodel mapping in MVVM Light

so in classic MVVM examples ive seen DataTemplate definitions are used to map up View Models to Views, what is the standard way to do this in MVVM Light framework, and where should the mappings be located? Following are examples of what I'm doing now and what I'm talking about, blendability is important to me!
Main Window:
<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"
mc:Ignorable="d"
x:Class="STS2Editor.MainWindow"
Title="{Binding ApplicationTitle, Mode=OneWay}"
DataContext="{Binding RootViewModel, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/ApplicationSkin.xaml" />
<ResourceDictionary Source="Resources/ViewMappings.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding ApplicationManagementViewModel}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</Window>
In the above code, my RootViewModel class has an instance of the class ApplicationManagementViewModel with the same property name:
public ApplicationManagementViewModel ApplicationManagementViewModel {get {...} set {...} }
I reference the ResourceDictionary "ViewMappings.xaml" to specify how my view model is represented as a view.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:STS2Editor.ViewModel">
<DataTemplate DataType="{x:Type local:ApplicationManagementViewModel}">
<local:ApplicationManagementView/>
</DataTemplate>
</ResourceDictionary>
should I be doing things like this using ViewModelLocator? what about collections of view models?
The method you use (with implicitly typed DataTemplates) works OK in WPF, but unfortunately it does not work in Silverlight. This is one of the reason why I prefer to use a more explicit method which works in both worlds.
Also, implicitly typed DataTemplates can be a bit confusing, because it is not always quite clear where the template comes from. That can render the work of the integrator very difficult at times, especially for small changes to the UI (been there, done that :)
There is no obligation to use the ViewModelLocator in MVVM Light, it is just a way that works well and is quite easy to understand (for people reading the code who are not familiar with the subtleties of WPF/SL). In the end, it is very much a matter of preference, but lately the ViewModelLocator pattern seems to gain in popularity (see for example this post where a generic ViewModelLocator is used together with MEF).
http://www.johnpapa.net/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/
Finally, let me add that I am not very satisfied with the current implementation of the ViewModelLocator in MVVM Light, and I want to propose a much more generic solution in the next version.

How to update Dynamic Resource within a Dynamic Resource?

I have a visual brush which is a group of shapes, the main colour of which is a dynamic resource itself - so the shape is for example MyShape and the Colour, MyColour which is referenced by the Shape object.
My problem is when I update the colour for this - it only happens the first time the shape is loaded (the colour needs to be set first) however as much as I change the colour it won't update the dynamic resource that uses the colour - how do I make this work?
Just need to make a dynamic resource work within another dynamic resource and have them both update when I change the colour.
I have no idea how to get this to work - I spent time creating a colour-picker for WPF only to find I cannot change the colour of this item - 1-Tier resources work where I set the brush/colour directly but not a colour within another object or 2-Tier Resource.
Edit: My problem seems to be specific to using these in a seperate Resource / Dictionary as my program needs to access this item from a class not the Window, the main example mentioned does not work when the MyColor is in a seperate Resource.
Unless I misunderstand the situation, exactly what you're talking about works pretty well. I just tried it out with this Xaml:
<Window x:Class="ConditionalTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<SolidColorBrush x:Key="MyColor" Color="Aqua" />
<VisualBrush x:Key="MyBrush">
<VisualBrush.Visual>
<Ellipse Height="50" Width="100" Fill="{DynamicResource MyColor}" />
</VisualBrush.Visual>
</VisualBrush>
</Window.Resources>
<Grid Background="{DynamicResource MyBrush}">
<Button Height="30" Width="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" Content="ChangeColor" Click="Button_Click" />
</Grid>
</Window>
And then changed the color in the click handler for that button:
private void Button_Click(object sender, RoutedEventArgs e)
{
((SolidColorBrush)Resources["MyColor"]).Color = Colors.Purple;
}
And it worked like a champ.
Can you post an example of how you are attempting to change the color in the resource dictionary?
When I make a sample app and try to change the resource value it appears that the SolidColorBrush in the resource dictionary has been frozen so it can't be modified. To get around this I just set the new value to a new SolidColorBrush.

Resources