2012-05-06 60 views
0

我想知道如何从我的父窗口更改用户控件,并将其放入其中。如何从父窗口中自定义用户控件

我有一个用户控件,其中有一个网格和数据网格。现在我想在我的窗口中更改数据网格属性...并且我想将另一个控件添加到我的网格中。 一些这样的事

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

或我可以添加一个文本块到用户控件电网这样的代码:

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

在用户控件的所有元素都公共,所以我可以从C#代码做出改变,但我想这样做xaml设计模式

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

回答

3

我想你应该为你的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的内部控件。 =)

+0

感谢帮助完整我读了你的消息的最后一行,你回答所有问题:) –

+0

哇这是清晰和简单的答案...它对我很有帮助。谢谢 ;) –

相关问题