2011-09-19 55 views
3

也許這是一個非常簡單的問題,但我找不到答案: 如何減少我的代碼,幾乎相同的結構數可以非常大:如何減少我的代碼,幾乎相同的結構數量可以非常大

例如

<Windows:HierarchicalDataTemplate x:Key="**Level2ItemTemplate**" 
             ItemsSource="{Binding Children}" 
             ItemTemplate="{StaticResource **Level3ItemTemplate**}"> 
     <Grid Height="100" 
       Width="100" 
       Margin="5"> 
      <Border Padding="5" 
        BorderThickness="1" 
        BorderBrush="#FFADADAD" 
        CornerRadius="5"> 
       <Border.Background> 
        <LinearGradientBrush EndPoint="0.5,1" 
             StartPoint="0.5,0"> 
         <GradientStop Color="#FFD47E00" 
             Offset="0" /> 
         <GradientStop Color="#FF563300" 
             Offset="1" /> 
        </LinearGradientBrush> 
       </Border.Background> 
       <TextBox TextWrapping="Wrap" 
         Text="{Binding Title, Mode=TwoWay}" 
         VerticalAlignment="Center" 
         HorizontalAlignment="Center" 
         FontWeight="Bold" 
         FontSize="13.333" 
         Style="{StaticResource EditableTitleStyle}" /> 
      </Border> 
      <Image HorizontalAlignment="Right" 
        Source="add.png" 
        Stretch="Fill" 
        Width="16" 
        VerticalAlignment="Bottom" 
        Margin="0,0,2,2" 
        Height="16"> 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="MouseLeftButtonDown"> 
         <Graph:AddSpiderItemAction /> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
      </Image> 
      <Image HorizontalAlignment="Right" 
        Height="16" 
        Source="remove.png" 
        Stretch="Fill" 
        VerticalAlignment="Top" 
        Width="16" 
        Margin="0,2,2,0"> 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="MouseLeftButtonDown"> 
         <Graph:RemoveSpiderItemAction /> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
      </Image> 
     </Grid> 
    </Windows:HierarchicalDataTemplate> 
    <Windows:HierarchicalDataTemplate x:Key="**Level1ItemTemplate**" 
             ItemsSource="{Binding Children}" 
             ItemTemplate="{StaticResource **Level2ItemTemplate**}"> 
     <Grid Height="100" 
       Width="100" 
       Margin="5"> 
      <Border Padding="5" 
        BorderThickness="1" 
        BorderBrush="#FFADADAD" 
        CornerRadius="5"> 
       <Border.Background> 
        <LinearGradientBrush EndPoint="0.5,1" 
             StartPoint="0.5,0"> 
         <GradientStop Color="#FFB5B5B5" 
             Offset="0" /> 
         <GradientStop Color="#FF474747" 
             Offset="1" /> 
        </LinearGradientBrush> 
       </Border.Background> 
       <TextBox TextWrapping="Wrap" 
         Text="{Binding Title, Mode=TwoWay}" 
         VerticalAlignment="Center" 
         HorizontalAlignment="Center" 
         FontWeight="Bold" 
         FontSize="13.333" 
         Style="{StaticResource EditableTitleStyle}" /> 
      </Border> 
      <Image HorizontalAlignment="Right" 
        Source="add.png" 
        Stretch="Fill" 
        Width="16" 
        VerticalAlignment="Bottom" 
        Margin="0,0,2,2" 
        Height="16"> 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="MouseLeftButtonDown"> 
         <Graph:AddSpiderItemAction /> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
      </Image> 
      <Image HorizontalAlignment="Right" 
        Height="16" 
        Source="remove.png" 
        Stretch="Fill" 
        VerticalAlignment="Top" 
        Width="16" 
        Margin="0,2,2,0"> 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="MouseLeftButtonDown"> 
         <Graph:RemoveSpiderItemAction /> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
      </Image> 
     </Grid> 
    </Windows:HierarchicalDataTemplate> 

等等...

+2

你知道模板和樣式,是嗎? –

+0

[link](http://stackoverflow.com/questions/7471356/how-to-reduce-my-code-the-number-of-nearly-identical-structures-can-be-very-larg)_italic_ **大膽**'是的,但我不知道如何實現這一點,因爲我有不同的x:Key和ItemTemplate'。 – revolutionkpi

回答

2

您可以創建常見元素的樣式。例如,爲邊框創建樣式,爲文本框創建樣式等。然後將這些樣式應用到相應元素的數據模板中。

這將使數據模板變小,並且由於樣式數據不會被重複,所以會減小內存佔用。

相關問題