I'm trying to use a String from a dictionary:
<?xml version="1.0" encoding="utf-8" ?>
<Dictionary EnglishName="English" CultureName="English" Culture="en-US">
...
<Value Id="ButtonSuppressFieldInformation"
ToolTip="Remove field" Name="Remove field number "/>
...
</Dictionary>
In this ConverterParamter to enable multiple langages support:
<Button>
...
<AutomationProperties.Name>
<MultiBinding
Converter="{StaticResource IndexedForAutomationId}"
ConverterParameter="{loc:Translate
Uid=ButtonSuppressFieldInformation, Default=Delete field}">
<Binding RelativeSource="{RelativeSource Self}" />
<Binding ElementName="MyContactDirectoryView"
Path="ListConditionToSearch" />
</MultiBinding >
</AutomationProperties.Name>
</Button>
But the only thing shown is the number (IndexedForAutomationId), the string does not appear.
Using a string instead of "{loc:Translate Uid=ButtonSuppressFieldInformation, Default=Delete field}" works:
<MultiBinding Converter="{StaticResource IndexedForAutomationId}"
ConverterParameter="Delete field">
Displays Delete field 0.
What is the way to use loc:Translate as a ConverterParameter?
This issue could be due to a lot of things and some more code would really help here. I would start, however, with a breakpoint on the Convert() method of the IndexedForAutomationId converter to 1) check if you get the values your are expecting from the inner bindings and 2) check if the converter itself is returning the right string from the dictionary.
Please make sure to review these guidelines about how to debug WPF bindings as well.
Related
I have a button with some multibinding to which is attached a command :
<Button Content="remove" HorizontalAlignment="Right" VerticalAlignment="Top" Cursor="Hand" Focusable="False">
<Button.Command>
<Binding Path="DataContext.DeleteColumnCommand" ElementName="treeView" />
</Button.Command>
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource midConverter}">
<Binding Path="Text" ElementName="tableName"/>
<Binding Path="Name" />
</MultiBinding>
</Button.CommandParameter>
</Button>
I see that when I put a breakpoint in the converter, every value is set and it looks like it is working.
However, when actually called on the command, I receive as argument an array populated with nulls !
I imagine that WPF reuses and mutates the array I saw in my converter, which does not make nonsense as this is a reference type which I have not allocated and in the context of WPF max performance is much needed.
My question is : what best summarizes the guidance/guarantees around mutation like that in WPF ?
Is there a rule around this ?
PS : I see here that other people had the same problem and did not understand the origin apparently.
PPS : I did not make my question clear enough may be, but it follows naturally that one has to allocate a new structure, list, array, whatever, on the heap, as the one you get might be reused. The question is : from this ad-hoc example, what are the rules for WPF in such cases ?
here is an answer to a similar question
you can use Multibinding and a Converter
<Button Content="Add" Command="{Binding AddCommand}"
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource YourConverter}">
<Binding Path="Text" ElementName="txt1"/>
<Binding Path="Text" ElementName="txt2"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
converter: its important to create a new array!
public class YourConverter : IMultiValueConverter
{
public object Convert(object[] values, ...)
{
//.Net > 4.0
return new Tuple<int, int>((int)values[0], (int)values[1]); //<-- this is important
//or .Net < 4.0
//return values.ToArray();//<-- this is important
}
...
}
command
private void CommandExecute(object parameter)
{
var o= (Tuple<int, int>)parameter;
var a= o.Item1;
var b= o.Item2;
Calculater calcu = new Calcu();
int c = calcu.sum(a, b);
}
ps: pls check my syntax - its written from my mind...
I want to concatenate in my window title a property from my viewmodel and a value that cames from a resources file.
This is what I have working without the string from resources:
Title="Binding Path=Description, StringFormat=Building: {0}}"
Now I want to remove the "Building" string and put a value from a resource like I use on other places:
xmlns:res="clr-namespace:Project.View.Resources"
{res:Strings.TitleDescription}
How can I define both? Can I define like a {1} parameter?
I've seen the MultiBinding answer in several places now, and it is almost never necessary to use it. You can define your resource as the string format instead, and as long as there is only one string format argument, no MultiBinding is required. Makes the code a lot more succinct:
<TextBlock Text="{Binding Description, StringFormat={x:Static res:Strings.TitleDesc}}" />
And the TitleDesc resource is obviously "Building: {0}".
Yes, you can. Simply use a MultiBinding.
The MSDN article on StringFormat has an example.
In your case, the code would look something like this:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Source="{x:Static res:Strings.TitleDescription}"/>
<Binding Path="Description"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
I have this XAML
<MultiBinding StringFormat=" {0}{1}/{2}">
<Binding Path="Text" ElementName="tbxAuthHost" />
<Binding Path="Text" ElementName="tbxAuthWebsiteName" />
<Binding Path="Text" ElementName="tbxAuthServicesAddress" />
</MultiBinding>
When I try change " {0}{1}/{2}" into "{0}{1}/{2}" so no leading space there and then Visual Studio gives this error:
Error 3 The text '{1}/{2}' is not allowed after the closing '}' of a MarkupExtension expression. Line 116 Position 56.
How I can fix this issue?
You can fix this by putting {} at the front of the string format.
StringFormat="{}{0}{1}/{2}"
The MSDN Page does a particularly bad job of explaining the format.
If you look at the page on the escape sequence it explains that an opening curly bracket at the beginning denotes a markup extension (e.g. Binding), and {0}{1}/{2} is not a valid markup extension. It doesn't explain that not having it as the first character also works.
I'm trying to combine 2 fields of information in my grid by using a Multibinding, the multibinding is working fine but I'm having problems when I try to start formating 1 of the fields which is a date in this binding.
The 2 fields are Users Initials i.e. EGJ and the entry date hoping to achieve a combined field looking like "EGJ - 01/01/2011"
Below is where I'm with my existing XAML
<tk:DataGridTextColumn.Binding>
<MultiBinding StringFormat=" {0} - {}{1:dd/MM/yyyy}">
<Binding Path="UserInitials" />
<Binding Path="EntryDate" />
</MultiBinding>
</tk:DataGridTextColumn.Binding>
Any help or pointers are most appreciated
Couldn't see the wood for the trees
Simply removing the empty braces solved my problem.
<tk:DataGridTextColumn.Binding>
<MultiBinding StringFormat=" {0} - {1:dd/MM/yyyy}">
<Binding Path="UserInitials" />
<Binding Path="EntryDate" />
</MultiBinding>
</tk:DataGridTextColumn.Binding>
Thanks to everyone who took the time to look.
Unless you intend to have a leading space in the formatted value you should use this binding instead:
<tk:DataGridTextColumn.Binding>
<MultiBinding StringFormat="{}{0} - {1:dd/MM/yyyy}">
<Binding Path="UserInitials" />
<Binding Path="EntryDate" />
</MultiBinding>
</tk:DataGridTextColumn.Binding>
If the StringFormat starts with a left brace { the XAML parser require you to escape it using a pair of braces {}. Otherwise the parser gets confused because braces also are used in the syntax of markup extensions.
Details are found in the XAML documentation for {} Escape Sequence / Markup Extension.
Perhaps you had the escape sequence correctly placed in the format string initially and the moved things around resulting in the empty pair of braces at the wrong place?
I have a MultiBinding, with 1 of the bindings, I want to use a static value eg. 1 or 0.33 how can I do that? also when I am at it, I want to bind to the value 1/3 = 0.33333... can I have an expression?
<!-- not correct syntax, but something like this is what I want to acheive -->
<Binding Value="1" />
UPDATE
I tried something like
<Binding Path="NonExistantValue" FallbackValue="0" />
But it fails ... in a "clean" app it works tho ...
Ah, I found the answer
<!-- in resources -->
<sys:Single x:Key="Single0">0</sys:Single>
<!-- in multibinding -->
<Binding Source="{StaticResource Single0}" />
I am abit confused as to why Single is a float, why the difference in names?