我定義了一個自定義的WPF樣式。我想要網格中的任何按鈕都是紅色的。但如果我定義這種風格,整個網格是紅色!爲什麼?我明確定義了Button.Background。爲什麼WPF樣式應用於父控件?
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="MyStyle">
<Setter Property="Button.Background" Value="Red" /> <!-- Only inner buttons -->
</Style>
</Window.Resources>
<Grid Style="{StaticResource MyStyle}">
<Button Content="Go" Margin="29,36,385,239" />
</Grid>
</Window>
不,因爲樣式必須應用於網格。並應包含不同內部控件的所有不同樣式 – Robert 2011-02-10 13:16:07