简体   繁体   English

wpf为命名元素创建样式

[英]wpf create style for named element

Is it possible to add style to xaml element without editing the element ? 是否可以在不编辑元素的情况下为xaml元素添加样式?

for example: 例如:

xaml element: xaml元素:

<Grid>
     <Grid x:Name="A">content A</Grid>
     <Grid x:Name="B">content B</Grid>
</Grid>

and style: 和风格:

<Style x:Key="StyleForA" TargetName="A" TargetType="{x:Type Grid}" >
   <Setter Property="Background" Value="Red"/>
</Style>
<Style x:Key="StyleForB" TargetName="B" TargetType="{x:Type Grid}" >
   <Setter Property="Background" Value="Green"/>
</Style>

UPD: I have a project with a lot of styles (aero, black, etc ). UPD:我有一个有很多风格的项目(航空,黑色等)。

And if I edit the element Style="{StaticResources StyleForA}" I must edit all styles. 如果我编辑元素Style="{StaticResources StyleForA}"我必须编辑所有样式。 So I need to create local style that affected to named element. 所以我需要创建影响命名元素的本地样式。

Natively you can't. 本地你不能。

But without touching your XAML, you can do it very easily using code : 但是,如果不接触您的XAML,您可以使用代码轻松完成:

    Grd1.Style = (Style) this.Resources["GridKey"];

Pure XAML approach using Blend behaviors , 使用Blend behaviors纯XAML方法,

<Grid x:Name="Grd1">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <ic:ChangePropertyAction PropertyName="Style" Value="{DynamicResource GridKey}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Grid>

Yes. 是。 I use this style on my App.xaml to add a teplate around all aplication controls with error: 我在App.xaml上使用这种样式,在所有应用程序控件周围添加了一个错误:

<Style x:Key="BordaTemplate" TargetType="Control">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">                           
                        <Border BorderBrush="Red" BorderThickness="1">                           
                            <AdornedElementPlaceholder Name="myControl"/>
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>


 <Style TargetType="TextBox" BasedOn="{StaticResource BordaTemplate}" />        
        <Style TargetType="PasswordBox" BasedOn="{StaticResource BordaTemplate}" />
        <Style TargetType="DataGrid" BasedOn="{StaticResource BordaTemplate}" />
        <Style TargetType="ListBox"  BasedOn="{StaticResource BordaTemplate}" />
    <Style TargetType="CheckBox" BasedOn="{StaticResource BordaTemplate}" />
    <Style TargetType="ComboBox" BasedOn="{StaticResource BordaTemplate}" />
    <Style TargetType="DatePicker"  BasedOn="{StaticResource BordaTemplate}" />   

Not like as you have defined using target but if you want to apply a style to a specific control . 不像您已使用target定义但是如果要将style应用于特定control then use below: 然后使用以下:

 <StackPanel>
        <Button x:Name="A">
            <Button.Resources>
                <Style  TargetType="{x:Type Button}" >
                    <Setter Property="Background" Value="Red"/>
                </Style>
            </Button.Resources>
            content A</Button>
        <Button x:Name="B">
            <Button.Resources>
                <Style  TargetType="{x:Type Button}" >
                    <Setter Property="Background" Value="Green"/>
                </Style>
            </Button.Resources>
            content B</Button>
    </StackPanel>

Or probably create a base style and then create a BasedOn style for your controls. 或者可能创建一个基本样式,然后为您的控件创建一个BasedOn样式。

Update: If you only want to be concerned about the style at a time and be sure of impacted elements: 更新:如果您只想一次关注样式并确保受影响的元素:

 <Window.Resources>
    <Style  TargetType="{x:Type Button}" x:Key="First" >
        <Setter Property="Background" Value="Red"/>
    </Style>
    <Style  TargetType="{x:Type Button}" x:Key="Second" >
        <Setter Property="Background" Value="Green"/>
    </Style>
</Window.Resources>
<StackPanel>
    <Button x:Name="A">
        <Button.Resources>
            <Style  TargetType="{x:Type Button}" BasedOn="{StaticResource First}" />                  
        </Button.Resources>
        content A</Button>
    <Button x:Name="B">
        <Button.Resources>
            <Style  TargetType="{x:Type Button}"  BasedOn="{StaticResource Second}" />                    
        </Button.Resources>
        content B</Button>
</StackPanel>

I think you can have a default style and you can override the properties you want on your local style. 我认为您可以使用默认样式,并且可以在本地样式上覆盖所需的属性。

 <Grid>
        <Grid.Resources>
            <Style TargetType="Button" x:Key="Default">
                <Setter Property="Background" Value="White"></Setter> 
                <Setter Property="Foreground" Value="Blue"/>
            </Style>

            <Style x:Key="StyleForA" BasedOn="{StaticResource Default}" TargetType="Button" >
                <Setter   Property="Background"  Value="Red" ></Setter>
            </Style>

            <Style x:Key="StyleForB" BasedOn="{StaticResource Default}" TargetType="Button" >
                <Setter   Property="Background"  Value="Green" ></Setter>
            </Style>
        </Grid.Resources>
        <StackPanel>
            <Button Style="{StaticResource StyleForA}" Width="100" x:Name="A" Click="Button_Click" Height="100">Test A</Button>
            <Button Style="{StaticResource StyleForB}" Width="100" x:Name="B" Click="Button_Click" Height="100">Test B</Button>
        </StackPanel>
    </Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM