Home > Software design >  How to change styles in UWP correctly?
How to change styles in UWP correctly?

Time:09-17

Tell me how you can change the styles more succinctly? The thing is, I change the style for each element separately, which is not very good

 <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState>
                    <VisualState.StateTriggers>
                        <StateTrigger IsActive="{Binding IsOfferSO}" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="gridOffer1.Style" Value="{StaticResource SelectedOffer}" />
                        <Setter Target="gridOffer2.Style" Value="{StaticResource UnSelectedOffer}" />
                        <Setter Target="durationOffer1.Style" Value="{StaticResource SelectedTxtBlock}" />
                        <Setter Target="durationOffer2.Style" Value="{StaticResource UnSelectedTxtBlock}" />
                        <Setter Target="priceOffer1.Style" Value="{StaticResource SelectedTxtBlock}" />
                        <Setter Target="priceOffer2.Style" Value="{StaticResource UnSelectedTxtBlock}" />
                    </VisualState.Setters>
                </VisualState>

                <VisualState>
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="gridOffer1.Style" Value="{StaticResource UnSelectedOffer}" />
                        <Setter Target="gridOffer2.Style" Value="{StaticResource SelectedOffer}" />
                        <Setter Target="durationOffer1.Style" Value="{StaticResource UnSelectedTxtBlock}" />
                        <Setter Target="durationOffer2.Style" Value="{StaticResource SelectedTxtBlock}" />
                        <Setter Target="priceOffer1.Style" Value="{StaticResource UnSelectedTxtBlock}" />
                        <Setter Target="priceOffer2.Style" Value="{StaticResource SelectedTxtBlock}" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

Are there any examples that would allow you to do the same, but in a simpler way?

CodePudding user response:

Please refer to this document to edit your style, you could specific a TargetType and an x:Key attribute attribute for the Style and then by setting the target control's Style property with a {StaticResource} markup extension reference that uses the explicit key.

And if you want to this style could effect for each target control by default. please specific only a TargetType for the Style.

  • Related