RadioButton

De Banane Atomic
Aller à la navigationAller à la recherche

DevExpress ListBoxEdit avec un style RadioButton

ListBox avec un style RadioButton

Xaml.svg
<ListBox Style="{StaticResource HorizontalRadioButtonList}" SelectedValue="{Binding Selection}">
    <ListBoxItem>Choix1</ListBoxItem>
    <ListBoxItem>Choix2</ListBoxItem>
</ListBox>

<ListBox SelectedIndex="{Binding Mode, Mode=TwoWay}"
         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="RadioButtonListItem"
       TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <DockPanel LastChildFill="True"
                           Background="{TemplateBinding Background}"
                           HorizontalAlignment="Stretch"
                           VerticalAlignment="Center">

                    <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>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="RadioButtonList"
       TargetType="ListBox">
    <Style.Resources>
        <Style TargetType="Label">
            <Setter Property="Padding" Value="0" />
        </Style>
    </Style.Resources>

    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="Transparent" />

    <Setter Property="ItemContainerStyle" Value="{StaticResource RadioButtonListItem}" />

    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="TextBlock.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
        </Trigger>
    </Style.Triggers>

</Style>

<Style x:Key="HorizontalRadioButtonList"
       BasedOn="{StaticResource RadioButtonList}"
       TargetType="ListBox">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Background="Transparent"
                                        Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>