How to bind multiple values to a single WPF TextBlock? - wpf

I'm currently using the TextBlock below to bind the value of a property named Name:
<TextBlock Text="{Binding Name}" />
Now, I want to bind another property named ID to the same TextBlock.
Is it possible to bind two or more values to the same TextBlock? Can it be done with simple concatenation, like Name + ID and, if not, how else could this be approached?

You can use a MultiBinding combined with the StringFormat property. Usage would resemble the following:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} + {1}">
<Binding Path="Name" />
<Binding Path="ID" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Giving Name a value of Foo and ID a value of 1, your output in the TextBlock would then be Foo + 1.
Note: This is only supported in .NET 3.5 SP1 and 3.0 SP2 or later.

I know this is a way late, but I thought I'd add yet another way of doing this.
You can take advantage of the fact that the Text property can be set using "Runs", so you can set up multiple bindings using a Run for each one. This is useful if you don't have access to MultiBinding (which I didn't find when developing for Windows Phone)
<TextBlock>
<Run Text="Name = "/>
<Run Text="{Binding Name}"/>
<Run Text=", Id ="/>
<Run Text="{Binding Id}"/>
</TextBlock>

If these are just going to be textblocks (and thus one way binding), and you just want to concatenate values, just bind two textblocks and put them in a horizontal stackpanel.
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding ID}"/>
</StackPanel>
That will display the text (which is all Textblocks do) without having to do any more coding. You might put a small margin on them to make them look right though.

Use a ValueConverter
[ValueConversion(typeof(string), typeof(String))]
public class MyConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Format("{0}:{1}", (string) value, (string) parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
and in the markup
<src:MyConverter x:Key="MyConverter"/>
.
.
.
<TextBlock Text="{Binding Name, Converter={StaticResource MyConverter Parameter=ID}}" />

Related

What does a WPF DataGrid bind to the headers DataTemplate?

When creating a WPF DataGrid, I have the option to set a DataTemplate to each column's HeaderTemplate, like this:
<DataGridTextColumn Binding="{Binding Name}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<!-- just some random content, not important, but notice the bindings use RelativeSource-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name" VerticalAlignment="Center"/>
<Button Command="{Binding Path=DataContext.SortFoldersByNameCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" Content="▲"
Foreground="{Binding Path=DataContext.IsFoldersSortByName, Converter={StaticResource EnabledToBrushConverter}, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
Now, notice that for my bindings to work inside that template, I need to set a RelativeSource, because this template is not bound to the grid's DataContext anymore.
The question is: what is bound to the header's DataTemplate?
If I simply try to put a <TextBlock Text="{Binding}"/>, for instance, the text is empty. I don't know what other kinds of tests I could do to find out.
The short answer is that the datacontext is nothing.
I threw a quick template into some datagrid I have on disk.
I used this thing to answer a few questions years back.
The header isn't in the visual tree and does not inherit datacontext.
My quick and dirty header template:
<DataGridTextColumn Binding="{Binding LastName}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="XXXX"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
Examine that textblock using Snoop...
Datacontext is empty:
This is why you do all that relativesource stuff to get to your property in the datacontext.
As an aside.
If you're doing much wpf development I recommend you give Snoop a go.
It's still way better than the in built thing.
Besides using Snoop (great tool!), you can also check that by adding a converter to the binding and inspecting it with a breakpoint. (The inspection is necessary if the binding is null, which is the case)
The Path=. indicates the current DataContext.
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=., Converter={local:TestConverter}}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
Where the converter is defined as:
class TestConverter : MarkupExtension, IValueConverter
{
//source to target
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.GetType().FullName;
}
//target to source
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}

Bind DataContext Index to Slider Control Value

