有没有办法在 wpf 中按名称设置特定控件的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31916738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:19:46 来源:igfitidea点击:
Is There any way for style for specific control by name in wpf
提问by mehdi
Set control color via resource.
通过资源设置控件颜色。
in resource file:
在资源文件中:
<style TargetType="{x:Name button1}">
<setter Property="ColorBrush" Value="Red"/>
</style>
<Window .... >
<Button Name="Button1" Content="Test Button" />
</Window>
like css in html
就像 html 中的 css
<style>
#controlID
{
color:red;
}
</style>
<body>
<input id="button1" type="button" value="test button" />
</body>
回答by shreesha
Define a Style like this in window.resources
在 window.resources 中定义这样的样式
<Window.Resources>
<Style x:Key="btnStyleRed" TargetType="Button">
<Setter Property="Background" Value="Red"/>
</Style>
</Window.Resources>
and use this style only on those button to which you want to apply particular style
并仅在您要应用特定样式的按钮上使用此样式
<Button x:Name="btnLogin" Style="{StaticResource btnStyleRed}" Content="Login" Width="75" />

