Вопрос

I have the following Code:

<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>

This works fine for me. But now I want to refactor the template to its own file. I tried the following:

New File 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>

Original File:

<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>

This does'nt work (nothing is shown). There is no error while compile time and no error in debug output but IntelliSense shows Cannot add instance of type 'Multibinding' to a collection of type 'PathFigureCollection'. Only items of type 'PathFigure' are allowed. I don't understand this because its exactly the same code for the Path-Object than before.

If I add another control to the grid e.g.:

<Label Content="{Binding Source.ActualWidth}"/>) 

the conrol and the data binding works without problems.

The DataContext of the new UserControl seems to be fine (it's of type EdgeControl)

I'm using Visual Studio 2012 Update 3

Can anybody help?

Это было полезно?

Решение

You left the MultiBinding with the related source to the TemplatedParent, but you no longer have a templated parent. Remove it from the bindings, and I think it would work. Your MultiBinding should look like this:

<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>

You said the DataContext to the UserControl works fine, so you don't need to specify any source.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top