0

I'm just getting started with my first "serious" WPF application. I've ran into an issue where my Style.Triggers > IsChecked for a RadioButton is not picking up on the changes. When I start my application everything seems fine. When I change pages it should be updating the background color of the buttons. It applies the change as it should when the button IsChecked = True but it never reverts back to the default(Transparent) once it's marked as False.

Items Control:

<ItemsControl ItemsSource="{Binding MainMenuButtons}" Grid.Row="1" Margin="0 0 3 0">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <RadioButton Content="{Binding Content}" IsChecked="{Binding IsChecked}"
                                     Command="{Binding Command}" Style="{StaticResource MenuButtonTheme}" />                            
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

StaticResource MenuButtonTheme:

<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}" x:Key="MenuButtonTheme">
    <Style.Setters>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="RadioButton">
                    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="{TemplateBinding Background}">
                        <TextBlock Text="{TemplateBinding Property=Content}" VerticalAlignment="Center" Margin="25,0,0,0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontSize" Value="14" />
        <Setter Property="Height" Value="50" />
    </Style.Setters>
    
    <Style.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Background" Value="#5F5F5F" />
        </Trigger>
    </Style.Triggers>
</Style>

In my view model for now I just say anytime you click on a button set the IsChecked to false for all buttons for testing. This is where it starts to break down. It's like when I update the IsChecked from within the ViewModel everything is fine but the style update doesnt get triggered.

ViewModel Code Chunks:

When you click on a button:
foreach (var button in _mainMenuButtons)
        {
            button.IsChecked = false;
        }

On Startup:
_mainMenuButtons.Add(new MenuButton() { Name = "HomeViewBtn", IsChecked = true, Command = GoToViewHome, Content = "Home"});
_mainMenuButtons.Add(new MenuButton() { Name = "CheckPreviousBatchViewBtn", IsChecked = false, Command = GoToViewCheckPreviousBatch, Content = "Check Previous Batch"});

I have verified that the IsChecked property is indeed set to false in the foreach loop correctly:

enter image description here

Here is what the default startup view looks like(correct):

enter image description here

Here is what it looks like after I click on the view batch button:

enter image description here

If I were to click on Home again it then keeps them all highlighted as if IsChecked is true. Any thoughts or suggestions?

Silent
  • 438
  • 2
  • 6
  • 19
  • 1
    Does MenuButton implement the INotifyPropertyChanged interface and does it fire the PropertyChanged event for its IsChecked property? – Clemens Jun 23 '21 at 21:09
  • @Clemens come on that would be to easy! Not sure why this went so far over my head. Let me try that here when I get back to the office. – Silent Jun 23 '21 at 21:16
  • @Clemens so you were spot on with this. I do know where I screwed up though, I originally and put the OnPropertyChanged() in the for loop right after I made the update. Your comment made me realize that doing that makes no sense, since I was telling it my MainWindow properties were updating which isnt the case. I moved the MenuButton over to use my "ObservableObject" which just inherits INotifyPropertyChanged and does a couple other small things to help with re-usability. After doing this everything works as expected. – Silent Jun 23 '21 at 21:27
  • I cant mark the comment as an answer, if you want to move it to an answer I can flag it though if you'd like. – Silent Jun 23 '21 at 21:28

0 Answers0