我想你應該爲你的DataGrid的BACKGROUNDCOLOR一個DependencyProperty(或任何屬性,你想改變)背後的用戶控件的代碼中:
public static DependencyProperty GridColorProperty = DependencyProperty.Register("GridColor", typeof (Brush),
typeof (UserControl1),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions
.AffectsRender));
public Brush GridColor
{
get { return (Brush)GetValue(GridColorProperty); }
set { SetValue(GridColorProperty, value);}
}
後,您必須將DataGrid的顏色屬性綁定到它在用戶控件的XAML:
<DataGrid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=YourControlType}, Path=GridColor}"/>
現在你可以使用這樣的控制:
<YourControlType GridColor="Green"/>
至於控件的添加,它取決於你想要達到什麼樣的前景。最直接的方法是從網格中派生用戶控件。或者可能從ContentControl派生出來就足以滿足您的需求
編輯: 這就是您可以如何放入新的控件。從電網獲得您的控制:
<Grid x:Class="WpfApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:app="clr-namespace:WpfApplication3" mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<DataGrid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=app:YourControlType}, Path=GridColor}"/>
</Grid>
而且你會使用它這樣的:
<YourControlType GridColor="Green">
<Button Grid.Row="1"/>
</YourControlType>
但實際上它是做一個非常奇怪的事情,我會更好地從ContentControl中獲得它:
<ContentControl x:Class="WpfApplication3.YourControlType"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:app="clr-namespace:WpfApplication3" mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<DataGrid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=app:YourControlType}, Path=GridColor}"/>
<ContentPresenter Content="{TemplateBinding Content}" Grid.Row="1"/>
</Grid>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
這是你如何使用它:
<YourControlType GridColor="Green">
<Button/>
</YourControlType>
另一種可能性是您可以爲控件的內容創建依賴項屬性。後面的代碼:
public static readonly DependencyProperty InnerContentProperty =
DependencyProperty.Register("InnerContent", typeof (FrameworkElement), typeof (YourControlType),
new FrameworkPropertyMetadata(default(FrameworkElement),
FrameworkPropertyMetadataOptions.AffectsRender));
public FrameworkElement InnerContent
{
get { return (FrameworkElement) GetValue(InnerContentProperty); }
set { SetValue(InnerContentProperty, value); }
}
UserControl的XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<DataGrid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=WpfApplication3:UserControl1}, Path=GridColor}"/>
<ContentControl Grid.Row="1" Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=app:YourControlType}, Path=InnerContent}"/>
</Grid>
用法:
<YourControlType GridColor="Green">
<YourControlType.InnerContent>
<Button/>
</YourControlType.InnerContent>
</YourControlType>
但是如果你想只是一個快速和簡單的回答你最初的問題,因爲它規定,沒有您可以直接從XAML處理UserControl的內部控件。 =)
感謝幫助完整我讀了你的消息的最後一行,你回答所有問題:) –
哇這是清晰和簡單的答案...它對我很有幫助。謝謝 ;) –