Wpf String.Format cause dependencyproperty.unsetvalue - wpf

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>

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

Why does MultiBinding with a Converter not work within a ToolTip?

For part of a fairly-complex WPF ToolTip, I'm attempting to use a MultiBinding to produce formatted text based on two properties. The problem is, the binding's MultiConverter receives DependencyProperty.UnsetValue for each item in its values array.
The following works, using a single Binding:
<ToolTipService.ToolTip>
<StackPanel>
<TextBlock>
<TextBlock.Text>
<Binding Path="Amt" Converter="{StaticResource singleValueConverter}"/>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</ToolTipService.ToolTip>
And so does this, using a MultiBinding with StringFormat:
<ToolTipService.ToolTip>
<StackPanel>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat='{0:C} in {1}'>
<Binding Path="Amt"/>
<Binding Path="Currency"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</ToolTipService.ToolTip>
But a MultiBinding with a Converter does not:
<ToolTipService.ToolTip>
<StackPanel>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource multiValueConverter}">
<Binding Path="Amt"/>
<Binding Path="Currency"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</ToolTipService.ToolTip>
The bindings in the last example don't receive any value. This isn't the case outside of a ToolTip - what is going on such that binding fails in this specific case?
Try setting Mode="OneWay" on your binding.
Also, have you checked this problem and solution:
http://social.msdn.microsoft.com/Forums/en-IE/wpf/thread/15ada9c7-f781-42c5-be43-d07eb1f90ed4
The reason of this error is the
tooltips have not been loaded, so
DependencyProperty.GetValue returns
DependencyProperty.UnsetValue. You
should add some code to test that is
value is Dependency.UnsetValue. The
following code shows how to do this.
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue)
return "";
[...]
}
Try this:
<ToolTipService.ToolTip>
<StackPanel>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource multiValueConverter}">
<MultiBinding.Bindings>
<BindingCollection>
<Binding Path="Amt"/>
<Binding Path="Currency"/>
</BindingCollection>
</MultiBinding.Bindings>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</ToolTipService.ToolTip>

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>

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