【问题标题】:WPF Multibinding in UserControl not accepted不接受用户控件中的 WPF 多绑定
【发布时间】:2013-09-10 07:39:03
【问题描述】:

我有以下代码:

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

有人可以帮忙吗?

【问题讨论】:

    标签: c# wpf xaml user-controls multibinding


    【解决方案1】:

    您将带有相关源的 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>
    

    您说DataContext 对UserControl 工作正常,因此您无需指定任何来源。

    【讨论】:

    • 谢谢! IntelliSense 警告完全令人困惑。
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2013-06-06
    • 2021-08-10
    • 2014-01-14
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多