我有以下代码:
<Style TargetType="{x:Type gc:EdgeControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type gc:EdgeControl}">
<Path Stroke="Black"
StrokeThickness="1"
MinWidth="1"
MinHeight="1"
ToolTip="Transition"
x:Name="edgePath">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<MultiBinding Converter="{StaticResource routeToPathConverter}">
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Source.(gc:GraphCanvas.X)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Source.(gc:GraphCanvas.Y)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Source.ActualWidth" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Source.ActualHeight" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Target.(gc:GraphCanvas.X)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Target.(gc:GraphCanvas.Y)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Target.ActualWidth" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Target.ActualHeight" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="RoutePoints" />
</MultiBinding>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这对我来说很好。但现在我想将模板重构为自己的文件。我尝试了以下方法:
新文件 FsmTransitionControl.xaml
<UserControl x:Class="LogicEditor.View.FsmTransitionControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:gc="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls"
xmlns:viewmodel="clr-namespace:LogicEditor.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<viewmodel:EdgeRouteToPathConverter x:Key="routeToPathConverter" />
</UserControl.Resources>
<Grid>
<Path Stroke="Black"
StrokeThickness="1"
MinWidth="1"
MinHeight="1"
ToolTip="Transition"
x:Name="edgePath">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<MultiBinding Converter="{StaticResource routeToPathConverter}">
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Source.(gc:GraphCanvas.X)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Source.(gc:GraphCanvas.Y)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Source.ActualWidth" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Source.ActualHeight" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Target.(gc:GraphCanvas.X)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Target.(gc:GraphCanvas.Y)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Target.ActualWidth" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Target.ActualHeight" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="RoutePoints" />
</MultiBinding>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
</Grid>
</UserControl>
原始文件:
<Style TargetType="{x:Type gc:EdgeControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type gc:EdgeControl}">
<view:FsmTransitionControl DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这不起作用(没有显示)。编译时没有错误,调试输出中也没有错误,但 IntelliSense 显示无法将“Multibinding”类型的实例添加到“PathFigureCollection”类型的集合中。只允许“PathFigure”类型的项目。我不明白这一点,因为它的 Path-Object 代码与以前完全相同。
如果我向网格添加另一个控件,例如:
<Label Content="{Binding Source.ActualWidth}"/>)
控制和数据绑定工作没有问题。
新 UserControl 的 DataContext 似乎很好(它的类型为 EdgeControl)
我正在使用 Visual Studio 2012 更新 3
有人可以帮忙吗?