Silverlight: How do I pass True to a CommandParameter? - silverlight

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}" />

Related

Pass another control as property value in XAML

Consider a XAML snippet like this:
<Grid>
<Button x:Name="myButton"/>
<m:MyControl Button="myButton"/>
</Grid>
Of course this doesn't work. But what I need is to pass the Button control instance named "myButton" as value to the other control's Button property. I've also tried it with {StaticResource myButton} but it fails telling me that the resource cannot be found.
What's the correct XAML syntax for that?
If MyControl.Button is a DependencyProperty you can do it so
<m:MyControl Button="{Binding ElementName=myButton}" />

Databinding issue with textbox and accessors

I am wondering what am I missing? The binding is not displaying at all in the textbox. These are my codes:
XAML Namespace:
xmlns:c="clr-namespace:mySystem.Workspace"
DataContext and Resources:
<Grid.Resources>
<c:Parameter x:Key="mySource"/>
</Grid.Resources>
<Canvas>
<Canvas.DataContext>
<Binding Source="{StaticResource mySource}" />
</Canvas.DataContext>
Textbox:
<TextBox x:Name="TextBox" Width="159" Height="26" Canvas.Left="36" Canvas.Top="47">
<TextBox.Text>
<Binding Path="JobKey" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
The class:
namespace mySystem.Workspace
{
public class Parameter : Object
{
The accessors:
public BasePar JobKey
{
get { return jobKey; }
set { jobKey = value; }
}
There are lots of odd things here but the most obvious one that will get you working is that the Binding Path is case sensitive.
Change your binding to:
<Binding Path="JobKey" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
This should get the binding working.
I'm also not sure what type BasePar is, or is meant to be, but unless you are doing something clever intentionally, just make it a standard type like string.
You should also probably not use the namespace System.Workspace, but something related to your own project.
After your response, the only thing I can guess that the BasePar object is intended for, is to be used within a DataTemplate, on an ItemsControl say. DataTemplates have the behaviour that when they do not know how to render an Object they will fall back the the Object's .ToString() method.
Now, in my comment I said that I don't think the TextBox can have a DataTemplate, and I believe this is true however I did find a trick at this Stackoverflow question which templates a content control and a textblock instead. The code is below:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:System.Workspace"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<c:Parameter x:Key="mySource"/>
<DataTemplate x:Key="MyDataTemplate">
<TextBlock Text="{Binding}"/>
</DataTemplate>
</Grid.Resources>
<Canvas>
<Canvas.DataContext>
<Binding Source="{StaticResource mySource}" />
</Canvas.DataContext>
<ContentControl
Content="{Binding Path=JobKey}"
ContentTemplate="{StaticResource MyDataTemplate}" />
</Canvas>
</Grid>
I don't have time right now to get the TextBox working - don't even know if it is possible, given my first few tries. However, this might help get you where you need to go.
But still - if I was me I'd just use simple binding to standard objects. I can't see the benefit of the BasePar class in this scenario.
What does the BasePar implementation look like? Have a look in the Debug Output window to see if you have a line like this:
System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'WpfApplication1.BasePar' and 'System.String'. Consider using Converter property of Binding. BindingExpression:Path=JobKey; DataItem='Parameter' (HashCode=14209755); target element is 'TextBox' (Name='TextBox'); target property is 'Text' (type 'String')
This is telling you that you are trying to bind to the property, but WPF cannot create a 2-way binding, because it cannot convert the text (you type into the TextBox) into a 'BasePar' object.
As per David's suggestion, you could bind to a primitive string type, or alternately (as per the warning message above) you could add a Converter to the binding to convert a string into a BasePar.
you need to make jobkey a DependencyProperty by deriving it from DependencyObject or derive your class from INotifyPropertyChanged and add all the notify code, etc.
if you do not do this, then you will not receive update notifications and your bindings wont work as expected.
Path="jobKey"
You need to bind to the property not the field, i.e. make that upper-case. Also: To debug bindings check the Output-window in Visiual Studio.

Binding an enum to MVVM Light RelayCommand<T> CommandParameter [duplicate]

I want to pass an enum value as command parameter in WPF, using something like this:
<Button
x:Name="uxSearchButton"
Command="{Binding Path=SearchMembersCommand}"
CommandParameter="SearchPageType.First"
Content="Search">
</Button>
SearchPageType is an enum and this is to know from which button search command is invoked.
Is this possible in WPF, or how can you pass an enum value as command parameter?
Try this
<Button CommandParameter="{x:Static local:SearchPageType.First}" .../>
local - is your namespace reference in the XAML
Also remember that if your enum is inside another class you need to use the + operator.
<Button CommandParameter="{x:Static local:MyOuterType+SearchPageType.First}".../>
You can use property element syntax instead of attribute syntax for this:
<Button x:Name="uxSearchButton"
Command="{Binding Path=SearchMembersCommand}"
Content="Search">
<Button.CommandParameter>
<SearchPageType>First</SearchPageType>
</Button.CommandParameter>
</Button>
Also if you want to provide a [Flags] enum you can use the property element syntax:
<Button>
<Button.CommandParameter>
<SearchPageType>First,Second</SearchPageType>
<Button.CommandParameter>
</Button>
Try this
CommandParameter="{x:Static "Class namespace e.g(Models)":SearchPageType.First}"

Dynamic Binding for Settings?

I am facing a problem, I have a application with several setting's files but which have exactly the same fields.
Sample :
Profil1.settings
Profil2.settings
Profil3.settings
What I'd like to do in my setting window XAML is to dynamicaly change the binding source for each profile.
At the moment my XAML binding look like this :
SelectedValue="{Binding Source={x:Static Local:Properties.Profil1.Default}, Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
My problem is how to replace the "Profil1" by "Profil2" without remaking all my Binding one by one for each controls via my code behind ? Is that possible to use some king of reflection variable in the XAML binding source and than simply change the type of that variable with Profil1 type or Profil2 type ?
Anyone can help me about this ?
Thank in advance.
What you want to do is set the DataContext of your settings window to the appropriate profile. You can do it anyway you want, but by doing so, all your bindings would point to that object. To make the bindings work like you have now, you'd do:
<Window x:Class="MyNamespace.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="clr-namespace:MyNamespace"
DataContext="{Binding Source={x:Static Local:Properties.Profil1.Default}}">
...
<ComboBox SelectedValue="{Binding Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />

WPF StaticResource bound to subproperty

I have a class thats created as a resource:
<Window.Resources>
<Model:MyModel x:Key="model" />
</Window.Resources>
The MyModel class has a cli property named Foo. I want the selected value in a combobox to be bound to this property. I thought I could do this but Im getting errors:
<ComboBox SelectedItem="{Binding Source={StaticResource model.Foo}}" />
Heres the error:
Cannot find resource named '{model.Foo}'.
Where did I go wrong? What extra parameters do I need to specify to properly bind to a subproperty?
You almost have it correct. You want to use a combination of the Binding's Path property and its Source property. So use one of the following (they are equivalent.)
{Binding Foo, Source={StaticResource model}}
or
{Binding Path=Foo, Source={StaticResource model}}
Hope this helps.

Resources