Selection checkbox with triggers? - wpf

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.

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>

Binding a unit label in WPF 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>

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!

Problem changing the Foreground colour of a WPF DataGrid Cell using MultiBinding

I am trying to change the Foreground colour of a DataGridCell, NOT the entire row, given the value of the specific cell contents. For example, if the date value of the cell is out of date then I want to set the Foreground to red. I am almost where I am using MultiBinding to extract two values from the data grid and parsing it through value converter which implements IMultiValueConverter.
In short, and without then need to go into the converter itself, I have written the following XAML:
<GlobalTasks:CellHighlighterConverter x:Key="cellHighlighterConverter"/>
<Style x:Key="CellHighlighterStyle">
<Setter Property="my:ExtendedDataGrid.Foreground">
<Setter.Value>
<MultiBinding Converter="{StaticResource cellHighlighterConverter}">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource self}"/>
<Binding Path="Row" Mode="OneWay"/>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
I then added to my Datagrid : CellStyle="{StaticResource CellHighlighterStyle}".
Now, my converter sees the first Value[0] property as the DataGridCell but the second value is some sort of made up DependencyProperty. It’s like it has created a default or dummy property when I actually want to get the row. In fact, I would really like to get the class object associated to that row. My DataGrid is bound to a List<of a class object>.
I am sure that I am inches away. How do I parse either the DataGrid Row so that I can extract the value of the field, or the object bound to that row or column itself in order for me to return the colour I want to return? OR, is there a better way for me to selectively alter the properties of a DataGrid Cell given the value?
The DataContext of the cell should be the data-object of the row, try changing the second binding to this:
<Binding />
Which binds directly to the DataContext.

Resources