WPF中是否有一种按名称为特定控件设置样式的方法?

5

通过资源文件设置控件颜色。

在资源文件中:

<style TargetType="{x:Name button1}">
      <setter Property="ColorBrush" Value="Red"/>
</style>
<Window .... >
    <Button Name="Button1" Content="Test Button" />
</Window>

就像HTML中的CSS一样

 <style>
       #controlID
       {
         color:red;
       }
    </style>
<body>
      <input id="button1" type="button" value="test button" />
</body>

将样式明确应用于特定按钮。 - Sriram Sakthivel
亲爱的Sriram Sakthivel,我想为我的控件拥有两个文本集合,分别存储在两个不同的资源文件中,以便于多语言应用程序使用。如果我更改资源文件,则语言也会随之更改。是否有任何方法可以做到这一点,而无需为控件设置静态资源键以节省时间? - mehdi
1个回答

9
在window.resources中定义一个类似这样的样式
<Window.Resources>
    <Style x:Key="btnStyleRed" TargetType="Button">
                <Setter Property="Background" Value="Red"/>
            </Style>
 </Window.Resources>

只在您希望应用特定样式的按钮上使用此样式

<Button x:Name="btnLogin" Style="{StaticResource btnStyleRed}" Content="Login" Width="75" />

1
这仍然需要一个静态资源。 - Wobbles

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接