IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Xaml usercontrol : un RadioButton avec un style qui ne fonctionne pas


Sujet :

Windows Presentation Foundation

  1. #1
    Membre habitué
    Inscrit en
    Octobre 2004
    Messages
    352
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 352
    Points : 136
    Points
    136
    Par défaut Xaml usercontrol : un RadioButton avec un style qui ne fonctionne pas
    Bonjour,

    J'essaie de customiser un RadioButton, l'idée général : j'aimerai un radio button carré avec un fond en couleur (paramétrable quand on utilise le composant), et quand il est sélectionné j'affiche 2 carrés autour (le rouge extérieur et le blanc intérieur).
    Pour le réaliser, je me suis inspiré d'un bout de code trouvé sur le net, mais j'ai soit la couleur de fond mais mon radioButton ne se sélectionne pas quand je clic, soit il se sélectionne mais la couleur de fond n'est pas affiché.

    Code du user control :
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    <UserControl x:Class="AmemSalaShap.RadioImage"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:AmemSalaShap"
                 mc:Ignorable="d" 
                 >
        <Grid>
            <RadioButton GroupName="grpPalette" >
                <RadioButton.Style>
                    <Style TargetType="{x:Type RadioButton}" >
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type RadioButton}">
     
                                    <BulletDecorator Cursor="Hand">
                                        <BulletDecorator.Bullet>
                                            <Grid Height="15" Width="15" Background="AliceBlue" >
                                                <!--Define size of the Bullet-->
                                                <!--The two borders-->
                                                <Border Name="RadioMark" BorderBrush="Red" BorderThickness="1" CornerRadius="0" Visibility="Visible" >
                                                    <Border BorderBrush="White" BorderThickness="1"  >
                                                        <Border Name="RadioFond" Background="{TemplateBinding Background}" Visibility="Visible">
                                                        </Border>
                                                    </Border>
                                                </Border>
                                            </Grid>
                                        </BulletDecorator.Bullet>
                                    </BulletDecorator>
     
                                    <!--If item is checked, trigger the visibility of the mark-->
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsChecked" Value="true">
                                            <!--If item is checked, trigger the visibility of the mark
    and change the color of the selected bullet into a darker gray for better highlighting-->
                                            <Setter TargetName="RadioFond" Property="Visibility" Value="Visible"/>
                                            <Setter TargetName="RadioMark" Property="Visibility" Value="Visible"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </RadioButton.Style>
            </RadioButton>
        </Grid>
    </UserControl>

    J'intègre ce contrôle dans un autre user control de cette manière :
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    xmlns:jpeControl="clr-namespace:AmemSalaShap"
     
    […]
     
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2">
                        <BulletDecorator Background="BlueViolet"/>
                    </jpeControl:RadioImage>

    Dans cet exemple, la couleur de fond s'affiche bien mais les 2 carrés de sélection ne s'affichent pas quand je clic dessus, et c'est bien ce point que je ne comprend pas.

    Est-ce que vous savez comment faire ?

    Merci

    PS Bonus : Ensuite, je dois trouver un moyen de récupérer la propriété couleur du backgroud de mon radio button.

    édit: voici la source qui m'a inspiré :
    http://wpfstyles.blogspot.com/2011/0...iobuttons.html

  2. #2
    Membre habitué
    Inscrit en
    Octobre 2004
    Messages
    352
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 352
    Points : 136
    Points
    136
    Par défaut
    Bonjour,

    J'ai essayé plusieurs choses, j'ai réussis à résoudre mon problème de cadre de sélection, voici le code :

    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    <UserControl x:Class="AmemSalaShap.RadioImage"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:AmemSalaShap"
                 mc:Ignorable="d" 
                 >
        <Grid>
            <RadioButton GroupName="grpPalette" >
                <RadioButton.Style>
     
     
                    <Style TargetType="{x:Type RadioButton}" >
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type RadioButton}">
                                    <BulletDecorator Cursor="Hand">
                                        <BulletDecorator.Bullet>
                                            <Grid Height="15" Width="15"  >
                                                <!--Define size of the Bullet-->
     
                                                <!--The two borders-->
                                                <Border Name="RadioColor" Background="{Binding Background}" Visibility="Visible"/>
                                                <Border Name="RadioOuter" Background="Transparent" BorderBrush="Gainsboro" BorderThickness="0">
                                                    <Border Name="RadioInner"  BorderBrush="White" BorderThickness="0"/>
                                                </Border>
                                            </Grid>
                                        </BulletDecorator.Bullet>
                                    </BulletDecorator>
                                    <!--If item is checked, trigger the visibility of the mark-->
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsChecked" Value="true">
     
                                            <!--If item is checked, trigger the visibility of the mark
    and change the color of the selected bullet into a darker gray for better highlighting-->
                                            <Setter TargetName="RadioOuter" Property="BorderBrush" Value="Red" />
                                            <Setter TargetName="RadioOuter" Property="BorderThickness" Value="1" />
                                            <Setter TargetName="RadioInner" Property="BorderThickness" Value="1" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
     
                </RadioButton.Style>
            </RadioButton>
        </Grid>
    </UserControl>

    Il ne me reste plus qu'à trouver comment je vais récupérer la couleur ? En effet, ce user control est inclus dans un autre user control qui comprend beaucoup de ces radio button custom, l'idée c'est que le composant qui englobe tous mes radio button custom me retourne une seule valeur*: celle du radio button sélectionné. Pour l'instant je ne voit pas comment faire

    édit : et voici l'appel
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Blue"/>

    édit : Et voici comment se présente le user control qui contient les radio button :
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <UserControl x:Class="AmemSalaShap.ColorPalette"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:AmemSalaShap"
                 xmlns:jpeControl="clr-namespace:AmemSalaShap"
                 mc:Ignorable="d" 
                 >
        <Grid>
     
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="White"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="AliceBlue"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="AntiqueWhite"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Aqua"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Aquamarine"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Azure"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Beige"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Bisque"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Black"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="BlanchedAlmond"/>
                </StackPanel>
     
                <StackPanel Orientation="Horizontal">
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Blue"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="BlueViolet"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Brown"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="BurlyWood"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="CadetBlue"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Chartreuse"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Chocolate"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Coral"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="CornflowerBlue"/>
                    <jpeControl:RadioImage Height="15" Width="15" Margin="2" Background="Cornsilk"/>
                </StackPanel>
            </StackPanel>
        </Grid>
    </UserControl>

    C'est lui qui doit me retourner la valeur du radio button sélectionné. J'espère que c'est possible ?

  3. #3
    Membre habitué
    Inscrit en
    Octobre 2004
    Messages
    352
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 352
    Points : 136
    Points
    136
    Par défaut
    Bonjour,

    Je n'ai toujours pas réussi à lier la propriété de mon UC.

    Rappel : Mon UC RadioImage possède une couleur de background. Il est placé sur un autre UC ColorPalette qui lui même est placé dans une Window.

    La Window doit connaitre la couleur de background de mon UC RadioImage, même si c'est sous forme de string par exemple, car un composant Image sur la window doit récupérer le background de mon UC RadioImage pour son propre background.

    Dans un premier temps, j'aimerai savoir si c'est possible ? Et si c'est le cas, comment faire ça ?

    Merci

  4. #4
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour

    C'est lui qui doit me retourner la valeur du radio button sélectionné. J'espère que c'est possible ?
    Oui c'est possible comme l'illustre exemple code qui suit où j'ai simplifié ton usercpntrol RadioImage à dessin pour faciliter la compréhension de la problématique générée par un "empilement" de users controls...

    Car il faut des "wrappers" pour chaque DP du radiobutton qu'on veut exposer à l'extérieur du usercontrol RadioImage....
    - CheckState
    - RadioName
    - GroupName
    De plus pour savoir quel radiobutton a été "checké",il faut :
    - un "wrapper" OptionChanged de l'event RadioButton_Checked dans le usercontrol RadioImage
    -et un autre "wrapper" OptionExternChanged dans le usercontrol UserControl (container) qui l'expose à l'extérieur (il prolonge OptionChanged )

    Moyennant ces aménagements le problème se trouve solutionné...

    code xaml du control RadioImage:
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <UserControl x:Class="WpfRadioButtons.RadioImage"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfRadioButtons"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300"
                 >
     
        <Grid> 
            <RadioButton 
                x:Name="rd"
                Background="{Binding Path=Background, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:RadioImage} }}" 
                IsChecked="{Binding Path=CheckState, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:RadioImage} }}" 
                Checked="RadioButton_Checked"
                HorizontalAlignment="Center"
                VerticalAlignment="Center">
     
            </RadioButton>
        </Grid>
    </UserControl>

    et son code behind .cs:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    namespace WpfRadioButtons
    {
        /// <summary>
        /// Logique d'interaction pour RadioImage.xaml
        /// </summary>
        public partial class RadioImage : UserControl
        {
            public event EventHandler<RadioImageArgs> OptionChanged;
            public RadioImage()
            {
                InitializeComponent();
            }
            private void RadioButton_Checked(object sender, RoutedEventArgs e)
            {
                RadioButton radio = sender as RadioButton;
                radio.Background = this.Background;
                EventHandler<RadioImageArgs> h = OptionChanged;
                if (h != null)
                    h(this, new RadioImageArgs(radio, radio.IsChecked));
            }
            // DP "wrappers"
            public bool? CheckState
            {
                get { return (bool?)GetValue(CheckStateProperty); }
                set { SetValue(CheckStateProperty, value); }
            }
     
            // Using a DependencyProperty as the backing store for CheckState.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty CheckStateProperty =
                DependencyProperty.Register("CheckState", 
                typeof(bool?),
                typeof(RadioImage ),
                new UIPropertyMetadata(null));
     
            public string RadioName
            {
                get { return (string)GetValue(RadioNameProperty); }
                set { SetValue(RadioNameProperty, value); }
            }
     
            // Using a DependencyProperty as the backing store for RadioName.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty RadioNameProperty =
                DependencyProperty.Register("RadioName", 
                typeof(string), 
                typeof(RadioImage),
                new UIPropertyMetadata(null, OnRadioNameChanged));
            private static void OnRadioNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
            {
                RadioImage ctl = d as RadioImage;
                string name = args.NewValue as string;
                if (!string.IsNullOrEmpty(name))
                    ctl.rd.Name = name;
            }
     
     
            public string GroupName
            {
                get { return (string)GetValue(GroupNamePropertyProperty); }
                set { SetValue(GroupNamePropertyProperty, value); }
            }
     
            // Using a DependencyProperty as the backing store for GroipNameProperty.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty GroupNamePropertyProperty =
                DependencyProperty.Register("GroupNameProperty",
                typeof(string), 
                typeof(RadioImage ),
                new UIPropertyMetadata(null, OnGroupNameChanged));
     
            private static void OnGroupNameChanged(DependencyObject d,DependencyPropertyChangedEventArgs args)
            {
                RadioImage ctl = d as RadioImage;
                string grp = args.NewValue as string;
                if (! string.IsNullOrEmpty(grp))
                    ctl.rd.GroupName = grp;
            }
     
     
     
     
        }
    }
    ode xaml du control "container" UserControl1:

    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    <UserControl x:Class="WpfRadioButtons.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:WpfRadioButtons" 
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300"
                 >
        <Grid Name="grid">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <local:RadioImage 
                        RadioName="RBOneA"
                        GroupName="groupe1"
                        CheckState="True"
                        Height="25" Width="25" Margin="2" Background="Magenta"
                        OptionChanged="rb_OptionChanged"
                        />
                    <local:RadioImage 
                        RadioName="RBOneB"
                        GroupName="groupe1"
                        Height="25" Width="25" Margin="2" Background="Blue"
                        OptionChanged="rb_OptionChanged"/>
                    <local:RadioImage 
                        RadioName="RBOneC"
                        GroupName="groupe1"
                        Height="25" Width="25" Margin="2" Background="AntiqueWhite"
                        OptionChanged="rb_OptionChanged"/>
                    <local:RadioImage 
                        RadioName="RBOneD"
                        GroupName="groupe1"
                        Height="25" Width="25" Margin="2" Background="LimeGreen"
                        OptionChanged="rb_OptionChanged" />
                    <local:RadioImage 
                        RadioName="RBOneE"
                        GroupName="group1"
                        Height="25" Width="25" Margin="2" Background="Moccasin"
                        OptionChanged="rb_OptionChanged"/>
                </StackPanel>
     
                <StackPanel Orientation="Horizontal">
                    <local:RadioImage 
                        RadioName="RBTwoA"
                        GroupName="groupe2"
                        CheckState="True"
                        Height="25" Width="25" Margin="2" Background="Blue"
                        OptionChanged="rb_OptionChanged"/>  
                    <local:RadioImage 
                        RadioName="RBTwoB"
                        GroupName="groupe2"
                        Height="25" Width="25" Margin="2" Background="Brown"
                        OptionChanged="rb_OptionChanged"/>
                    <local:RadioImage
                        RadioName="RBTwoC"
                        GroupName="groupe2"
                        Height="25" Width="25" Margin="2" Background="BurlyWood"
                        OptionChanged="rb_OptionChanged"/>
                    <local:RadioImage 
                        RadioName="RBTwoD"
                        GroupName="group2"
                        Height="25" Width="25" Margin="2" Background="CornflowerBlue"
                        OptionChanged="rb_OptionChanged"/>
                    <local:RadioImage 
                        RadioName="RBTwoE"
                        GroupName="groupe2"
                        Height="25" Width="25" Margin="2" Background="CornflowerBlue"
                        OptionChanged="rb_OptionChanged"/>
                </StackPanel>
            </StackPanel>
        </Grid>
    </UserControl>

    et son code behind .cs:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    namespace WpfRadioButtons
    {
        /// <summary>
        /// Logique d'interaction pour UserControl1.xaml
        /// </summary>
        public partial class UserControl1 : UserControl
        {
            // Event  "weapper" prolongareur
            public event EventHandler<RadioImageArgs> OptionExternChanged;
            public UserControl1()
            {
                InitializeComponent();
            }
     
            private void rb_OptionChanged(object sender, RadioImageArgs e)
            {
                RadioButton radio = e.RadioButton;
                //pick the Background
                RadioBackground = radio.Background;
                EventHandler<RadioImageArgs> h = OptionExternChanged;
                if (h != null)
                    h(this, new RadioImageArgs(radio, radio.IsChecked));
            }
     
     
            // DP "weapper" du famous Background
            public Brush RadioBackground
            {
                get { return (Brush)GetValue(RadioBackgroundProperty); }
                set { SetValue(RadioBackgroundProperty, value); }
            }
     
            // Using a DependencyProperty as the backing store for RadioBrush.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty RadioBackgroundProperty =
                DependencyProperty.Register("RadioBackground", 
                typeof(Brush),
                typeof(UserControl1), 
                new UIPropertyMetadata(null));
     
     
        }
    }
    code xaml du form User qui démontre le "montage":
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <Window x:Class="WpfRadioButtons.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfRadioButtons" 
            Title="MainWindow" Height="350" Width="525">
        <StackPanel >
            <local:UserControl1 
                x:Name="ctl" OptionExternChanged="ctl_OptionExternChanged"
                />
            <TextBlock x:Name ="tb" />
            <Label Content="color" Background="{Binding ElementName=ctl,Path=RadioBackground}"/>
        </StackPanel>
    </Window>

    et son code behind .cs:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    namespace WpfRadioButtons
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
     
            private void ctl_OptionExternChanged(object sender, RadioImageArgs e)
            {
                UserControl1 ctl = sender as UserControl1;
                this.tb.Text = e.RadioButton.Name + "-" + e.RadioButton.GroupName;
     
            }
        }
    }
    Bon Code....

  5. #5
    Membre habitué
    Inscrit en
    Octobre 2004
    Messages
    352
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 352
    Points : 136
    Points
    136
    Par défaut
    Bonjour MABROUKI,

    J'ai copié ton code dans un nouveau projet, j'ai quelques erreurs :
    - RadioImageArgs est introuvable => Ne manque t'il pas une définition dans RadioImage ?
    - RadioName, GroupName, CheckState, OptionChanged : n'est pas reconnu ou n'est pas accessible => Quand on fait un UC avec un control comme un RadioButton, j'ai l'impression qu'il faut réécrire les propriétés du RadioButton quelque part dans l'UC pour qu'elles soient reconnues, non ?

    En fait, je n'imaginais pas que ce soit "si compliqué" de passer un état à travers 2 UC !
    Merci pour ton retour et ton aide

  6. #6
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    rebonjour

    Excuse moi mais effectivement j'ai loupé ce class RadioImageArgs....
    Le voici...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
     
    namespace WpfRadioButtons
    {
        public class RadioImageArgs:EventArgs 
        {
            public RadioImageArgs(RadioButton radioButton, bool? checkedState)
            {
                RadioButton = radioButton;
                CheckedState = checkedState;
            }
            public RadioButton RadioButton{ get; set; }
            public bool? CheckedState { get; set; }
     
        }
    }
    bon code...

  7. #7
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    Quand on fait un UC avec un control comme un RadioButton, j'ai l'impression qu'il faut réécrire les propriétés du RadioButton quelque part dans l'UC pour qu'elles soient reconnues, non ?
    Toutes les DP du RadioButton qu'on a besoin de "manipuler" à exterieur du UserControl et de tout autre control...
    En général la méthode consiste à "binder" les DP du RadioButton aux DP "ad-hoc" de notre cru par le mécanisme :"{Binding path=DPadhoc,RelativeSource={etc...}}"...
    Pour les events du radiobutton il faut un "event ad-hoc" "per-event"...

    Bon code...

  8. #8
    Membre habitué
    Inscrit en
    Octobre 2004
    Messages
    352
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 352
    Points : 136
    Points
    136
    Par défaut
    Citation Envoyé par MABROUKI Voir le message
    rebonjour

    Excuse moi mais effectivement j'ai loupé ce class RadioImageArgs....
    Le voici...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
     
    namespace WpfRadioButtons
    {
        public class RadioImageArgs:EventArgs 
        {
            public RadioImageArgs(RadioButton radioButton, bool? checkedState)
            {
                RadioButton = radioButton;
                CheckedState = checkedState;
            }
            public RadioButton RadioButton{ get; set; }
            public bool? CheckedState { get; set; }
     
        }
    }
    bon code...
    Bonjour,

    Avec cette class, cela compile.
    Il reste un petit bug, dans le group1 j'ai plusieurs radioButton check, idem dans le group2, et la Windows démarre avec tout les radioButton check.
    Mais, mais… mais : C'est génial !! Cela fait exactement ce que je voulais à propos du binding, ne t'embête pas avec cette histoire de bug, je vais regarder ça. Je te remercie beaucoup, j'ai beaucoup appris grâce à toi. Je place le tag résolu sur la discussion.

    À bientôt

    Édit: Pour info : il manquait un "e" sur le GroupName (groupe1 / groupe2) d'un contrôle du Groupe1 et Groupe2.
    J'ai ajouter CheckState="False" sur les contrôles sauf le premier de chaque groupe pour qu'ils soient dans un état stable.
    Du coup, c'est parfait maintenant

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2010] Condition avec du texte qui ne fonctionne pas
    Par Miyamura dans le forum Excel
    Réponses: 2
    Dernier message: 07/08/2017, 10h32
  2. [AC-2010] Requête avec NOT IN qui ne fonctionne pas
    Par happyaccess dans le forum Requêtes et SQL.
    Réponses: 2
    Dernier message: 24/03/2013, 23h53
  3. Test avec un if qui ne fonctionne pas
    Par zoom61 dans le forum Langage
    Réponses: 1
    Dernier message: 13/01/2012, 13h11
  4. requete SQL avec sous requete qui ne fonctionne pas
    Par skyarnangel dans le forum Langage SQL
    Réponses: 8
    Dernier message: 06/02/2009, 14h47
  5. Réponses: 6
    Dernier message: 04/09/2007, 00h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo