M
M
Mikhail Usotsky2018-05-24 12:33:18
WPF
Mikhail Usotsky, 2018-05-24 12:33:18

How to get a new value when changing the pointer in a radio button, and not the old one?

There is this code:

<Style
        x:Key="Item"
        BasedOn="{x:Null}"
        TargetType="{x:Type ListBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="Height" Value="42" />
        <Setter Property="Margin" Value="-2,0,0,0" />
        <Setter Property="local:PeakHelper.IsRadioButton" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid x:Name="ListItem">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBlock
                            x:Name="ListItem_Text"
                            Grid.Column="0"
                            Padding="10,0"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Center"
                            FontSize="13"
                            FontWeight="Medium"
                            Foreground="{TemplateBinding Foreground}"
                            Text="{TemplateBinding Content}" />
                        <RadioButton
                            x:Name="PART_SelectVariable_1"
                            Grid.Column="1"
                            Content="Вариант 1"
                            Style="{StaticResource {x:Type ToggleButton}}" />
                        <RadioButton
                            x:Name="PART_SelectVariable_2"
                            Grid.Column="2"
                            Content="Вариант 2"
                            Style="{StaticResource {x:Type ToggleButton}}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="ListItem" Property="Background" Value="#FF203FE6" />
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="White" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="ListItem" Property="Background" Value="LightGray" />
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="DarkGray" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="ListItem" Property="Background" Value="#FFFDDFAD" />
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="#FFA47C38" />
                        </Trigger>
                        <DataTrigger Binding="{Binding IsChecked, ElementName=PART_SelectVariable_1}" Value="True">
                            <Setter Property="local:PeakHelper.IsRadioButton" Value="1" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsChecked, ElementName=PART_SelectVariable_2}" Value="True">
                            <Setter Property="local:PeakHelper.IsRadioButton" Value="2" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsPressed, ElementName=PART_SelectVariable_1}" Value="True">
                            <Setter Property="IsSelected" Value="True" />
                            <Setter TargetName="ListItem" Property="Background" Value="#FFE8E9F2" />
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="#FF9A9FB8" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsPressed, ElementName=PART_SelectVariable_2}" Value="True">
                            <Setter Property="IsSelected" Value="True" />
                            <Setter TargetName="ListItem" Property="Background" Value="#FFE8E9F2" />
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="#FF9A9FB8" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

There is a problem. After clicking on one of the radio buttons, I definitely get only the previous value local:PeakHelper.IsRadioButton (this is a DependencyObject). For example, when I click on the Option 1 radio button, I get either 0 (if all radio buttons start with IsChecked = false) or 2 (if the Option 2 radio button is set to true). After clicking again, I already get the value I need.
How to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Usotsky, 2018-06-09
@AquariusStar

Found a solution for this issue. It turns out that there is a certain nuance to the sequence of events.

<!--  ListBoxItem  -->
    <Style x:Key="Item"
           BasedOn="{x:Null}"
           TargetType="{x:Type ListBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Height" Value="42"/>
        <Setter Property="Margin" Value="-2,0,0,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid x:Name="ListItem">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="ListItem_Text"
                                   Grid.Column="0"
                                   Padding="10,0"
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Center"
                                   FontSize="13"
                                   FontWeight="Medium"
                                   Foreground="{TemplateBinding Foreground}"
                                   Text="{TemplateBinding Content}"/>
                        <RadioButton x:Name="PART_SelectVariable_1"
                                     Grid.Column="1"
                                     Content="Вариант 1"
                                     IsEnabled="{TemplateBinding IsEnabled}"
                                     Style="{StaticResource {x:Type ToggleButton}}"/>
                        <RadioButton x:Name="PART_SelectVariable_2"
                                     Grid.Column="2"
                                     Content="Вариант 2"
                                     IsEnabled="{TemplateBinding IsEnabled}"
                                     Style="{StaticResource {x:Type ToggleButton}}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="ListItem" Property="Background" Value="#FF203FE6"/>
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="White"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="ListItem" Property="Background" Value="LightGray"/>
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="DarkGray"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="ListItem" Property="Background" Value="#FFFDDFAD"/>
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="#FFA47C38"/>
                        </Trigger>
                        <Trigger Property="local:Styles.IsRadioButton" Value="-1">
                            <Setter Property="DataContext" Value="0"/>
                            <Setter TargetName="PART_SelectVariable_1" Property="IsChecked" Value="false"/>
                            <Setter TargetName="PART_SelectVariable_2" Property="IsChecked" Value="false"/>
                        </Trigger>
                        <DataTrigger Binding="{Binding ElementName=PART_SelectVariable_1, Path=IsPressed}"
                                     Value="True">
                            <Setter TargetName="ListItem" Property="Background" Value="#FFE8E9F2"/>
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="#FF9A9FB8"/>
                            <Setter Property="local:Styles.IsRadioButton" Value="1"/>
                            <Setter Property="IsSelected" Value="True"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=PART_SelectVariable_2, Path=IsPressed}"
                                     Value="True">
                            <Setter TargetName="ListItem" Property="Background" Value="#FFE8E9F2"/>
                            <Setter TargetName="ListItem_Text" Property="Foreground" Value="#FF9A9FB8"/>
                            <Setter Property="local:Styles.IsRadioButton" Value="2"/>
                            <Setter Property="IsSelected" Value="True"/>
                        </DataTrigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

If you put local:PeakHelper.IsRadioButton into the IsChecked trigger, and cause the SelectedIndex active pointer to change to IsPressed, then I will get the old data. And it was necessary at once everything in IsPressed.
Since IsPressed comes first in the sequence of events, and IsChecked is already second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question