RadioButton

De Banane Atomic
Aller à la navigationAller à la recherche

DevExpress ListBoxEdit avec un style RadioButton

ListBox with RadioButton / ToogleButton item style

Useful to bind multiple RadioButton / ToogleButton on 1 enum property.

Xaml.svg
<ListBox SelectedIndex="{Binding Mode, Mode=TwoWay, Converter={StaticResource EnumToNumberConverter}}"
         Style="{StaticResource HorizontalRadioButtonList}">
    <ListBoxItem Content="Mode 1" />
    <ListBoxItem Content="Mode 2" />
    <ListBoxItem Content="Mode 3" />
</ListBox>
Cs.svg
public Mode Mode
{
    get => mode;
    set => SetProperty(ref mode, value);
}

enum Mode
{
    Mode1,
    Mode2,
    Mode3
}
Xaml.svg
<Style x:Key="RadioButtonHorizontalListBox"
       TargetType="ListBox">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Border BorderThickness="1,0,0,0"
                        BorderBrush="{TemplateBinding BorderBrush}">
                    <ItemsPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListBoxItem">
                <!-- to get the same brush as the ListBox -->
                <Setter Property="BorderBrush"
                        Value="{Binding BorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox, AncestorLevel=1}}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <!-- RadioButton -->
                            <DockPanel Background="{TemplateBinding Background}">

                                <RadioButton IsChecked="{TemplateBinding IsSelected}"
                                             Focusable="False"
                                             IsHitTestVisible="False"
                                             VerticalAlignment="Center"
                                             Margin="0,0,4,0" />

                                <ContentPresenter Content="{TemplateBinding ContentControl.Content}"
                                                  ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                                                  ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                                                  HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                                  VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                                                  SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                            </DockPanel>

                            <!-- ToggleButton -->
                            <Grid Background="{TemplateBinding Background}">
                                <ToggleButton IsChecked="{TemplateBinding IsSelected}"
                                              Content="{TemplateBinding Content}"
                                              Padding="10,5"
                                              BorderThickness="0,1,1,1"
                                              BorderBrush="{TemplateBinding BorderBrush}"
                                              Focusable="False"
                                              IsHitTestVisible="False" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>
Cs.svg
[ValueConversion(typeof(Enum), typeof(int))]
public sealed class EnumToNumberConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || targetType == null)
        {
            return DependencyProperty.UnsetValue;
        }

        return value.GetType().IsEnum
            ? System.Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType()), culture)
            : DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || targetType == null)
        {
            return DependencyProperty.UnsetValue;
        }

        return targetType.IsEnum ? Enum.ToObject(targetType, value) : DependencyProperty.UnsetValue;
    }
}