如何在ViewBox中引用现有的DataTemplate?

时间:2017-08-23 19:49:42

标签: c# wpf xaml

在一个资源字典文件中,我有一个DataTemplateDictionary,其中包含不同的DataTemplates。让我们说有一个DataTemplateA和一个DataTemplateB是唯一的形状。对于DataTemplateC,我想重新使用DataTemplateA,但它将按比例缩小并用作预览。我已经读过我应该使用ViewBox,但经过多次尝试后它还没有用。 以下是DataTemplateA的片段:

<DataTemplate x:Key="DataTemplateA">
    <Grid ShowGridLines="False" Margin="0,0,0,0"  Visibility="Visible" Width="auto" Height="auto" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.ColumnDefinitions>                        
            <ColumnDefinition Width="174" />
            <ColumnDefinition Width="126"/>                        
        </Grid.ColumnDefinitions>
        <Canvas>
            <Path Grid.RowSpan="2" Grid.ColumnSpan="2" Fill="#bcc5d2" Stroke="#5075ba" StrokeThickness="1" 
            Data="M 11,0 287,0 
                Q 299,0 299,11
                L 299,11 299,87
                Q 299,99 287,99
                L 287,99 11,99
                Q 0,99 0,87
                L 0,87 0,11
                Q 0,0 11,0 Z"
            Margin="0,0,0,0" Stretch="UniformToFill" />
        </Canvas>                   
    </Grid>            
</DataTemplate>

以下是DataTemplateC的片段:

    <DataTemplate x:Key="DataTemplateC">
        <go:NodePanel Sizing="Fixed" go:Part.SelectionAdorned="True" ToolTip="Breaker Left Align" go:Node.Location="{Binding Path=Data.Location, Mode=OneWay}">
            <Viewbox Width="50" Height="30">
                <Rectangle  x:Name="FusedCB_VIcon" 
                       RadiusX="0" RadiusY="0" Stroke="Black" StrokeThickness="0"
                       Fill="{StaticResource DataTemplateA}">
                </Rectangle>
            </Viewbox>               
        </go:NodePanel>
    </DataTemplate>

正如您所看到的,我正在尝试使用现有的DataTemplate填充矩形,但这不起作用。我也尝试了以下内容:

    <DataTemplate x:Key="DataTemplateC">
        <go:NodePanel Sizing="Fixed" go:Part.SelectionAdorned="True" ToolTip="Breaker Left Align" go:Node.Location="{Binding Path=Data.Location, Mode=OneWay}">
            <Viewbox Width="50" Height="30">
                <my:DataTemplateA/>
            </Viewbox>                
        </go:NodePanel>
    </DataTemplate>

我只是不确定如何做这么简单的任务。任何建议或帮助都受到高度赞赏,或是否还有其他方法需要研究?

这是我的xaml文件,DataTemplateA是我的BreakerLA,DataTemplateC是我的sBreakerLA:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="GoXamStyleDictionary.xaml" />
    <ResourceDictionary Source="ElevationImages.xaml" />
</ResourceDictionary.MergedDictionaries>

