如何从父窗口自定义usercontrol

时间:2012-05-06 05:30:12

标签: c# wpf xaml user-controls

我想知道如何从父窗口更改用户控件,我将usercontrol放入其中。

我有一个用户控件,其中包含网格和数据网格。现在我想在我的窗口中更改数据网格属性...我想在我的网格中添加另一个控件。 像这样的事情

<window>
<usercontrol>
<usercontrol.datagrid backcolor=#ff00000>
<usercontrol/>
<window/>

或者我可以像这样的代码一样将文本块添加到usercontrol网格中:

<window>
<usercontrol.grid>
<textblock grid.row=1/>
<usercontrol.grid/>
<window/>

用户控件中的所有元素都是公开,所以我可以从c#代码进行更改,但我想用 xaml 设计模式进行更改

在Windows窗体中我创建一个用户控件继承数据网格视图然后我自定义它。我在10个窗口和第11个窗口中使用它我需要更改数据网格视图有点我不更改usercontrol因为它更改所有窗口所以我只是更改该usercontrol是在第11个窗口 请帮忙!

1 个答案:

答案 0 :(得分:3)

我认为你应该在你的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 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的内部控制问题。 =)

相关问题