WPF: Partial data binding in TextBlock - wpf

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

Related

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

How to add a thousands separator to a Binding of a Multibinding?

So I have this XAML:
<TextBlock.Text>
<MultiBinding StringFormat="SomeText: {0}
SomeOtherText: {1}">
<Binding Path="SomeBoundVar" />
<Binding Path="AnotherBoundVar" StringFormat="{}{0:N2}" />
</MultiBinding>
</TextBlock.Text>
And I'd like to add a thousand separator to the 2nd binding (the StringFormat as above doesn't work).
How can I do that ? Or do I have to use a converter ?
I had a same kind of issue and following is the way I fixed.
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{} Balance {0} : {1:##,#0.00}">
<Binding Path="Currency"/>
<Binding Path="Balance"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Sample Result
Balance AED : 2,456.45

MultiBinding StringFormat of TimeSpan

I cannot for the life of me get this to work. I need to display hh:mm from a pair of timespan objects in a textblock and it is just not working. This is what I have so far:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}From {0:hh\\:mm} to {1:hh\\:mm}">
<Binding Path="StartTime"/>
<Binding Path="EndTime"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
The text block shows up blank. I've also tried the following with the same results:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}From {0} to {1}">
<Binding Path="StartTime" StringFormat="hh\\:mm"/>
<Binding Path="EndTime" StringFormat="hh\\:mm"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
If I have the string format as hust "hh" then I get just the hours, so I suppose I could build it out of 4 pieces but that just does not feel right. Any help is appreciated.
Using hh':'mm in the format string seems to work:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}From {0:hh':'mm} to {1:hh':'mm}">
<Binding Path="StartTime"/>
<Binding Path="EndTime"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Also, this only works in .NET 4

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>

Data Binding: multiple sources for one property

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>

Resources