Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 绑定画布。左侧和画布。样式为顶部_Wpf_Binding_Attached Properties - Fatal编程技术网

Wpf 绑定画布。左侧和画布。样式为顶部

Wpf 绑定画布。左侧和画布。样式为顶部,wpf,binding,attached-properties,Wpf,Binding,Attached Properties,我试图通过将项目视图样式中的Canvas.Left和Canvas.Top属性绑定到视图模型中的相应属性,来反映视图模型中表示对象在视图中位置的位置。但是,绑定似乎不起作用 对于这个最小的示例,我已经简化了结构,因此只有一个控件Thing是样式化和模板化的: using System; using System.Windows; using System.Windows.Controls; namespace LocationBinding { public class Thing :

我试图通过将项目视图样式中的
Canvas.Left
Canvas.Top
属性绑定到视图模型中的相应属性,来反映视图模型中表示对象在视图中位置的位置。但是,绑定似乎不起作用

对于这个最小的示例,我已经简化了结构,因此只有一个控件
Thing
是样式化和模板化的:

using System;
using System.Windows;
using System.Windows.Controls;

namespace LocationBinding
{
    public class Thing : Control
    {
        static Thing()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Thing), new FrameworkPropertyMetadata(typeof(Thing)));
        }

        public Point Location {
            get {
                return new Point(70, 70);
            }
        }

        public double VPos {
            get {
                return 100;
            }
        }
    }
}
为了简单起见,我在主窗口的资源字典中声明了样式——一个带有画布的简单窗口(我真正的项目在
Themes\Generic.xaml
中有这个样式)。在其样式中,我绑定到控件的属性值:

<Window x:Class="LocationBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:LocationBinding"
    Title="LocationBinding" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="local:Thing">
            <Setter Property="Panel.ZIndex" Value="542"/>
            <Setter Property="Canvas.Left" Value="{Binding Location.X}"/>
            <Setter Property="Canvas.Top" Value="{Binding VPos}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:Thing">
                        <Ellipse Fill="ForestGreen" Width="30" Height="30"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Canvas Name="cnv">

    </Canvas>
</Window>
样式已正确应用,这可以通过
ZIndex
值(根据Snoop)和控件基于模板的正确外观来证明。但是,
Canvas.Left
Canvas.Top
保持未设置状态(因此,
东西会粘在画布的左上角),即使根据或之类的线程,
Property=“Canvas.Left”
似乎是在样式中引用附加属性的正确语法

我第一次尝试将
Canvas.Top
绑定到
Location.Y
,并将其替换为
VPos
属性,以防问题与绑定到结构属性有关,但这似乎也不会改变任何东西

我错过了什么;如何以我的样式将
Canvas.Left
Canvas.Top
绑定到我的
Thing.Location
属性的坐标?

默认情况下,绑定将在控件的DataContext中搜索属性,但
Location
是控件(Thing)属性

因此,您需要使用模式设置为
Self
RelativeSource
,告诉绑定引擎在控件本身而不是控件的DataContext中搜索属性:

<Setter Property="Canvas.Left" Value="{Binding Location.X,
                                      RelativeSource={RelativeSource Self}}"/>

<Setter Property="Canvas.Left" Value="{Binding Location.X,
                                      RelativeSource={RelativeSource Self}}"/>