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)
Related
I'm binding two strings using Multibinding and StringFormat to a TextBox. I've noticed something strange when I add a Converter to one of the inner bindings, like this:
<TextBox>
<TextBox.Text>
<MultiBinding StringFormat="{}{0} {1} ">
<Binding Path="Foo"
Converter="{StaticResource someConverter}"
ConverterParameter="true" />
<Binding Path="Bar" />
</MultiBinding>
</TextBox.Text>
</TextBox>
In the converter the TargetType property is going to be object. Using the same converter directly on a Text property (without the multibinding) it is string.
I'm wondering why is that happening, and if there is any way of letting the converter know about the type of the parent binding (not sure if the terminology is correct here).
The reason I'm asking is that I'm usually checking the to/from types in the converters and I return Binding.DoNothing if there is a mismatch.
When you use the someConverter directly on a Text property, the TargetType will be String, because it's based on the Type of Text property. But when you use a converter in Binding which is placed inside MultiBinding, there is no information about TargetType and Object is used by default.
Maybe you are familiar with IMultiValueConverter, so when you use it, the TargetType will be String as you expected. Look at the example bellow:
<TextBox>
<TextBox.Text>
<MultiBinding StringFormat="{}{0} {1} " Converter="{StaticResource someMultiConverter}" ConverterParameter="true" >
<Binding Path="Foo" />
<Binding Path="Bar" />
</MultiBinding>
</TextBox.Text>
</TextBox>
I'm binding a pressure to a textbox. The pressure could be in PSI, KPA, BARS, etc. I would like to display the unit inside the textbox next to the value. There doesn't seem to be a way to bind the Units string property in my viewmodel to the StringFormat option of the value binding. Is there any way to accomplish this without retemplating the textbox?
You can use MultiBinding:
<TextBox>
<TextBox.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Pressure" />
<Binding Path="Unit"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
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>
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
I assume this is possible but not sure how to do it. I need to pass the value of a class level variable to a converter, from within side a data template.
<DataTemplate x:Key="ResponseItemTemplate">
<StackPanel Orientation="Horizontal" >
<StackPanel.Visibility>
<MultiBinding Converter="{StaticResource VisibilityConverter}">
<Binding Path="Key"/>
<Binding Path="CurrentLanguage"/>
</MultiBinding>
</StackPanel.Visibility>
<TextBox Width="200" Text="{Binding Value}" />
</StackPanel>
</DataTemplate>
The 'Key' value exists on the response item for the data template so this gets passed correctly, whereas the CurrentLanguage is a class variable and I can't get that to pass properly to the converter. Any ideas?
Thanks for the replies, this is what I needed to use in the end:
<Binding Path="DataContext.CurrentLanguage" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}"/>
You can use the binding object as follows:
<Binding Source="{x:Static local:DataObject.MyData}" />
See: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c94682e5-ad16-42f9-973f-fd7588a9c0b5.
If you define the converter as a resource, which you have, you can access it in the code behind. Once you have the converter you can then set a property on it.
var myVisConverter = (VisibilityConverter)window.Resources["VisibilityConverter"];
myVisConverter.CurrentLanguage = ...
EDIT Ok, if you're trying to get access to the parent DataContext from within the DataTemplate, there's a couple of options. Easiest is to name the control with the correct DataContext, then bind to that control like so...
<Binding Path="DataContext.CurrentLanguage" ElementName="nameGivenToElement" />
Josh Smith wrote an article with more ways of getting inherited DataContexts.