我认为您应该在后面的 UserControl 代码中为 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);}
}
之后,您应该在 UserControl 的 XAML 中将 DataGrid 的 Color 属性绑定到它:
<DataGrid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=YourControlType}, Path=GridColor}"/>
现在您可以像这样使用控件:
<YourControlType GridColor="Green"/>
至于控件添加,这取决于您要达到的目标。最直接的方法是从网格派生您的用户控件。或者可能来自 ContentControl 就足以满足您的目的
编辑:这就是您可以放入新控件的方式。从 Grid 派生控件:
<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); }
}
用户控件的 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 的内部控件。= )