« RadioButton » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
 
(3 versions intermédiaires par le même utilisateur non affichées)
Ligne 6 : Ligne 6 :


<kode lang='xaml'>
<kode lang='xaml'>
<ListBox SelectedIndex="{Binding Mode, Mode=TwoWay}"
<ListBox SelectedIndex="{Binding Mode, Mode=TwoWay, Converter={StaticResource EnumToNumberConverter}}"
         Style="{StaticResource HorizontalRadioButtonList}">
         Style="{StaticResource HorizontalRadioButtonList}">
     <ListBoxItem Content="Mode 1" />
     <ListBoxItem Content="Mode 1" />
Ligne 29 : Ligne 29 :
</kode>
</kode>


<kode lang='xaml'>
<kode lang='xaml' collapsed>
<Style x:Key="RadioButtonHorizontalListBox"
<Style x:Key="RadioButtonHorizontalListBox"
       TargetType="ListBox">
       TargetType="ListBox">
Ligne 54 : Ligne 54 :
         <Setter.Value>
         <Setter.Value>
             <Style TargetType="ListBoxItem">
             <Style TargetType="ListBoxItem">
                <!-- to get the same brush as the ListBox -->
                 <Setter Property="BorderBrush"
                 <Setter Property="BorderBrush"
                         Value="{Binding BorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox, AncestorLevel=1}}" />
                         Value="{Binding BorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox, AncestorLevel=1}}" />
Ligne 93 : Ligne 94 :
     </Setter>
     </Setter>
</Style>
</Style>
</kode>
<kode lang='cs'>
// binding from Int to Enum need a converter
[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;
    }
}
</kode>
</kode>

Dernière version du 30 mai 2022 à 16:38

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
// binding from Int to Enum need a converter 
[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;
    }
}