<go:DataTemplateDictionary x:Key="OneLineEditorDiagramTemplateDictionary">
    <DataTemplate x:Key="Breaker">
        <go:NodePanel Sizing="Fixed" go:SpotPanel.Main="True" go:Part.SelectionAdorned="True" ToolTip="Breaker" go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}">
            <Rectangle Width="50" Height="50" x:Name="CB_VIcon"
                       RadiusX="0" RadiusY="0" Stroke="Transparent" StrokeThickness="0"
                       Cursor="Hand" Fill="{StaticResource CB_V}">
            </Rectangle>
            <TextBlock Style="{StaticResource TextBlockStyle}"
                       Text="{Binding Path=Data.Text, Mode=TwoWay}" />
        </go:NodePanel>
    </DataTemplate>

    <DataTemplate x:Key="BreakerLA">
        <Border BorderThickness="1" BorderBrush="{Binding Path=Data.Held, Converter={StaticResource heldConverter}}"
                Background="Transparent" x:Name="BreakerIcon"
                Width="Auto" Height="Auto"
                TouchDown="touchDownHandler" TouchUp="touchUpHandler"
                TouchMove ="touchMoveHandler" TouchLeave="touchLeaveHandler"
                Stylus.IsPressAndHoldEnabled="False"
                go:Node.Movable="False"
                go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}" 
                go:Node.LocationSpot="TopLeft"
                go:Part.SelectionAdorned="True"
                go:Part.SelectionElementName="BreakerIcon"
                go:Part.SelectionAdornmentTemplate="{StaticResource NodeSelectionAdornmentTemplate}"
                go:Part.Resizable="False"
                go:Part.ResizeElementName="BreakerIcon"
                go:Part.ResizeAdornmentTemplate="{StaticResource NodeResizeAdornmentTemplate}"
                go:Node.RotationAngle="{Binding Path=Data.Angle, Mode=TwoWay}"
                go:Part.Rotatable="False"
                go:Part.DragOverSnapEnabled="True"
                go:Part.DragOverSnapCellSpot="TopLeft"
                go:Part.RotateAdornmentTemplate="{StaticResource NodeRotateAdornmentTemplate}">
            <!--Breaker info-->
            <Grid ShowGridLines="False" Margin="0,0,0,0"  Visibility="Visible" Width="auto" Height="auto" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Grid.ColumnDefinitions>                        
                    <ColumnDefinition Width="174" />
                    <ColumnDefinition Width="126"/>                        
                </Grid.ColumnDefinitions>
                <Canvas>
                    <Path Grid.RowSpan="2" Grid.ColumnSpan="2" Fill="#bcc5d2" Stroke="#5075ba" StrokeThickness="1" 
                    Data="M 11,0 287,0 
                        Q 299,0 299,11
                        L 299,11 299,87
                        Q 299,99 287,99
                        L 287,99 11,99
                        Q 0,99 0,87
                        L 0,87 0,11
                        Q 0,0 11,0 Z"
                    Margin="0,0,0,0" Stretch="UniformToFill" />
                </Canvas>
                <!--Data Labels-->
                <StackPanel Grid.Row="0" Grid.Column="0" Margin="0,0,12,0"  HorizontalAlignment="Right" VerticalAlignment="Center">
                    <TextBlock Text="{Binding Path=Data.OneLineBreakerData.BreakerName, Mode=TwoWay}" FontFamily="XX Inspira Medium" 
                               FontSize="16px" Foreground="Black" HorizontalAlignment="Right" FontWeight="DemiBold"
                                LineStackingStrategy="BlockLineHeight" LineHeight="16"/>
                    <TextBlock Text="{Binding Path=Data.OneLineBreakerData.BreakerCompartmentNo, Mode=TwoWay}" FontFamily="XX Inspira Regular"
                               FontSize="15px" Foreground="Black" HorizontalAlignment="Right"
                                LineStackingStrategy="BlockLineHeight" LineHeight="18"/>
                    <TextBlock Text="{Binding Path=Data.OneLineBreakerData.PTRating, Mode=TwoWay}" FontFamily="XX Inspira Regular" 
                               FontSize="15px" Foreground="Black" HorizontalAlignment="Right"
                                LineStackingStrategy="BlockLineHeight" LineHeight="16"/>
                </StackPanel>
                <!--Breaker Node and Icon Info-->
                <Grid ShowGridLines="False" VerticalAlignment="Top" HorizontalAlignment="Right" Grid.Column="1" Height="98" Margin="0,1,12,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="52" />
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <go:SpotPanel MouseEnter="Node_MouseEnter" MouseLeave="Node_MouseLeave" Grid.Column="0" Margin="-12,0,0,0">
                        <go:NodePanel Sizing="Fixed" go:SpotPanel.Main="True" Width="52" Height="90"  Margin="0,4,0,0" VerticalAlignment="Top">
                            <Rectangle x:Name="CB_VIcon" RadiusX="0" RadiusY="0" Stroke="Transparent" StrokeThickness="0" Margin="0,0,0,0"
                                Cursor="Hand" Fill="{StaticResource CB_V}" SnapsToDevicePixels="True">
                            </Rectangle>
                    </go:SpotPanel>
                    <Grid Grid.Column="1" VerticalAlignment="Center" Margin="0,0,0,0" HorizontalAlignment="Center" Height="100">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="TRIP" Margin="0,0,0,0" Foreground="#df2026" FontSize="16" 
                                       TextWrapping="NoWrap" FontFamily="XX Inspira Medium" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" />
                        <Rectangle x:Name="Info_Icon" RadiusX="0" RadiusY="0" Stroke="Transparent" StrokeThickness="0" Margin="0,0,0,7" HorizontalAlignment="Center"
                                   Height="30" Width="30" VerticalAlignment="Bottom"  Fill="{StaticResource info_Icon}" SnapsToDevicePixels="True">
                        </Rectangle>
                    </Grid>
                </Grid>
            </Grid>
        </Border>
    </DataTemplate>

    <DataTemplate x:Key="sBreakerLA">
        <go:NodePanel Sizing="Fixed" go:Part.SelectionAdorned="True" ToolTip="Breaker Left Align" go:Node.Location="{Binding Path=Data.Location, Mode=OneWay}">
            <Viewbox Width="50" Height="30">
                <ContentControl Content="." ContentTemplate="{StaticResource BreakerLA}" />
            </Viewbox>
        </go:NodePanel>
    </DataTemplate>
</go:DataTemplateDictionary>

这是抓取DataTemplate的函数:

private void CB_VerticalSelection(Node brkNode)

{
        editorDiagram.StartTransaction("DroppedBreaker");
        OneLineModelData brkData = brkNode.Data as OneLineModelData;
        System.Console.WriteLine("Test");
        editorDiagram.GroupTemplate = resGroupTempdict["PaletteGroupTemplate"] as DataTemplate;
        //editorDiagram.NodeTemplateDictionary = resPaletteDictionary["OneLinePaletteTemplateDictionary"] as DataTemplateDictionary;
        editorDiagram.Model.RemoveNode(brkData);
        // Populates the pop-up with the following nodes
        editorDiagram.Model.AddNode(new OneLineModelData()
        { Key = "0", Text = "", Category = "sBreakerLA", SubGraphKey = "BG1",
            Location = new System.Windows.Point(nodeDropLocation.X, nodeDropLocation.Y + 20) });
        editorDiagram.CommitTransaction("DroppedBreaker");
    }

我得到的错误: enter image description here

1 个答案:

答案 0 :(得分:0)

您可以将DataTemplate应用于带有虚拟内容的ContentControl,例如简单的点(Content="."下方):

<DataTemplate x:Key="DataTemplateC">
    <go:NodePanel Sizing="Fixed" go:Part.SelectionAdorned="True" ToolTip="Breaker Left Align" go:Node.Location="{Binding Path=Data.Location, Mode=OneWay}">
        <Viewbox Width="50" Height="30">
            <ContentControl Content="." ContentTemplate="{StaticResource DataTemplateA}" />
        </Viewbox>
    </go:NodePanel>
</DataTemplate>