如何在 ViewBox 中引用现有的 DataTemplate? [英] How to reference an existing DataTemplate in a ViewBox?
问题描述
在一个资源字典文件中,我有一个带有不同数据模板的 DataTemplateDictionary
.假设有一个 DataTemplateA 和一个 DataTemplateB,它们是独特的形状.对于 DataTemplateC,我想重新使用 DataTemplateA,但它将被缩小并用作预览.我读过我应该使用 ViewBox 但经过几次尝试后它没有工作.这是 DataTemplateA 的一个片段:
In one Resource Dictionary file I have a DataTemplateDictionary
with different DataTemplates. Let us say there is a DataTemplateA, and a DataTemplateB which are unique shapes. For DataTemplateC, I want to re-use DataTemplateA but it will be scaled down and used as a preview. I've read that I should use a ViewBox but after several attempts it has not worked.
Here is a snippet of 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 的一个片段:
Here is a snippet of 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 填充矩形,但这不起作用.我还尝试了以下方法:
As you can see I am trying to fill the rectangle with an existing DataTemplate but this does not work. I have also tried the following:
<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>
我只是不确定如何完成如此简单的任务.非常感谢任何建议或帮助,或者我应该研究其他方法吗?
I am just unsure of how to do such simple task. Any advice or help is highly appreciated, or is there another method I should look into?
这是我的 xaml 文件,DataTemplateA 是我的 BreakerLA,DataTemplateC 是我的 sBreakerLA:
Here is my xaml file, DataTemplateA is my BreakerLA and DataTemplateC is my 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的函数:
And this is the function that grabs the 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="."
下面):
You could apply the DataTemplate
to a ContentControl
with some dummy content like for example a simple dot (Content="."
below):
<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>
这篇关于如何在 ViewBox 中引用现有的 DataTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!