Skip to content

Commit

Permalink
RadioButtons: Fix header not having correct color when disabled (micr…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Oct 25, 2020
1 parent 89bc3d6 commit 76e1899
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
13 changes: 13 additions & 0 deletions ModernWpf.Controls/RadioButtons/RadioButtons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public RadioButtons()
PreviewKeyDown += OnChildPreviewKeyDown;

m_radioButtonsElementFactory = new RadioButtonsElementFactory();

IsEnabledChanged += OnIsEnabledChanged;
}

#region ItemsSource
Expand Down Expand Up @@ -250,6 +252,7 @@ public override void OnApplyTemplate()
}

UpdateItemsSource();
UpdateVisualStateForIsEnabledChange();
}

// When focus comes from outside the RadioButtons control we will put focus on the selected radio button.
Expand Down Expand Up @@ -641,6 +644,11 @@ bool MoveFocus(int indexIncrement)
return false;
}

private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
UpdateVisualStateForIsEnabledChange();
}

public UIElement ContainerFromIndex(int index)
{
var repeater = m_repeater;
Expand Down Expand Up @@ -715,6 +723,11 @@ private void UpdateItemTemplate()
m_radioButtonsElementFactory.UserElementFactory(ItemTemplate);
}

private void UpdateVisualStateForIsEnabledChange()
{
VisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled", false);
}

// Test Hooks helpers, only function when m_testHooksEnabled == true
internal void SetTestHooksEnabled(bool enabled)
{
Expand Down
16 changes: 14 additions & 2 deletions ModernWpf.Controls/RadioButtons/RadioButtons.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@
<Setter.Value>
<ControlTemplate TargetType="local:RadioButtons">
<StackPanel SnapsToDevicePixels="True">
<ContentPresenter
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Disabled" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter x:Name="HeaderContentPresenter"
Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}"
Margin="{DynamicResource RadioButtonsHeaderMargin}"
TextElement.Foreground="{DynamicResource RadioButtonsHeaderForeground}"
Margin="{DynamicResource RadioButtonsTopHeaderMargin}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<local:ItemsRepeater x:Name="InnerRepeater">
<local:ItemsRepeater.Layout>
Expand All @@ -26,6 +33,11 @@
</local:ItemsRepeater.Layout>
</local:ItemsRepeater>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="HeaderContentPresenter" Property="TextElement.Foreground" Value="{DynamicResource RadioButtonsHeaderForegroundDisabled}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
Expand Down
2 changes: 1 addition & 1 deletion ModernWpf/ControlsResources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<sys:Double x:Key="TextControlThemeMinHeight">32</sys:Double>
<Thickness x:Key="NumberBoxSpinButtonBorderThickness">0,1,1,1</Thickness>
<Thickness x:Key="NumberBoxIconMargin">10,0,0,0</Thickness>
<Thickness x:Key="RadioButtonsHeaderMargin">0,0,0,4</Thickness>
<Thickness x:Key="RadioButtonsTopHeaderMargin">0,0,0,4</Thickness>
<Thickness x:Key="TextControlThemePadding">10,5,6,6</Thickness>
<Thickness x:Key="ToggleSwitchTopHeaderMargin">0,0,0,0</Thickness>

Expand Down
4 changes: 4 additions & 0 deletions ModernWpf/ThemeResources/Dark.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,10 @@
<m:StaticResource x:Key="RadioButtonCheckGlyphStrokePressed" ResourceKey="SystemControlTransparentBrush" />
<m:StaticResource x:Key="RadioButtonCheckGlyphStrokeDisabled" ResourceKey="SystemControlTransparentBrush" />

<!-- Resources for RadioButtons -->
<m:StaticResource x:Key="RadioButtonsHeaderForeground" ResourceKey="SystemControlForegroundBaseHighBrush" />
<m:StaticResource x:Key="RadioButtonsHeaderForegroundDisabled" ResourceKey="SystemControlDisabledBaseMediumLowBrush" />

