繁体   English   中英

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

[英]How to reference an existing DataTemplate in a ViewBox?

在一个资源字典文件我有一个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");
    }

我得到的错误: 在此处输入图片说明

您可以将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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM