下面的代碼將創建兩個按鈕,但風格將只應用於第二我知道我可以使用模板,而我想知道爲什麼這個設置不起作用?爲什麼內容設置器僅適用於最後一個控件?
<Window x:Class="WpfApplication9.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 TargetType="Button">
<Setter Property="Content">
<Setter.Value>
<Grid>
<TextBlock Text="help"></TextBlock>
</Grid>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Row="1" Grid.Column="0"></Button>
<Button Grid.Row="1" Grid.Column="1"></Button>
</Grid>
使用模板或將共享屬性設置爲false會更好嗎?或者這是否具有相同的性能影響? – Bob 2014-10-20 20:29:42
這只是另一種做事的方式,但我個人可以選擇模板。如果你可以避免它,你不應該使用視覺樣式設置器 – dkozl 2014-10-20 20:43:22
同意,如果你想堅持WPF的推薦做法,使用模板。在將近十年的WPF開發中,我從未發現有必要使用'x:Shared',並且這條路線基本上是一種處理規則和最佳實踐的方式。這就是說,這是一個有用的事情要知道。 – 2014-10-20 21:00:18