是否有任何方式可以在wpf中按名称进行特定控制

meh*_*hdi 5 wpf styles

通过资源设置控件颜色.

在资源文件中:

<style TargetType="{x:Name button1}">
      <setter Property="ColorBrush" Value="Red"/>
</style>
<Window .... >
    <Button Name="Button1" Content="Test Button" />
</Window>
Run Code Online (Sandbox Code Playgroud)

喜欢html中的css

 <style>
       #controlID
       {
         color:red;
       }
    </style>
<body>
      <input id="button1" type="button" value="test button" />
</body>
Run Code Online (Sandbox Code Playgroud)

shr*_*sha 7

在 window.resources 中定义这样的样式

<Window.Resources>
    <Style x:Key="btnStyleRed" TargetType="Button">
                <Setter Property="Background" Value="Red"/>
            </Style>
 </Window.Resources>
Run Code Online (Sandbox Code Playgroud)

并仅在您要应用特定样式的按钮上使用此样式

<Button x:Name="btnLogin" Style="{StaticResource btnStyleRed}" Content="Login" Width="75" />
Run Code Online (Sandbox Code Playgroud)