Binding to two values - c#

Is it possible to bind a label content to two values. For eg, I want a single label whose content is displayed as below,
UserName= Firstname, Lastname
where Firstname and Lastname, both are values from database. If I would be using to labels I would bind as Content={Binding Firstname} for one and Content={Binding Lastname}
for another. But I want a single label to display both. Is it possible?

You can do something like this
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding Path="firstName" />
<Binding Path="lastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>

Related

Telerik RadComboBox ItemTemplate and MultipleSelectionBoxTemplate

how can i specify MultipleSelectionBoxTemplate which i can access properties of selected items
i have an employee class
public class Employee {
public string Firstname {get;set;}
public string Lastname {get;set;}
}
I am using Telerik radCombobox in my wpf application to display list of employee
<telerik:RadComboBox x:Name="radComboBox"
Width="200"
AllowMultipleSelection="True"
ItemsSource="{Binding Path=EmployeeList}"
>
<telerik:RadComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Lastname}" />
</DataTemplate>
</telerik:RadComboBox.ItemTemplate>
this works fine for itemtemplate but when i select an item the display text in combobox show name of Employee class not the Lastname
i tried
<telerik:RadComboBox.MultipleSelectionBoxTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Lastname}" />
</DataTemplate>
</telerik:RadComboBox.MultipleSelectionBoxTemplate>
i get no result , the combobox displayvalue of selected item is always empty.
i tried another solution
<telerik:RadComboBox.MultipleSelectionBoxTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=SelectedItems,Converter={StaticResource SelectionToCustomStringCoverter}}" />
</DataTemplate>
</telerik:RadComboBox.MultipleSelectionBoxTemplate>
this neither work , the converter always execute at first selection
this is really crazy spending 4 hours to figure out a simple thing ,
so my question is how can i specify MultipleSelectionBoxTemplate which i can access properties of selected items .
this neither work , the converter always execute at first selection
You need to bind to a property that is actually set when the selection changes, such as the Count property of the SelectedItems collection.
Try using a multi value converter and a MultiBinding:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource SelectionToCustomStringCoverter}">
<Binding Path="SelectedItems" RelativeSource="{RelativeSource AncestorType=telerik:RadComboBox}"/>
<Binding Path="SelectedItems.Count" RelativeSource="{RelativeSource AncestorType=telerik:RadComboBox}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
You will then get the currently selected items from values[0] in the converter.

WPF animation how to change animation value with stringformat

I want to bind a string value with string format.
I have tried many ways but it didn't work.
Could anybody help me to solve this problem?
This is the way I am currently using, but I still want to use StringFormat.
How could I do this ?
<DiscreteObjectKeyFrame.Value>
<MultiBinding Converter="{StaticResource DotConverter}">
<Binding Path="LoadingStringShow"/>
<Binding>
<Binding.Source>
<sys:Int16>1</sys:Int16>
</Binding.Source>
</Binding>
</MultiBinding>
</DiscreteObjectKeyFrame.Value>
The best way to format a string is to do this in code.
You could use string.Format() or for C#6's new Feature: "Interpolated Strings" as shown below to format your string.
private string _name;
public string Name
{
get {return $"My Name is {_name}";}
set
{
_name = value;
//OnPropertyChanged("Name");
}
}
Your Binding will then show: My Name is <valueofvariable>
Since your bindings name is LoadingStringShow I assume you want to display some kind of loading message.
Maybe this could also help:
<TextBlock Text="{Binding LoadingStringShow, StringFormat={}{0}%}" />
or
<TextBlock TextAlignment="Center">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}%">
<Binding Path="LoadingStringShow" />
<Binding Path="CurrentValue" />
</MultiBinding>
</TextBlock.Text>
Reference 1
Reference 2

Partly deactivate xaml Styler plugin (at LineBreak MultiBinding cleanup)

I'm using xaml-Styler Plugin on VS2015. So far I didn't had any problems.
Now I have the problem, that the styler removes linebreaks (I use HTML encoded characters).
xaml (simplified)
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}
Min X: {1:F3}; Max X: {2:F3}">
<Binding Path="Area.Name" ... />
<Binding Path="Area.MinX" ... />
<Binding Path="Area.MaxX" ... />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
after Pressing Save, the xaml styler automatically makes this (html character is removed and LineBreak inserted:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}
Min X: {1:F3}; Max X: {2:F3}">
<Binding Path="Area.Name" ... />
<Binding Path="Area.MinX" ... />
<Binding Path="Area.MaxX" ... />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
then the text is displayed at one line.
I'm not happy with using html encoded characters at all. Is there maybe a better (simple) way to format strings with linebreaks in a MultiBinding?
For this simple formatting I don't want to use a MultiValueConverter, because it is only an informative string...
If I could tell xaml styler don't style this part/line I would be happy, but didn't found the possibility or a property in options of xaml styler.
You can use the hex representation of the LineFeed character (char 10) :
to get a line break :
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}
Min X: {1:F3}
Max X: {2:F3}">
<Binding Path="Area.Name" ... />
<Binding Path="Area.MinX" ... />
<Binding Path="Area.MaxX" ... />
</MultiBinding>
</TextBlock.Text>
</TextBlock>

ComboBox.ItemTemplate not displaying selection properly

In the question c# wpf - cannot set both DisplayMemberPath and ItemTemplate, I have read that you can replace the DisplayMemberPath to combine multiple data items in a ComboBox.
I set up my Combobox using ItemTemplate and it is successfully populated. But when I select an item in my ComboBox, I get Data.MaterialNumber displayed instead of the actual text I selected.
MaterialNumbers is an ObservableCollection<>
Can someone tell me why my ComboBox is not displaying the item correctly after it is selected?
// Binding from my ViewModel, which retrieves material numbers correctly.
ObservableCollection<MaterialNumber> MaterialNumbers = GetMaterialNumbers();
<ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding MaterialNumbers}"
SelectedItem="{Binding MaterialNumber}"
Validation.Error="View_Validator" Validation.ErrorTemplate="{StaticResource ErrorTemplateBorder}"
IsEnabled="{Binding IsEnabled}" IsEditable="True" IsReadOnly="True" Margin="0,10,0,0">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}: {1}">
<Binding Path="Program.Name" />
<Binding Path="MaterialNumberString" />
<Binding UpdateSourceTrigger="PropertyChanged" />
<Binding NotifyOnValidationError="True" />
<Binding ValidatesOnDataErrors="True" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Thanks to this article http://www.shujaat.net/2010/08/wpf-editable-combobox-with-datatemplate.html, I was able to figure out why my ComboBox did not display correctly when an item was selected. It is because my ComboBox was set to IsEditable="True". Apparently, when using a ComboBox.ItemTemplate and having MultiBinding set, the ComboBox cannot determine which item to display so it displays the class instead.

Compound DisplayMemberPath for a combobox

I need to create a DisplayMemberPath that is a compound of a few properties (ie object.category.Name+" -> "+object.description) I'm pretty sure I can do this by creating a dynamic data type that encapsulates the object and also adds a new property called displayField that is what I need but I'm wondering if there is a more proper way to do this that does not involve creating a new object. Any ideas?
DisplayMemberPath is just a "shortcut" for when you don't need a complex template for items. If you need more control, use ItemTemplate instead:
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} -> {1}">
<Binding Path="Category.Name" />
<Binding Path="Description" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

Categories