如何减少我的代码,几乎相同的结构数量可能非常大

发布于 2024-12-05 05:25:53 字数 5285 浏览 1 评论 0原文

也许这是一个非常简单的问题,但我找不到答案: 如何减少我的代码,几乎相同的结构数量可能会非常大:

例如

<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>

等等...

Maybe this is a very simple question, but I can't find the answer:
How to reduce my code, the number of nearly identical structures can be very large:

For example

<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>

and so on...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后eg是否自 2024-12-12 05:25:53

您可以将相同的布局部分提取到单独的 DataTemplate 中,请参阅 DataTemplate 类。并且将相同的风格分成不同的风格。

看一下非常好的 MSDN 文章 控件自定义,它描述了模板、样式、部件和状态模型。

ScottGu 的精彩文章:

You can extract the same layout parts into separate DataTemplates, see DataTemplate class. And the same styles into separate styles.

Take a look at the very nice MSDN article Control Customization, it describes templates, styles, parts and state models.

Great articles by ScottGu's:

丶视觉 2024-12-12 05:25:53

您可以为常见元素创建样式。例如,为边框创建一种样式,为文本框创建一种样式等。然后在数据模板内的相应元素上应用这些样式。

这将使数据模板更小,并且内存占用也更小,因为样式数据不会重复。

You can create styles for common elements. For example create a style for the border, one for the text box etc. Then apply these styles inside the data template on the corresponding elements.

This will make the data templates smaller and also will make the memory footprint smaller since the style data does not get duplicated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文