<!-- Resources for RatingControl -->
<m:StaticResource x:Key="RatingControlUnselectedForeground" ResourceKey="SystemControlBackgroundBaseLowBrush" />
<m:StaticResource x:Key="RatingControlSelectedForeground" ResourceKey="SystemControlForegroundAccentBrush" />
Expand Down
4 changes: 4 additions & 0 deletions ModernWpf/ThemeResources/HighContrast.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,10 @@
<m:StaticResource x:Key="RadioButtonCheckGlyphStrokePressed" ResourceKey="SystemControlTransparentBrush" />
<m:StaticResource x:Key="RadioButtonCheckGlyphStrokeDisabled" ResourceKey="SystemControlTransparentBrush" />

<!-- Resources for RadioButtons -->
<m:StaticResource x:Key="RadioButtonsHeaderForeground" ResourceKey="SystemControlForegroundBaseHighBrush" />
<m:StaticResource x:Key="RadioButtonsHeaderForegroundDisabled" ResourceKey="SystemControlDisabledBaseMediumLowBrush" />

<!-- Resources for RatingControl -->
<m:StaticResource x:Key="RatingControlUnselectedForeground" ResourceKey="SystemControlForegroundBaseLowBrush" />
<m:StaticResource x:Key="RatingControlSelectedForeground" ResourceKey="SystemControlHighlightAccentBrush" />
Expand Down
4 changes: 4 additions & 0 deletions ModernWpf/ThemeResources/Light.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,10 @@
<m:StaticResource x:Key="RadioButtonCheckGlyphStrokePressed" ResourceKey="SystemControlTransparentBrush" />
<m:StaticResource x:Key="RadioButtonCheckGlyphStrokeDisabled" ResourceKey="SystemControlTransparentBrush" />

<!-- Resources for RadioButtons -->
<m:StaticResource x:Key="RadioButtonsHeaderForeground" ResourceKey="SystemControlForegroundBaseHighBrush" />
<m:StaticResource x:Key="RadioButtonsHeaderForegroundDisabled" ResourceKey="SystemControlDisabledBaseMediumLowBrush" />

<!-- Resources for RatingControl -->
<m:StaticResource x:Key="RatingControlUnselectedForeground" ResourceKey="SystemControlBackgroundBaseLowBrush" />
<m:StaticResource x:Key="RatingControlSelectedForeground" ResourceKey="SystemControlForegroundAccentBrush" />
Expand Down
41 changes: 41 additions & 0 deletions test/ModernWpfTestApp/ApiTests/RadioButtonsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Windows.Media;
using System.Windows;
using System.Linq;

#if USING_TAEF
using WEX.TestExecution;
Expand Down Expand Up @@ -80,5 +81,45 @@ public void VerifyCustomItemTemplate()
Verify.IsTrue(testCondition, "The foreground color of the RadioButton should have been [blue].");
});
}

[TestMethod]
public void VerifyIsEnabledChangeUpdatesVisualState()
{
RadioButtons radioButtons = null; ;
VisualStateGroup commonStatesGroup = null;
RunOnUIThread.Execute(() =>
{
radioButtons = new RadioButtons();
// Check 1: Set IsEnabled to true.
radioButtons.IsEnabled = true;
Content = radioButtons;
Content.UpdateLayout();
var radioButtonsLayoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(radioButtons, 0);
commonStatesGroup = VisualStateManager.GetVisualStateGroups(radioButtonsLayoutRoot).Cast<VisualStateGroup>().First(vsg => vsg.Name.Equals("CommonStates"));
Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);
// Check 2: Set IsEnabled to false.
radioButtons.IsEnabled = false;
});
IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
Verify.AreEqual("Disabled", commonStatesGroup.CurrentState.Name);
// Check 3: Set IsEnabled back to true.
radioButtons.IsEnabled = true;
});
IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);
});
}
}
}

0 comments on commit 76e1899

Please sign in to comment.