Use StringFormat for special output in XAML-Binding [duplicate] - wpf

This question already has answers here:
StringFormat is ignored
(3 answers)
Closed 6 years ago.
I have an integer value named Id and want a Label in Xaml to display the following:
(ID: 160)
I tried the following:
<Label Content="{Binding Id, StringFormat='(ID: {0:0})'} />
but it doesn't work - it just displays the value of Id:
160
How can I get this working without using a special ValueConverter class?

You have to use ContentStringFormat for content controls instead of the binding's StringFormat, has been asked various times before, will have to look for that.

Related

Bind selected text in TextBox to the view model in WPF and by MVVM [duplicate]

This question already has answers here:
MVVM and the TextBox's SelectedText property
(5 answers)
Closed 3 years ago.
I am new to WPF and MVVM.
I have a TextBox with some lines of text. I want to bind a piece of text highlighted by a user, to my ViewModel.
How can I do this without coding in code behind?
You cannot directly bind your selected text to a property. You have to implement a DependencyProperty and attach that but this is not trivial for beginners. A good explanation is here:
MVVM and the TextBox's SelectedText property

TextBlock Text property was bound to a source, can I format the text display? [duplicate]

This question already has answers here:
Format part of the text of TextBlock using iValueConverter
(2 answers)
Closed 4 years ago.
I have a TextBlock object with a bound Text property.
I cannot control the binding source, but I can change Binding and TextBlock.
I want to format some key word display style...
For example:
I want to formart word "key" to bold, and formart word "RED" to red color,
And source data is "Example keyword and RED word".
After formatting, the equivalent Xaml should be
Example <Bold>key</Bold>word and <Span Foreground="Red">RED</Span> word
Of course, if the source was changed, Text must also changing synchronously.
What should I do?
You can use TextBlock.inlines like below-
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="Key" />
<Run Foreground="Red" Text="Red" />
</TextBlock.Inlines>
You can also use binding for Text property in inlines.
You can refer this example.

ValueTuple With WPF Binding [duplicate]

This question already has answers here:
Is it possible to bind to a ValueTuple field in WPF with C#7
(3 answers)
Closed 4 years ago.
why binding to ValueTuple property members (like Item1, Item2 ect) dont work?
<TextBlock x:Name="txtTest" Text="{Binding Item1}" />
the code:
txtTest.DataContext = ("Item A", "Another Item..");
output window:
BindingExpression path error: 'Item1' property not found on 'object' ''ValueTuple`2'
However in Tuple It always worked.
As stated in the documentation, Item1 and Item2 of a ValueTuple are fields rather than properties and you can only bind to public properties in WPF.
So if you want to be able to bind to a tuple, you should use the Tuple class.

What does x:Static mean [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does “{x:Static}” mean in XAML?
In a xaml file there is:
Command="{x:Static src:CustomCommands.Create}"
What does the x:Static mean? If I remove it then it throws an error when loading the xaml saying it "Cannot create unknown type CustomCommands.Create"
http://msdn.microsoft.com/en-us/library/ms742135.aspx
The link above from MSDN mentions:
References any static by-value code entity that is defined in a Common
Language Specification (CLS)–compliant way. The static property that
is referenced can be used to provide the value of a property in XAML.

Why does WPF have x:Name and Name XAML? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In WPF, what are the differences between the x:Name and Name attributes?
Why does WPF have x:Name and Name XAML? What's the deal with x:Name?
Long story short: x:Name is an attached property and can thus be set on everything. Name is not an attached property and thus is only available on things that expose a Name property. WPF aliases Name to x:Name so you are safe to always use x:Name and this is what is recommended as you can use it everywhere.

Resources