C# 如何从父窗口自定义usercontrol
我想知道如何从父窗口更改用户控件,我将usercontrol放入父窗口 我有一个用户控件,其中包含一个网格和数据网格。现在我想在我的窗口中更改数据网格属性。。。我想在网格中添加另一个控件。 像这样的东西C# 如何从父窗口自定义usercontrol,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我想知道如何从父窗口更改用户控件,我将usercontrol放入父窗口 我有一个用户控件,其中包含一个网格和数据网格。现在我想在我的窗口中更改数据网格属性。。。我想在网格中添加另一个控件。 像这样的东西 <window> <usercontrol> <usercontrol.datagrid backcolor=#ff00000> <usercontrol/> <window/> 或者我可以像下面的代码那样在usercontrol
<window>
<usercontrol>
<usercontrol.datagrid backcolor=#ff00000>
<usercontrol/>
<window/>
或者我可以像下面的代码那样在usercontrol网格中添加一个textblock:
<window>
<usercontrol.grid>
<textblock grid.row=1/>
<usercontrol.grid/>
<window/>
用户控件中的所有元素都是公共的,因此我可以从c#代码中进行更改,但我希望使用xaml设计模式进行更改
在windows窗体中,我创建了一个从数据网格视图继承的用户控件,然后对其进行了自定义。我在10个窗口中使用它,在第11个窗口中,我需要稍微更改数据网格视图,我不更改usercontrol,因为它更改了所有窗口,所以我只更改usercontrol在第11个窗口中
请帮忙 我认为您应该在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);}
}
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中将DataGrid的Color属性绑定到它:
<DataGrid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=YourControlType}, Path=GridColor}"/>
<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>
UserControl的XAML:
<DataGrid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=YourControlType}, Path=GridColor}"/>
<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的内部控件。=) 我认为您应该在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);}
}
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中将DataGrid的Color属性绑定到它:
<DataGrid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=YourControlType}, Path=GridColor}"/>
<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>
UserControl的XAML:
<DataGrid Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=YourControlType}, Path=GridColor}"/>
<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的内部控件。=) 谢谢你的帮助我很晚才读到你留言的最后几行你回答了所有的问题:)哇,答案很清楚很简单。。。这对我帮助很大。谢谢;)谢谢你的帮助我很晚才读到你留言的最后几行你回答了所有的问题:)哇,答案很清楚很简单。。。这对我帮助很大。谢谢;)