WPF的样式选择器多选的写法。按照官网的说法是这样的语法。

<Style x:TargetType="Selector">
<Setter Property="MyProperty" Value="Green" />
</Style>

想要实现这个Selector多选我们应该怎么写呢?下面举例假设只有一个条件

<StackPanel Background="LightSlateGray">
            <StackPanel.Resources>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Margin" Value="0,10,0,0"/>
                </Style>
            </StackPanel.Resources>
            <Label>在这里设置中心点</Label>
            <TextBox Text="Apple"/>
            <TextBox Text="Banana"/>
            <TextBox Text="Cherry"/>
            <Button Click="Button_Click" Content="点击缩放"></Button>
        </StackPanel>

上面示例只是约束StackPanel内的子控件有一个样式Margin  Top=10。但是如果我想要同时约束Label和Button呢?应该如何写呢?

我们可以像如下这样写:

 <StackPanel  Background="LightSlateGray">
            <StackPanel.Resources>
                <Style TargetType="{x:Type FrameworkElement}">
                    <Setter Property="Margin" Value="0,10,0,0"/>
                </Style>
                <Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type FrameworkElement}}" />
                <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" />
                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type FrameworkElement}}" />
            </StackPanel.Resources>
            <Label>在这里设置中心点</Label>
            <TextBox Text="Apple"/>
            <TextBox Text="Banana"/>
            <TextBox Text="Cherry"/>
            <Button Click="Button_Click" Content="点击缩放"></Button>
        </StackPanel>

lebang2020.cn

https://lebang2020.cn/details/211106dudgcwwc.html