Binding a unit label in WPF textbox - wpf

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>

Related

Dynamic binding to any tab control - WPF

I am trying to set the max width of the grid of a view, however I want to be able to put this view anywhere within any tab control, as the width of the grid is determined by the tab control it's within. I don't want to have to specify the specific tab control by name because I want it to work with any tab control.
Does anyone know how I can change the below XAML code? This is in the WeUserControl view
<Grid.MaxWidth>
<MultiBinding Converter="{StaticResource WeMaxWidthConverter}">
<Binding ElementName="mainWindowTabControl" Path="ActualWidth"/>
</MultiBinding>
</Grid.MaxWidth>
In the view that it is being used in, it is literally just:
<views:WeUserControl Grid.Column="0" Grid.Row="1" />
and that is encapsulated in a tab control, in this case, called mainWindowTabControl
It's just the tabcontrol you're looking for and the grid will be within the visual tree of this.
The relativesource binding would be:
<Binding Path="ActualWidth"
RelativeSource="{RelativeSource AncestorType={x:Type TabControl}}" />
If you add a public property called "TabControl" to the parent window that returns a reference to the TabControl, you should be able to bind to it like this from any child element of the window:
<Binding Path="TabControl.ActualWidth"
RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}" />

MultiBinding with StringFormat - Why is the targetType == object in the inner converter?

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>

Using CommandParameters and MultiBindings?

Is it possible to use CommandParameter="{Binding}" in a multi binding?
I am trying to do this in a data grid.
<CheckBox.CommandParameter>
<MultiBinding Converter="{StaticResource CDetailConverter}">
<Binding Path ="IsChecked" ElementName="chkSelection"/>
<Binding ConverterParameter="{Binding}"/>
</MultiBinding>
</CheckBox.CommandParameter>
The second Binding throws an error.
In a nutshell, the answer is no.
In your second inner Binding you have set ConverterParameter. There are a couple of problems with this:
First, Binding is its own class separate from MultiBinding with both Converter and ConverterParameter properties. Here you have set the ConverterParameter property without setting the Converter property. Remember that ConverterParameter is passed to the Binding's specified converter regardless if it is used within a MultiBinding or not. If you were to add a Converter here, then the converter would be passed the specified ConverterParameter.
What you probably meant to do was set the ConverterParameter on the outer MultiBinding which also has this property:
<CheckBox.CommandParameter>
<MultiBinding Converter="{StaticResource CDetailConverter}" ConverterParameter="{Binding }">
<Binding Path ="IsChecked" ElementName="chkSelection"/>
</MultiBinding>
</CheckBox.CommandParameter>
If you try this, you will quickly see that ConverterParameter can not be the target of a Binding expression since it is not a DependencyProperty.
Since you can not bind to CommandParameter, the typical workaround is to modify your IMultiConverter to accept an additional value, and supply this value through a binding expression:
<CheckBox.CommandParameter>
<!-- CDetailConverter updated to expect an additional value in the values array -->
<MultiBinding Converter="{StaticResource CDetailConverter}">
<Binding Path ="IsChecked" ElementName="chkSelection"/>
<Binding />
</MultiBinding>
</CheckBox.CommandParameter>
Hope this helps!

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)

Selection checkbox with triggers?

I have this control (see picture). I like when check one option in this control, using styles or with triggers other option enable or disable. This is valid or I have other option for do that?
alt text http://img524.imageshack.us/img524/5819/combos.jpg
Hard to say without seeing your XAML, but you could bind each CheckBox's IsChecked property to the same thing:
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource MyConverter}">
<Binding Path="."/>
<Binding Path="SelectedItem" RelativeSource="..."/>
</MultiBinding>
</CheckBox.IsChecked>
The converter (IMultiValueConverter) would then determine whether the first value matches the selected value, and return true/false accordingly.

Resources