Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't override style of content within templated control #7792

Open
2 tasks done
michael-hawker opened this issue Oct 6, 2022 · 2 comments
Open
2 tasks done

Can't override style of content within templated control #7792

michael-hawker opened this issue Oct 6, 2022 · 2 comments
Labels
area-Styling bug Something isn't working team-Controls Issue for the Controls team wct

Comments

@michael-hawker
Copy link
Collaborator

michael-hawker commented Oct 6, 2022

Describe the bug

We're working on the new SettingsExpander control in Windows Community Toolkit Labs: CommunityToolkit/Labs-Windows#253

As part of this we need to ensure that ToggleSwitch controls as part of the content get the proper styling to align to the design guidelines for this pattern:

image

Not this:

image

We need this to be done implicitly so developers don't have to add extra styling for each common option they add to their app. The style is part of using this contract in the control. We've done this in other places and scenarios, but this specific one is failing and we don't understand the differences in the setup here causing the issue.

Steps to reproduce the bug

Minimal repro (both UWP and WinUI 3):

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <Style x:Key="RightAlignedCompactToggleSwitchStyle"
           BasedOn="{StaticResource DefaultToggleSwitchStyle}"
           TargetType="ToggleSwitch">
            <Setter Property="Background" Value="Red"/>
        </Style>

        <Style x:Key="MyUserControl" TargetType="Button"> <!-- Target Type doesn't matter here, using as a makeshift Templated Control in XAML Studio -->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <!-- For some reason template binding is negating the resource override here -->
                        <ContentPresenter Content="{TemplateBinding Content}">
                            <ContentPresenter.Resources>
                                <Style TargetType="ToggleSwitch" BasedOn="{StaticResource RightAlignedCompactToggleSwitchStyle}"/>
                            </ContentPresenter.Resources>
                        </ContentPresenter>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="MyUserControlHardcoded" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter>
                            <ContentPresenter.Content>
                                <ToggleSwitch/>
                            </ContentPresenter.Content>
                            <ContentPresenter.Resources>
                                <Style TargetType="ToggleSwitch" BasedOn="{StaticResource RightAlignedCompactToggleSwitchStyle}"/>
                            </ContentPresenter.Resources>
                        </ContentPresenter>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>

    <StackPanel Padding="40" Spacing="16">
        <!-- Doesn't Work as expected -->
        <Button Style="{StaticResource MyUserControl}">
            <ToggleSwitch/>
        </Button>
        <!-- Works within template -->
        <Button Style="{StaticResource MyUserControlHardcoded}"/>
        <!-- Works outside of a control -->
        <ContentPresenter>
            <ContentPresenter.Content>
                <ToggleSwitch/>
            </ContentPresenter.Content>
            <ContentPresenter.Resources>
                <Style TargetType="ToggleSwitch" BasedOn="{StaticResource RightAlignedCompactToggleSwitchStyle}"/>
            </ContentPresenter.Resources>
        </ContentPresenter>

        <!-- expected result -->
        <ToggleSwitch Style="{StaticResource RightAlignedCompactToggleSwitchStyle}"/>
    </StackPanel>

</Page>

We also tried not using Template Binding and setting the content property of the inner ContentPresenter in OnApplyTemplate but that didn't work as expected either. (It's also not tied to ToggleSwitch as the content either, could be a Button just an example for our scenario.)

image

All controls should have red background.

Expected behavior

Style is properly overridden and picked up by the content of the control.

Screenshots

No response

NuGet package version

No response

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

Windows 10 (21H2): Build 19044

Additional context

Related other issues:

@michael-hawker michael-hawker added the bug Something isn't working label Oct 6, 2022
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Oct 6, 2022
@MikeHillberg
Copy link
Contributor

I think what's happening in the first case is that the ToggleSwitch has two parents: the Button (because it's set as Button.Content) and the ContentPresenter (because the binding causes it to also be ContentPresenter.Content). That creates ambiguity when walking up the tree to find an implicit style: which parent to follow? What happens with ContentControls like Button and ListView Item is a preference the templated parent (the Button over the ContentPresenter).

WinUI doesn't have API around this, but this is the same logical tree concept as in WPF (WinUI doesn't have the LogicalTreeHelper like WPF).

michael-hawker pushed a commit to CommunityToolkit/Labs-Windows that referenced this issue Oct 6, 2022
@pratikone pratikone added area-Styling team-Controls Issue for the Controls team and removed needs-triage Issue needs to be triaged by the area owners labels Oct 11, 2022
michael-hawker added a commit to CommunityToolkit/Labs-Windows that referenced this issue Oct 13, 2022
* Adding new controls

* Uno fixes

* Added SettingsExpanderSample

* Adding sampleref

* Bugfixes

* CI fix

* Fix CI

* Adding ExpanderV2

* CI fix

* Update Generic.xaml

* Add overrides

* Uno fixes

* CI fix

* Update ExampleSettingsControlsTestClass.cs

* Simplifing state setting

* Revert "Simplifing state setting"

This reverts commit a014377.

* Quick fix to enable building locally to workaround unoplatform/uno#9297

