對於WPF而言並不新鮮,並且具有Tabs,並且在每個選項卡中內容呈現在彎曲的角落面板/窗口/ whateveryouwannacallit中。我不知道如何做到這一點(Style,ControlTemplate),但決定採用DataTemplate方式。在內容中設置的WPF DataTemplate屬性
所以現在我有這樣的DataTemplate:
<DataTemplate x:Key="TabContentPresenter" >
<Border Margin="10"
BorderBrush="{StaticResource DarkColorBrush}"
CornerRadius="8"
BorderThickness="2"
Grid.Row="0"
Padding="5"
Background="{TemplateBinding Background}">
<ContentPresenter Content="{Binding}" />
</Border>
</DataTemplate>
正如你可以與背景屬性我wan't設置在內容的背景顏色,但不知道怎麼看。我在這裏使用它。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Background="White">
<!-- Something Here -->
</ContentControl>
<ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Grid.Row="1" Background="Blue">
<!-- Something Here -->
</ContentControl>
</Grid>
是否在這裏使用DataTemplate錯誤還是有其他方法嗎?
我可以直接在內容上設置背景,並從模板中的填充更改爲內容中的邊距,但在某些類似的情況下不起作用,只需設置一次即可。
編輯:
按照建議我改的ControlTemplate,並把它放在裏面的風格。這解決了背景問題,但創建了一個更大的問題。現在內容不會出現。我在博客here上看到,把targetType解決這個問題,但它並沒有解決我的問題。代碼現在看起來像這樣,也改變了ContentControl使用樣式而不是Template。
<Style x:Key="TabContentPresenter" TargetType="ContentControl" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Border Margin="10"
BorderBrush="{StaticResource DarkColorBrush}"
CornerRadius="8"
BorderThickness="2"
Grid.Row="0"
Background="{TemplateBinding Background}">
<ContentPresenter Content="{Binding}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
這適用於背景,但現在我的內容不會出現在邊框內。 – 2010-06-25 16:33:54
最後發現,必須將ContentPresenter內容屬性更改爲{TemplateBinding Content}。 – 2010-06-28 16:49:40