Simple Value / Index for Static ComboBox in WPF

Go To StackoverFlow.com

0

So I just want to make a simple combo box that has 2 values, true and false. The catch is the item I want to bind to should receive a "1" for true and a "0" for false. I am trying to accomplish with pure xaml if possible (converter would be acceptable, but I have thus not been able to get it to work). Here's my code...

            <ComboBox SelectedItem="{Binding Criteria,Converter={StaticResource BoolToIntConverter}}">
                <ComboBox.Items>
                    <ComboBoxItem Name="True" Content="True" />
                    <ComboBoxItem Name="False" Content="False" />
                </ComboBox.Items>
            </ComboBox>

The converter just looks like this...

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ComboBoxItem cbo = new ComboBoxItem();

        if (value.ToString() == "1")
            cbo.Name = "True";
        else
            cbo.Name = "False";
        return cbo;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value.ToString().Contains("True"))
            return 1;
        return 0;
    }

It works in the sense that if I select True "1" is set correctly and vice versa, however it also needs to set the current item to True / False if the property is changed to 1 or 0 by another place in the code. Inotifyproperty changed is setup correctly on the bound string, I can break point the covert being called, but the item does not change. Is there a simple way to make a value / index relationship with a combo box in purely xaml (I don't want to have to use a backing object to achieve this). If not is there a way to correct my converter to get the behavior I'm looking for?

2012-04-05 21:45
by Kevin DiTraglia
You're making this harder on yourself by avoiding MVVM, that pattern was cut out for things like this - KodeKreachor 2012-04-05 22:11
I just feel like using MVVM just to ensure True means 1 and false means 0 is a bit overkill, but if I must I suppose I will - Kevin DiTraglia 2012-04-06 13:32


1

It is possible to apply a control template on a checkbox to make it display as a combobox. Example of how this can be done in XAML follows:

<CheckBox Content="Select" Width="100" Height="22">
        <CheckBox.Style>
            <Style TargetType="{x:Type CheckBox}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <ComboBox>
                                <ComboBoxItem x:Name="TrueComboBoxItem" Content="True" IsSelected="{Binding IsChecked, RelativeSource={RelativeSource AncestorType=CheckBox}, UpdateSourceTrigger=PropertyChanged}" />
                                <ComboBoxItem x:Name="FalseComboBoxItem" Content="False" />

                            </ComboBox>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>                     
        </CheckBox.Style>
    </CheckBox>

I have verified in Snoop that this works.

Snoop

2012-04-22 03:17
by Tore Aurstad
Ads