2012-05-06 49 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

哇這是清晰和簡單的答案...它對我很有幫助。謝謝 ;) –