6
我是一個XAML新手,看着這個博客重寫按鈕與控件模板不顯示內容
http://jeremybytes.blogspot.com/2009/03/wpf-xaml-sample.html
他提到「請注意,他們沒有任何內容(Button1a,Button1b,等等),這是因爲我們的模板不包含內容元素。「我正在考慮如何確保在應用新按鈕模板之後內容仍然保留。這裏是XAML
<Page.Resources>
<ControlTemplate TargetType="Button" x:Key="targetButton">
<Canvas Width="100" Height="100" Name="mainCanvas">
<Rectangle Height="100" Width="100" Fill="Aqua" Canvas.Top="1"/>
</Canvas>
</ControlTemplate>
<Style TargetType="Button">
<Setter Property="Margin" Value="5, 5, 5, 5"/>
<Setter Property="Template" Value="{StaticResource targetButton}"/>
</Style>
</Page.Resources>
<StackPanel DataContext="{Binding Board}">
<StackPanel Orientation="Horizontal">
<Button Name="testButton1a" Click="testButton1a_Click" Content="testButton1a"/>
<Button Name="testButton1b" Click="testButton1a_Click" Content="testButton1b"/>
<Button Name="testButton1c" Click="testButton1a_Click" Content="testButton1c"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button x:Name="testButton2a" Click="testButton1a_Click" Content="testButton2a"/>
<Button x:Name="testButton2b" Click="testButton1a_Click" Content="testButton2b"/>
<Button x:Name="testButton2c" Click="testButton1a_Click" Content="testButton2c"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Name="testButton3a" Click="testButton1a_Click" Content="testButton3a"/>
<Button Name="testButton3b" Click="testButton1a_Click" Content="testButton3b"/>
<Button Name="testButton3c" Click="testButton1a_Click" Content="testButton3c"/>
</StackPanel>
</StackPanel>
我試圖在模板中添加TextBlock的,但我看到所有的按鈕覆蓋在同一文本塊。我只想渲染矩形按鈕矩陣上顯示的testButton *內容,這樣我就可以更改一次點擊的內容。
謝謝,任何幫助將不勝感激
大。這工作,非常感謝! – Yam 2013-05-06 18:10:47