Is it possible to use the current value of a WPF Slider control (Slider.Value) as an input to a Indexing Binding on another control?
Use case: A collection of items is set as the DataContext for a control, and the slider is used to select which item from a collection is displayed.
<Slider x:Name="selector" Minimum="1" Maximum="{Binding Count}"/>
<!-- How to grab the value of selector and use as indexer?? -->
<StackPanel DataContext="{Binding [??????]}">
<TextBlock Text="{Binding name}"/>
<TextBlock Text="{Binding job}" />
</StackPanel>
Is it possible to use the current value of a WPF Slider control (Slider.Value) as an input to a Indexing Binding on another control?
No, not directly. ?????? in {Binding [??????]} has to be a compile-time constant.
You could bind to both the DataContext and the Value property of the Slider and use a converter to perform the lookup though:
public class MultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var yourDataContext = values[0] as IDictionary<double, object>; //cast to whatever the type of your DataContext is
double value = (double)values[1];
return yourDataContext[value];
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
XAML:
<StackPanel>
<StackPanel.DataContext>
<MultiBinding>
<MultiBinding.Converter>
<local:MultiConverter />
</MultiBinding.Converter>
<Binding Path="." />
<Binding Path="Value" ElementName="selector" />
</MultiBinding>
</StackPanel.DataContext>
<TextBlock Text="{Binding name}"/>
<TextBlock Text="{Binding job}" />
</StackPanel>
Declare two dependency properties on your data context: SliderIndex and SelectedItem (you can name them whatever you want, but those are the names I'll use for my answer).
Bind Slider.Value to SliderIndex. Then use a PropertyChangedCallback to update the SelectedItem property based on the new value of SliderIndex. Finally, bind StackPanel.DataContext to SelectedItem.
This is the best way I know of to do this. There is no easy way to bind the two directly since you can't use a variable as an index for collection binding. The other option is to use an IValueConverter or IMultiValueConverter, but the above is cleaner in my opinion.

WPF DataBinding where the Path is recovered from the object?

I have an object with several properties. Two of these are used to control the width and height of the target text box. Here is a simple example...
<DataTemplate DataType="{x:Type proj:SourceObject}">
<TextBox Width="{Binding ObjWidth}" Height="{Binding ObjHeight}"/>
</DataTemplate>
I also want to bind the Text property of the TextBox. The actual property to bind against is not fixed but instead is named in a field of the SourceObject. So ideally I would want to do this...
<DataTemplate DataType="{x:Type proj:SourceObject}">
<TextBox Width="{Binding ObjWidth}" Height="{Binding ObjHeight}"
Text="{Binding Path={Binding ObjPath}"/>
</DataTemplate>
Here the ObjPath is a string that returns a path that would be perfectly valid for the binding. But this does not work because you cannot use a binding against the Binding.Path. Any ideas how I can achieve the same thing?
For more context I will point out that the SourceObject is user customizable and hence the ObjPath can be updated over time and hence I cannot simply put a fixed path in the data template.
You could implement an IMultiValueConverter and use this one as BindingConverter for your Text Property. But then you have the problem, that the value of the Textbox is only updated if your ObjPath property changes (the path itself), not the value where the path is pointing to. If that's, okay you can go with a BindingConverter which returns the value of your binding Path using Reflection.
class BindingPathToValue : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value[0] is string && value[1] != null)
{
// value[0] is the path
// value[1] is SourceObject
// you can use reflection to get the value and return it
return value[1].GetType().GetProperty(value.ToString()).GetValue(value[1], null).ToString();
}
return null;
}
public object[] ConvertBack(object value, Type[], object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Have the converter in your Resources:
<proj:BindingPathToValue x:Key="BindingPathToValue" />
and use it in XAML:
<DataTemplate DataType="{x:Type proj:SourceObject}">
<TextBox Width="{Binding ObjWidth}" Height="{Binding ObjHeight}">
<TextBox.Text>
<MultiBinding Mode="OneWay" Converter="{StaticResource BindingPathToValue}">
<Binding Mode="OneWay" Path="ObjPath" />
<Binding Mode="OneWay" Path="." />
</MultiBinding>
</TextBox.Text>
</TextBox>
</DataTemplate>

How to boolean && two visibility converters

I have two separate converters for visibility, one based on whether a field has been updated and one based on whether a field is allowed to be seen. I use the updatedField one for each text item on my page so that a star shows up next to an updated field. But some text items only are visible to some users based on permission levels.
For example:
<Image Visibility="{Binding ElementName=MyObject, Path=UpdatedFields, Mode=OneWay, Converter={StaticResource updatedFieldConverter}, ConverterParameter=FieldToTest}" Source="Properties:Resources.star_yellow" />
and
<TextBlock FontSize="21" Foreground="{DynamicResource LabelBrush}" Text="{x:Static Properties:Resources.Some_Text}" Visibility="{Binding Source={StaticResource allowedFields}, Path=Some_Text_Field, Converter={StaticResource visibilityConverter}}" />
My problem is that for the case of the permission-required fields I need to run both converters to determine if the star shows up. Is there a way to do a boolean "And" on the results of two converters?
I looked at this post but it doesn't seem to allow for different sets of parameters to be passed into to the two different converters.
-------Update--------
I also tried to create a MultiValueConverter with this xaml
<Image Grid.Row="4" Grid.Column="0" Source="star_yellow.png">
<Image.Visibility>
<MultiBinding Converter="{StaticResource combinedVisibilityConverter}" ConverterParameter="FieldToTest" >
<Binding ElementName="allowedFieldsModel" Path="Some_Text_Field" Mode="OneWay" />
<Binding ElementName="MyObject" Path="UpdatedFields" Mode="OneWay" />
</MultiBinding>
</Image.Visibility>
</Image>
But when it enters the converter both values are "DependencyProperty.UnsetValue". So I'm apparently doing something wrong here.
--------Solution---------
I had to modify to this, but then it worked.
<Image.Visibility>
<MultiBinding Converter="{StaticResource combinedVisibilityConverter}" ConverterParameter="FieldToTest">
<Binding Source="{StaticResource allowedFieldsModel}" Path="Some_Text_Field" />
<Binding Path="MyObject.UpdatedFields" />
</MultiBinding>
</Image.Visibility>
You could use a MultiBinding together with a short, hand made IMultiValueConverter.
Example:
<StackPanel>
<StackPanel.Resources>
<local:MultiBooleanToVisibilityConverter x:Key="Converter" />
</StackPanel.Resources>
<CheckBox x:Name="Box1" />
<CheckBox x:Name="Box2" />
<TextBlock Text="Hidden Text">
<TextBlock.Visibility>
<MultiBinding Converter="{StaticResource Converter}">
<Binding ElementName="Box1"
Path="IsChecked" />
<Binding ElementName="Box2"
Path="IsChecked" />
</MultiBinding>
</TextBlock.Visibility>
</TextBlock>
</StackPanel>
... and the converter ...
class MultiBooleanToVisibilityConverter : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
bool visible = true;
foreach (object value in values)
if (value is bool)
visible = visible && (bool)value;
if (visible)
return System.Windows.Visibility.Visible;
else
return System.Windows.Visibility.Hidden;
}
public object[] ConvertBack(object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Late to the party here but an easier solution is to just wrap the control in another control. I prefer this to having lots of Converters that do different things.
<Border Visibility="{Binding Value1, Converter={convertersDF:Converter_ValueToVisibility}}">
<ComboBox Visibility="{Binding Value2, Converter={convertersDF:Converter_ValueToVisibility}}"/>
</Border>
One thing that came to mind is, perhaps, instead of two different boolean fields, a single bit field created by ORing together updatedField and allowedField. Then you can have three value converters, all operating on the same field.
Or just calculate another field in your data model that does the ANDing there. That's probably more efficient (in terms of runtime).
You could pass an array of two objects to the converter in the ConverterParameter - constructing the array in XAML.

Is it possible to add more characters after a binding in xaml?

I was wondering something, and couldn't find any relevant topics. I have following binding :
Content="{x:Static resx:Resource.Form_OtherOption_Description}"
This will place a string in a label. What i was asking myself is if i can add a ":" after that binding, not in code, just in xaml. The label represent something like "Name :". But adding the ":" as part of the binding is not an option.
Edit
I'm working in 3.5 version
Any suggestions.
Thanks in advance.
You could accomplish this with something like:
<TextBlock Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description},
StringFormat={}{0}:}" />
Edit: <Label>s Content property does not respect the StringFormat property of a binding apparently. Which I've found has been moved to the ContentStringFormat property on the <Label>.
<Label Content="{x:Static resx:Resource.Form_OtherOption_Description}"
ContentStringFormat="{}{0}:" />
If you're using WPF 4.0, you could also do this:
<TextBlock>
<Run Text="{Binding SomeLabel}"/>
<Run Text=":"/>
</TextBlock>
This actually concatenates the two strings coming from two Run tag and copied into TextBlock.Text property!.
Using this approach you can even bind to different properties in presenter, and display it in a single TexBlock. See this excellent example:
Can we concat two properties in data binding?
You can also use MultiBinding with StringFormat e.g:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="ID {0} Name: {1} Age: {2}">
<Binding Source="{x:Static resx:SomeResx.ID}"/>
<Binding Path="Name"/>
<Binding Path="Age"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
You can use this in a content control TextBlock TextBlock.Text (sorry I couldn't get the code to show up for this above)
Yes you can. Here I add "testing" after binding text(clouds.all) in windows phone.
<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}testing}"/>
Try Binding's property StringFormat - it can do very simply what you want.
if you use a label inside a progress bar you can use this way:
<Label x:Name="Progress" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontWeight="Bold" Foreground="White" Opacity=".7"
Content="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}}" ContentStringFormat="{}{0}%">
in this way you can visualize the value of progressbar with a % added.
You can create a converter that takes the input string and adds the ":".
public class AddStringToStringConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string input = value as string;
string suffix = parameter as string;
return input + suffix;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Xaml:
<Window.Resources>
<local:AddStringToStringConverter x:Key="AddStringToStringConverter"/>
</Window.Resources>
...
<Label Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description}, Converter={StaticResource AddStringToStringConverter}, ConverterParameter=:}"/>
Or something like that. Tried it and it worked for my source at least.
If you have whitespace and the like in you ConverterParameter you can use signle quotes to make sure it does not get disposed.
Edit: Oh right... yeah... there's also StringFormat which i have never needed before, ehehehe...

Resources