1

我有以下代码:

<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

有人可以帮忙吗?

4

1 回答 1

2

您将MultiBinding具有相关源的留给TemplatedParent,但您不再有模板化的父级。从绑定中删除它,我认为它会起作用。你MultiBinding应该是这样的:

<MultiBinding Converter="{StaticResource routeToPathConverter}">
  <Binding Path="Source.(gc:GraphCanvas.X)" />
  <Binding Path="Source.(gc:GraphCanvas.Y)" />
  <Binding Path="Source.ActualWidth" />
  <Binding Path="Source.ActualHeight" />
  <Binding Path="Target.(gc:GraphCanvas.X)" />
  <Binding Path="Target.(gc:GraphCanvas.Y)" />
  <Binding Path="Target.ActualWidth" />
  <Binding Path="Target.ActualHeight" />
  <Binding Path="RoutePoints" />
</MultiBinding>

DataContextUserControl作品说的很好,所以你不需要指定任何来源。

于 2013-09-10T11:03:11.160 回答