There may not be any practical use of what I want to do, but just to satisfy my curiosity can we compact the 2nd binding in the following XAML into 1 line
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource MyConverter}">
<Binding Source="{StaticResource One}"></Binding>
<Binding>
<Binding.Source>
<sys:String>2</sys:String>
</Binding.Source>
</Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
StaticResource One gives me "1" and MyConverter just concatenates all values it gets.
What I am looking for is some way to express the 2nd binding in a compact format like the 1st binding.
I'm assuming there's some reason you don't want to define a second static resource called Two and then use the same syntax as with One...
You should be able to:
<Binding Source="2"/>
Since Source is just an Object, this should assign the string "2" to the Source. Haven't checked though as I'm on linux at the moment.
Related
I have a multibiding issues. I have two binding path in my code but I want to show only one. How I can hide the first one ?
Thanks
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="PK_ID_Order"/>
<Binding Path="Wording" />
</MultiBinding>
</TextBlock.Text>
As others have commented it's not entirely clear what you're asking for, but i'm going to take a stab and guess you're trying to get a single binding but when you remove one of the binding lines it no longer works. This is because you will need a normal inline/single binding. So here is what you may want.
<TextBlock Text="{Binding Path=Wording}"/>
As you know StringFormat is of great importance for data representation in WPF. My problem is how to use StringFormat when multibinding in WPF?
If I give a very simple example:
We have variables,which are A and B and whose values are 10.255555 and 25.6999999
And we want to show them 10.2,25.6?
How can I do this with multibinding? Normally it is piece of cake with ValueConverter
Any help and ideas on this topic will be greately appreciated
Just set the StringFormat property on the MultiBinding; use placeholders ({0}, {1}...) for each binding in the multibinding, and include format specifiers if necessary (e.g. F1 for a decimal number with 1 decimal digit)
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:F1}{1:F1}">
<Binding Path="A" />
<Binding Path="B" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
The {} part at the beginning is the format string is an escape sequence (otherwise the XAML parser would consider { to be the beginning of a markup extension)
If anyone is looking for "Time Formats" this is for 24hr Clock which is how I came to this post:
<TextBlock TextAlignment="Center">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:HH:mm} - {1:HH:mm}">
<Binding Path="StartTime" />
<Binding Path="EndTime" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
The leading 0: and 1: are the reference to the Bindings
[0:hh:mm tt] to display AM/PM
To simplify you could use two TextBlock/Labels to display the values.
If you are using .Net4, you can bind in a Run Inline element of a TextBlock
<TextBlock>
<Run Text="{Binding A, StringFormat={}{0:F1}}"/>
<Run Text="{Binding B, StringFormat={}{0:F1}}"/>
</TextBlock>
Is it possible to use CommandParameter="{Binding}" in a multi binding?
I am trying to do this in a data grid.
<CheckBox.CommandParameter>
<MultiBinding Converter="{StaticResource CDetailConverter}">
<Binding Path ="IsChecked" ElementName="chkSelection"/>
<Binding ConverterParameter="{Binding}"/>
</MultiBinding>
</CheckBox.CommandParameter>
The second Binding throws an error.
In a nutshell, the answer is no.
In your second inner Binding you have set ConverterParameter. There are a couple of problems with this:
First, Binding is its own class separate from MultiBinding with both Converter and ConverterParameter properties. Here you have set the ConverterParameter property without setting the Converter property. Remember that ConverterParameter is passed to the Binding's specified converter regardless if it is used within a MultiBinding or not. If you were to add a Converter here, then the converter would be passed the specified ConverterParameter.
What you probably meant to do was set the ConverterParameter on the outer MultiBinding which also has this property:
<CheckBox.CommandParameter>
<MultiBinding Converter="{StaticResource CDetailConverter}" ConverterParameter="{Binding }">
<Binding Path ="IsChecked" ElementName="chkSelection"/>
</MultiBinding>
</CheckBox.CommandParameter>
If you try this, you will quickly see that ConverterParameter can not be the target of a Binding expression since it is not a DependencyProperty.
Since you can not bind to CommandParameter, the typical workaround is to modify your IMultiConverter to accept an additional value, and supply this value through a binding expression:
<CheckBox.CommandParameter>
<!-- CDetailConverter updated to expect an additional value in the values array -->
<MultiBinding Converter="{StaticResource CDetailConverter}">
<Binding Path ="IsChecked" ElementName="chkSelection"/>
<Binding />
</MultiBinding>
</CheckBox.CommandParameter>
Hope this helps!
I have some class with properties firstName and lastName. I want bind TextBlock to concatanation of this two properties. I know that I can create third property that will be return concatanation of these properties. But I dont want to use this approach. Is it possible to Bind TextBlock to two properties. and also I dont want create composite userControl.
In .NET 3.5SP1, Microsoft added StringFormat to bindings. This makes it much easier. See Lester's blog post for an example. In your case:
<TextBox>
<TextBox.Text>
<MultiBinding StringFormat="{0} {1}">
<Binding Path="FirstName" />
<Binding Path="LastName"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
or
<TextBox>
<TextBox.Text>
<MultiBinding StringFormat="{1}, {0}">
<Binding Path="FirstName" />
<Binding Path="LastName"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
You could use multibinding, but I guess that you have to code your way out of the concatanation.
Here is an example: Multibinding
I'm not sure if it's possible to bind to two properties, but there is not reason you cannot create two TextBlocks right?
<TextBlock Text="{Binding firstName}"/> <TextBlock Text="{Binding lastName}"/>
use either MultiBinding or Converter (if complex operation is there)
I want to bind one property to multiple sources. My reason for this are things like this:
midpoint=point2.X - point1.X; //depends on two sources!
How could this be realised? As far as I know it's not directly possible out-of-the-box?
I believe what you are looking for is a MultiBinding.
From the MSDN documentation:
<TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource myNameConverter}"
ConverterParameter="FormatLastFirst">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>