c# – 如何减少我的代码,几乎相同结构的数量可以非常大
作者:互联网
也许这是一个非常简单的问题,但我找不到答案:
如何减少我的代码,几乎相同结构的数量可以非常大:
例如
<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>
等等…
解决方法:
您可以将相同的布局部件提取到单独的DataTemplates中,请参阅DataTemplate class.并将相同的样式分为不同的样式.
看一下非常好的MSDN文章Control Customization,它描述了模板,样式,部件和状态模型.
ScottGu的精彩文章:
> Using Style Elements to Better Encapsulate Look and Feel
> Using Control Templates to Customize a Control’s Look and Feel
标签:c,asp-net,silverlight,silverlight-4-0 来源: https://codeday.me/bug/20190721/1495202.html