删除xaml数据模板重复

时间:2015-11-16 17:30:48

标签: c# wpf xaml

我正在尝试在我的UI中向两个扩展器面板添加工具提示。最后两个都需要一个按钮,上面有工具提示。所以,我所做的是我在Windows.Resources中添加了两个动态资源,如下所示。

<DataTemplate x:Key="PropertiesExpanderHeader">
            <StackPanel Orientation="Horizontal" Width="760">
                <Label Content="Reservoir properties" Width="120"></Label>
                <Button HorizontalAlignment="Right"  Height="24" IsEnabled ="{Binding IsSelected}" BorderBrush="Transparent"
                        VerticalAlignment="Top" Margin="560,0,0,0">
                    <StackPanel Orientation="Vertical" Margin="0,0,0,0">
                        <Image Source="../Resource/quickHelp.png" Width ="18" Height ="18"  />
                    </StackPanel>
                    <Button.ToolTip>
                        <StackPanel Width="360" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" >
                            <TextBlock TextWrapping="Wrap" Foreground="#FFF9F4F4" TextAlignment="Left" HorizontalAlignment="Left" VerticalAlignment="Top"   MaxWidth="360">
                                            <Run FontFamily="Verdana" FontSize="11" FontWeight="Normal" Foreground="#FF151514" Text="Input properties that can be used to find similar projects in the database

"></Run>
                            </TextBlock>
                        </StackPanel>
                    </Button.ToolTip>
                </Button>
            </StackPanel>

        </DataTemplate>
        <DataTemplate x:Key="DetailsExpanderHeader">
            <StackPanel Orientation="Horizontal" Width="760">
                <Label Content="Reservoir properties" Width="120"></Label>
                <Button HorizontalAlignment="Right"  Height="24" IsEnabled ="{Binding IsSelected}" BorderBrush="Transparent"
                        VerticalAlignment="Top" Margin="560,0,0,0">
                    <StackPanel Orientation="Vertical" Margin="0,0,0,0">
                        <Image Source="../Resource/quickHelp.png" Width ="18" Height ="18"  />
                    </StackPanel>
                    <Button.ToolTip>
                        <StackPanel Width="360" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" >
                            <TextBlock TextWrapping="Wrap" Foreground="#FFF9F4F4" TextAlignment="Left" HorizontalAlignment="Left" VerticalAlignment="Top"   MaxWidth="360">
                                            <Run FontFamily="Verdana" FontSize="11" FontWeight="Normal" Foreground="#FF151514" Text="1) Input project details  &#x0a;
2) Confidential project is only visible to the project team and super users, and hidden from common browsers."
></Run>
                            </TextBlock>
                        </StackPanel>
                    </Button.ToolTip>
                </Button>
            </StackPanel>

现在我通过在扩展器的Header字段上调用数据模板来使用它。

    <Expander Name="PropertiesExpander" IsExpanded="True" Header="Reservoir properties" HeaderTemplate="{DynamicResource PropertiesExpanderHeader}"  >

    <Expander Name="DetailsExpander" IsExpanded="True" Header="Details" HeaderTemplate="{DynamicResource DetailsExpanderHeader}"  >

如果您看到两个数据模板,除文本字段外,您可以看到它完全相同。我知道编程的黄金法则是避免重复。有关如何在不重复代码的情况下保留不同文本的任何指示?最好保持相同的动态资源实现吗?

0 个答案:

没有答案