我是C#的初学者。我试图使用以下xaml:
在我的可视化C#WPF应用程序中为我的按钮设置一些属性<Grid>
<Button Panel.ZIndex="1" Content="Button" Height="37" HorizontalAlignment="Stretch"
Margin="378,12,38,262" Name="button1" VerticalAlignment="Stretch" Width="87" >
<Button.Resources>
<Style x:Key="buttonStyle">
<Setter Property="Button.Background" Value="Gray"/>
<Setter Property="Button.Foreground" Value="White"/>
<Setter Property="Button.FontFamily" Value="Comic Sans MS"/>
</Style>
</Button.Resources>
</Button>
<Image Margin="0,0,0,0" Name="image1">
<Image.Source>
<BitmapImage UriSource="images.jpg"></BitmapImage>
</Image.Source>
</Image>
</Grid>
但Button.Resource对按钮的样式没有影响。任何人都可以告诉我这个问题
答案 0 :(得分:1)
替换
<Button Panel.ZIndex="1" Content="Button" Height="37" HorizontalAlignment="Stretch"
Margin="378,12,38,262" Name="button1" VerticalAlignment="Stretch" Width
与
<Button Style="{DynamicResource buttonStyle}" Panel.ZIndex="1"
Content="Button" Margin ="169,84,34,0" Name="button1" Height="23"
VerticalAlignment="Top">
答案 1 :(得分:1)
您创建了明确的Button
样式,但不要在任何地方使用它。您可以直接针对按钮
<Button ... Background="Gray" Foreground="White" FontFamily="Comic Sans MS"/>
或者,如果您想要创建明确的Style
,则需要在Button
<Grid>
<Grid.Resources>
<Style x:Key="buttonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontFamily" Value="Comic Sans MS"/>
</Style>
</Grid.Resources>
<Button ... Style="{StaticResource buttonStyle}"/>
</Grid>
或者您可以创建隐式样式,它将自动应用于当前可视树中的子Button
控件
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontFamily" Value="Comic Sans MS"/>
</Style>
</Grid.Resources>
<Button .../>
</Grid>
或仅为Style
Button
<Button>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontFamily" Value="Comic Sans MS"/>
</Style>
</Button.Style>
</Button>
答案 2 :(得分:1)
<Window.Resources>
<Style x:Key="ButtonStyle1"
TargetType="{x:Type Button}">
<Setter Property="Button.Background" Value="Gray"/>
<Setter Property="Button.Foreground" Value="White"/>
<Setter Property="Button.FontFamily" Value="Comic Sans MS"/>
</Style>
</Window.Resources>
<Grid>
<Button Panel.ZIndex="1" Content="Button" Height="37" HorizontalAlignment="Stretch"
Margin="378,12,38,262" Name="button1" VerticalAlignment="Stretch" Style="{StaticResource ButtonStyle1}" Width="87" >
</Button>
<Image Margin="0,0,0,0" Name="image1">
</Image>
</Grid>
答案 3 :(得分:0)
你可以这样写:
<Button Panel.ZIndex="1" Content="Button" Height="37" HorizontalAlignment="Stretch"
Margin="378,12,38,262" x:Name="button1"
VerticalAlignment="Stretch" Width="87"
Background="Gray" Foreground="White" FontFamily="Comic Sans MS" />