附加 MVVM 属性 查看模型

MVVM Attached Property View Model

我在 MVVM 中遇到了附加属性的问题。我正在构建一个 C# WPF 应用程序。

我正在使用 ClassObject 类型的自定义 类 填充 ObserverableCollection。从这个集合中,我在 canvas 上创建了 ClassShapeViews。这些 ClassShapeView 需要引用 ClassObject 才能显示正确的信息。

为了传递 ClassObject 引用,我绑定到 XAML 中的 AttachedProperty,如下所示:

<ItemsControl ItemsSource="{Binding ClassObjectList}">
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Canvas  x:Name="canvas" Background="White" AllowDrop="True" 
                    DragEnter="canvas_DragEnter" 
                    DragOver="canvas_DragOver" 
                    Drop="canvas_Drop"/>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel>
            <!-- This part doesn't work-->
                <shape:ClassShapeView>
                    <Style TargetType="shape:ClassShapeView">
                        <Setter Property="shape:ClassShapeView.ClassObject" Value="{Binding}"/>
                    </Style>
                </shape:ClassShapeView>
            <Label Content="{Binding Name}"/>
        </StackPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>

在 ClassShapeView 的代码后面,我正在处理 DependencyProperty:

public partial class ClassShapeView : UserControl
{
    protected ClassShapeViewModel viewModel { get { return DataContext as ClassShapeViewModel; } }
    public ClassShapeView()
    {
        InitializeComponent();
        this.DataContext = new ClassShapeViewModel();
    }

    public static readonly DependencyProperty ClassObjectProperty =
    DependencyProperty.RegisterAttached("ClassObject", typeof(ClassObject), typeof(ClassShapeView), new PropertyMetadata(default(ClassObject)));

    public static void SetClassObject(UIElement element, ClassObject value)
    {
        element.SetValue(ClassObjectProperty, value);
    }

    public static ClassObject GetClassObject(UIElement element)
    {
        return (ClassObject)element.GetValue(ClassObjectProperty);
    }

    public ClassObject MyClassObject
    {
        get { return viewModel.ClassObject; }
        set { viewModel.ClassObject = value; }
    }

}

收到 ClassObject 引用后,我想使用 属性 MyClassObject 将此引用发送到附加的 ViewModel。 ClassShapeView中的数据绑定到ViewModel。

我只是不知道如何将 ClassObject 引用从 CodeBehind 转换为 ViewModel。

感谢 Clemens 我得到了答案

"when the ItemsControl's ItemsSource property is bound to a collection of ClassObject instances (as what I suppose is your ClassObjectList), then the DataContext of each item container (e.g. a ContentPresenter) is set to the respective element from the collection. This DataContext is inherited into the ItemTemplate, so that the controls inside the DataTemplate (e.g. your UserControl) already get the correct ClassObject element as their DataContext."

所以我的 XAML 现在看起来像这样

        <ItemsControl ItemsSource="{Binding ClassObjectList}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas  x:Name="canvas" Background="White" AllowDrop="True" 
                         DragEnter="canvas_DragEnter" 
                         DragOver="canvas_DragOver" 
                         Drop="canvas_Drop"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <shape:ClassShapeView/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

我的视图如下所示:

    public partial class ClassShapeView : UserControl
{
    public ClassShapeView()
    {
        InitializeComponent();     
    }
}