* Update labs/SettingsControls/samples/SettingsControls.Samples/SettingsExpanderSample.xaml

Co-authored-by: Michael Hawker MSFT (XAML Llama) <[email protected]>

* Update labs/SettingsControls/samples/SettingsControls.Samples/SettingsExpanderSample.xaml

Co-authored-by: Michael Hawker MSFT (XAML Llama) <[email protected]>

* Tweaks

* Renamed ButtonIcon to ActionIcon

* Make SettingsExpander Content a ContentProperty

* File rename

* Making the StyleSelector work

* Add github.com/rudyhuyn/XamlPlus Attached Style Helper

Resolve issue with ToggleSwitch override due to microsoft/microsoft-ui-xaml#7792

* Add binding to Expander option toggle

Note: should be TwoWay bound but issue on Uno, added note here for now: #207 (comment)

* Removing HeaderedContentControl from the template

* XAML styling

* Removed HeaderedContentControl out of the SettingsCard template

* Adding WrapThreshold

* Fix failing test in SettingsCard tests

* Tweaked SettingsCard sample

* XAML styling

* Remove SettingsExpanderItem and use SettingsCard directly

Allows for better interop to just cut/paste SettingsCards in/out of Expanders 🙂

* UI tweaks to samples and SettingsCard to render the correct height

* Adding VSM only

* Temp: Comment out column trigger which was causing control to grow after state change and cause infinite layout cycle

i.e. when the break point was reached (e.g. control size 560), VSM was changing the layout and now control size would be larger again (e.g. 605), which would then turn off trigger and re-layout back to old size which would now be smaller again, etc...

We need to ensure the transition between the Right and Vertical states doesn't increase the width of the control.

* Remove redundant minwidth

* Clean up code, comments

* Code cleanup SettingsCard

* Settings ToolkitSampleRenderer alignment to Stretch

* Bumping version number

* Removing HeaderedContentControl

* Removing remaining reference

* Add triggers

* Update Generic.xaml

* Update Generic.xaml

* Use ThemeResource instead of StaticResource

Co-authored-by: michael-hawker <[email protected]>
Co-authored-by: Rudy Huyn <[email protected]>
@michael-hawker
Copy link
Collaborator Author

Bumping, based on #8638

Martin1994 pushed a commit to Martin1994/Labs-Windows that referenced this issue Sep 2, 2023
* Adding new controls

* Uno fixes

* Added SettingsExpanderSample

* Adding sampleref

* Bugfixes

* CI fix

* Fix CI

* Adding ExpanderV2

* CI fix

* Update Generic.xaml

* Add overrides

* Uno fixes

* CI fix

* Update ExampleSettingsControlsTestClass.cs

* Simplifing state setting

* Revert "Simplifing state setting"

This reverts commit a014377.

* Quick fix to enable building locally to workaround unoplatform/uno#9297

* Update labs/SettingsControls/samples/SettingsControls.Samples/SettingsExpanderSample.xaml

Co-authored-by: Michael Hawker MSFT (XAML Llama) <[email protected]>

* Update labs/SettingsControls/samples/SettingsControls.Samples/SettingsExpanderSample.xaml

Co-authored-by: Michael Hawker MSFT (XAML Llama) <[email protected]>

* Tweaks

* Renamed ButtonIcon to ActionIcon

* Make SettingsExpander Content a ContentProperty

* File rename

* Making the StyleSelector work

* Add github.com/rudyhuyn/XamlPlus Attached Style Helper

Resolve issue with ToggleSwitch override due to microsoft/microsoft-ui-xaml#7792

* Add binding to Expander option toggle

Note: should be TwoWay bound but issue on Uno, added note here for now: CommunityToolkit#207 (comment)

* Removing HeaderedContentControl from the template

* XAML styling

* Removed HeaderedContentControl out of the SettingsCard template

* Adding WrapThreshold

* Fix failing test in SettingsCard tests

* Tweaked SettingsCard sample

* XAML styling

* Remove SettingsExpanderItem and use SettingsCard directly

Allows for better interop to just cut/paste SettingsCards in/out of Expanders 🙂

* UI tweaks to samples and SettingsCard to render the correct height

* Adding VSM only

* Temp: Comment out column trigger which was causing control to grow after state change and cause infinite layout cycle

i.e. when the break point was reached (e.g. control size 560), VSM was changing the layout and now control size would be larger again (e.g. 605), which would then turn off trigger and re-layout back to old size which would now be smaller again, etc...

We need to ensure the transition between the Right and Vertical states doesn't increase the width of the control.

* Remove redundant minwidth

* Clean up code, comments

* Code cleanup SettingsCard

* Settings ToolkitSampleRenderer alignment to Stretch

* Bumping version number

* Removing HeaderedContentControl

* Removing remaining reference

* Add triggers

* Update Generic.xaml

* Update Generic.xaml

* Use ThemeResource instead of StaticResource

Co-authored-by: michael-hawker <[email protected]>
Co-authored-by: Rudy Huyn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Styling bug Something isn't working team-Controls Issue for the Controls team wct
Projects
None yet
Development

No branches or pull requests

3 participants