How to hide a binding path - Multibinding wpf - wpf

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}"/>

Related

How to use stringformat in multibinding in WPF XAML

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>

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>

WPF Compact this XAML which assigns static string to binding

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.

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)

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