Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
375
Alternating background style for the XamComboEditor
posted

How can I create a style for the XamComboEditor so it has alternating backbround like the XamDataGrid?

  • 54937
    Suggested Answer
    Offline posted

    Another option if you are using .net 3.5 sp1 or later is to provide a custom style for the contained ComboBox and set its AlternationCount and use the AlternationIndex on the comboboxitem's style. e.g.

    <igEditors:XamComboEditor>
        <igEditors:XamComboEditor.ComboBoxStyle>
            <Style TargetType="ComboBox">
                <Style.Resources>
                    <AlternationConverter x:Key="BackgroundConverter">
                        <SolidColorBrush Color="Blue" />
                        <SolidColorBrush Color="Red" />
                    </AlternationConverter>
     
                    <Style TargetType="ComboBoxItem" x:Key="alternatingItem">
                        <Setter Property="Background" 
                            Value="{Binding RelativeSource={RelativeSource Self},
                            Path=(ItemsControl.AlternationIndex),
                            Converter={StaticResource BackgroundConverter}}"/>
                    </Style>
                </Style.Resources>
                
                <Setter Property="ItemContainerStyle" Value="{StaticResource alternatingItem}" />
                <Setter Property="AlternationCount" Value="2" />
            </Style>
        </igEditors:XamComboEditor.ComboBoxStyle>
        
        <igEditors:XamComboEditor.ItemsProvider>
            <igEditors:ComboBoxItemsProvider>
                <sys:String>ABC</sys:String>
                <sys:String>DEF</sys:String>
                <sys:String>GHI</sys:String>
                <sys:String>JKL</sys:String>
            </igEditors:ComboBoxItemsProvider>
        </igEditors:XamComboEditor.ItemsProvider>
    </igEditors:XamComboEditor>

     

  • 69686
    Suggested Answer
    posted

    Hello,

    I think there is no direct way of achieving this, but you can use a IMultiValueConverter and see at which position is the ComboBoxItem inside the XamComboEditor and change the background according to that, like so:

    <igEditors:XamComboEditor  Name="xamComboEditor1">

                <igEditors:XamComboEditor.Resources>

                    <local:MyConverter x:Key="conv"/>

                    <Style TargetType="{x:Type ComboBoxItem}">

                        <Setter Property="Background" >

                            <Setter.Value>

                                <MultiBinding Converter="{StaticResource conv}">

                                    <MultiBinding.Bindings>

                                        <Binding />

                                        <Binding Path="ItemsProvider" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type igEditors:XamComboEditor}}" />

                                    </MultiBinding.Bindings>

                                </MultiBinding>

                            </Setter.Value>

                        </Setter>

                    </Style>

                </igEditors:XamComboEditor.Resources>

            </igEditors:XamComboEditor>

    and the convert method:

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)

            {

                if (values == null)

                    return Binding.DoNothing;

                else

                {

                    ComboBoxItemsProvider pr = values[1] as ComboBoxItemsProvider;

                    if (pr.Items.IndexOf(values[0]) % 2 == 0)

                        return Brushes.Orange;

                    else

                        return Brushes.OldLace;

                }

            }