Data Binding: multiple sources for one property - wpf

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>

Related

How to split apart a string in XAML

Can we do this in xaml using string format at all? I do not want to use any C# as I have no access to that.
For example:
<sap:Parameter x:Key="DescriptionForDisplay">
<sap:Parameter.Value>
<MultiBinding Converter="{StaticResource DescriptionConverter}" ConverterParameter="/">
<Binding Source="{StaticResource aDataSource}" Path="Properties[ID].Value"/>
<Binding Source="{StaticResource aDataSource}" Path="Properties[Description].Value"/>
</MultiBinding>
</sap:Parameter.Value>
</sap:Parameter>
Would give something like:
1234\My Description
And I would love to do the inverse of this so I could have one parameter/static resource set as '1234' and another parameter/static resource be 'My Description'
You could use a StringFormat like this:
<sap:Parameter x:Key="DescriptionForDisplay">
<sap:Parameter.Value>
<MultiBinding StringFormat="{}{0}\{1}">
<Binding Source="{StaticResource aDataSource}" Path="Properties[ID].Value"/>
<Binding Source="{StaticResource aDataSource}" Path="Properties[Description].Value"/>
</MultiBinding>
</sap:Parameter.Value>
</sap:Parameter>
This would display the Id\Description without the use of any converter.

MultiBinding ComboBoxItem.Content

Hi I am trying to achieve a binding like this:
<ComboBoxItem Style="{StaticResource ComboBoxItemStyle2}">
<ComboBoxItem.Content>
<MultiBinding StringFormat=" {}{0} {1}">
<Binding Path="Value" Source="{StaticResource Name}" />
<Binding Path="Name" Source="{StaticResource Person}" />
</MultiBinding>
</ComboBoxItem.Content>
</ComboBoxItem>
Where "Name" is a localized string and "Value" is used to get it's localized string.
for some reason this doesn't seems to work. I am getting empty string.
This might help you: String format using MultiBinding?
Taken from that post:
You are trying to bind a string to an object. But StringFormat requires its target to be a string type. Try putting a TextBlock in your content and bind your data to it.
Also put "" around Name.
Following is the corrected code:
<ComboBoxItem Style="{StaticResource ComboBoxItemStyle2}">
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Value" Source="{StaticResource Name}" />
<Binding Path="Name" Source="{StaticResource Person}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ComboBoxItem>
I need to fix two things:
Keep the content as TextBlock and do binding of text there.
Removed extra spacing from StringFormat i.e. " {}{0} {1}" ==> "{}{0} {1}".
You Can see this link i wish be helpful
https://social.msdn.microsoft.com/Forums/vstudio/en-US/32b81578-b201-4927-bdc2-ebb9a42ae303/comboboxdisplaymemberpath-and-multibinding

Wpf String.Format cause dependencyproperty.unsetvalue

This is the problem:
This is my XAML:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="My binding is: ({0})">
<Binding Path="FieldThatDoesntExistYet"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
My datacontext is being loaded on run time, so It's end with the problem above.
Is there a simple XAML way to just show a default value ? empty or 0 will be great.
Adding a fallback value to your binding should do the trick. Whatever is between the '' will be what is displayed if the binding is invalid.
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="My binding is: ({0})">
<Binding Path="FieldThatDoesntExistYet" FallbackValue='0'></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>

Bind to several class properties

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)

WPF: Partial data binding in TextBlock

So here is my TextBlock:
<TextBlock Text="1 Projects / 1 Issues"></TextBlock>
Using data binding I want replace 1 and 2 with {Binding Path=OpenProjects} and {Binding Path=OpenIssues}. What is the best way to do this?
P.S. I am not married to TextBlock.
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} Projects / {1} Issues">
<Binding Path="OpenProjects"/>
<Binding Path="OpenIssues"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
You should look into string format

Resources