2014-09-04 30 views
2

邊框模板假設我有以下控件模板:WPF - 與內容

<ControlTemplate x:Key="Test"> 
    <Grid> 
     <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Width="33" Height="33" CornerRadius="3"/> 
     <ContentControl Content="{TemplateBinding Property=ContentControl.Content}" VerticalAlignment="Center" HorizontalAlignment="Center"/> 
    </Grid> 
</ControlTemplate> 

我如何更改WPF控件的內容?我試着像

<Control Template="{StaticResource Test}" BorderBrush="Black" Content="aa"></Control> 

但是,當我這樣做,我上面說我的財產內容不認可,或者是找不到的。

+1

控制是一個基類,並且沒有內容屬性。 – Maximus 2014-09-04 13:06:40

回答

3

您需要自行使用ContentControl來做你想做的事......清楚的是,ContentControl元素與Control元素無關。它用於顯示數據對象,並可選擇將DataTemplate應用於對象。該DataTemplate是,你可以自定義部分:

<ContentControl Content="{Binding SomeDataObject}" 
    ContentTemplate="{StaticResource SomeDataObjectTemplate}" /> 

...

在一些Resources集合:

<DataTemplate x:Key="SomeDataObjectTemplate" DataType="{x:Type Prefix:SomeDataObject}"> 
    <Grid> 
     <Border BorderBrush="Black" BorderThickness="1" CornerRadius="3" /> 
     <TextBlock Text="{Binding}" /> 
    </Grid> 
</DataTemplate> 

你唯一的選擇就是宣佈UserControl和暴露的某些部分標記爲DependencyProperty s,您可以將數據與控件外部的數據綁定:

<Prefix:YourUserControl CustomContent="{Binding SomeDataObject}" /> 

控制內部:

<ContentControl Content="{Binding CustomContent, 
    RelativeSource={RelativeSource AncestorType={x:Type Local:YourUserControl }}}" /> 
1

由於Control不是從ContentControl派生的,因此它不公開Content屬性。查看更多信息here