« RadioButton » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
 
 
(9 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
[[Category:WPF]]
[[Category:WPF]]
= [[ListBoxEdit#Style_RadioButton|DevExpress ListBoxEdit avec un style RadioButton]] =
= [[ListBoxEdit#Style_RadioButton|DevExpress ListBoxEdit avec un style RadioButton]] =
= [http://stackoverflow.com/questions/1317891/simple-wpf-radiobutton-binding ListBox avec un style RadioButton] =
 
= [http://stackoverflow.com/questions/1317891/simple-wpf-radiobutton-binding ListBox with RadioButton / ToogleButton item style] =
Useful to bind multiple RadioButton / ToogleButton on 1 enum property.
 
<kode lang='xaml'>
<kode lang='xaml'>
<ListBox Style="{StaticResource HorizontalRadioButtonList}" SelectedValue="{Binding Selection}">
<ListBox SelectedIndex="{Binding Mode, Mode=TwoWay, Converter={StaticResource EnumToNumberConverter}}"
     <ListBoxItem>Choix1</ListBoxItem>
        Style="{StaticResource HorizontalRadioButtonList}">
     <ListBoxItem>Choix2</ListBoxItem>
     <ListBoxItem Content="Mode 1" />
    <ListBoxItem Content="Mode 2" />
     <ListBoxItem Content="Mode 3" />
</ListBox>
</ListBox>
</kode>
</kode>


<kode lang='xaml'>
<kode lang='cs'>
<Style x:Key="RadioButtonList" TargetType="ListBox">
public Mode Mode
     <Style.Resources>
{
        <Style TargetType="Label">
     get => mode;
            <Setter Property="Padding" Value="0" />
    set => SetProperty(ref mode, value);
        </Style>
}
    </Style.Resources>


     <Setter Property="BorderThickness" Value="0" />
enum Mode
     <Setter Property="Background"      Value="Transparent" />
{
    Mode1,
     Mode2,
     Mode3
}
</kode>


     <Setter Property="ItemContainerStyle" Value="{StaticResource RadioButtonListItem}" />
<kode lang='xaml' collapsed>
<Style x:Key="RadioButtonHorizontalListBox"
      TargetType="ListBox">
     <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>


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


     <Style.Triggers>
     <Setter Property="ItemContainerStyle">
         <Trigger Property="IsEnabled" Value="False">
         <Setter.Value>
            <Setter Property="TextBlock.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
            <Style TargetType="ListBoxItem">
        </Trigger>
                <!-- to get the same brush as the ListBox -->
    </Style.Triggers>
                <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" />


</Style>
                                <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>


<Style x:Key="HorizontalRadioButtonList" BasedOn="{StaticResource RadioButtonList}" TargetType="ListBox">
                            <!-- ToggleButton -->
    <Setter Property="ItemsPanel">
                            <Grid Background="{TemplateBinding Background}">
        <Setter.Value>
                                <ToggleButton IsChecked="{TemplateBinding IsSelected}"
            <ItemsPanelTemplate>
                                              Content="{TemplateBinding Content}"
                 <VirtualizingStackPanel Background="Transparent" Orientation="Horizontal" />
                                              Padding="10,5"
             </ItemsPanelTemplate>
                                              BorderThickness="0,1,1,1"
                                              BorderBrush="{TemplateBinding BorderBrush}"
                                              Focusable="False"
                                              IsHitTestVisible="False" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                 </Setter>
             </Style>
         </Setter.Value>
         </Setter.Value>
     </Setter>
     </Setter>
Ligne 49 : Ligne 96 :
</kode>
</kode>


<kode lang='xaml'>
<kode lang='cs'>
<Style x:Key="RadioButtonListItem" TargetType="{x:Type ListBoxItem}" >
// binding from Int to Enum need a converter
    <Setter Property="Template">
[ValueConversion(typeof(Enum), typeof(int))]
         <Setter.Value>
public sealed class EnumToNumberConverter : IValueConverter
             <ControlTemplate TargetType="ListBoxItem">
{
                <DockPanel LastChildFill="True" Background="{TemplateBinding Background}"
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                          HorizontalAlignment="Stretch" VerticalAlignment="Center" >
    {
        if (value == null || targetType == null)
        {
            return DependencyProperty.UnsetValue;
        }
 
         return value.GetType().IsEnum
            ? System.Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType()), culture)
             : DependencyProperty.UnsetValue;
    }


                    <RadioButton IsChecked="{TemplateBinding IsSelected}" Focusable="False"
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                                IsHitTestVisible="False" VerticalAlignment="Center" Margin="0,0,4,0" />
    {
        if (value == null || targetType == null)
        {
            return DependencyProperty.UnsetValue;
        }


                    <ContentPresenter
        return targetType.IsEnum ? Enum.ToObject(targetType, value) : DependencyProperty.UnsetValue;
                        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>
</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;
    }
}