将adorner属性绑定到viewmodel属性 [英] Bind adorner property to viewmodel property

查看:114
本文介绍了将adorner属性绑定到viewmodel属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据viewmodel的属性显示(或不)一个adorner。

I need to display (or not) an adorner depending on a viewmodel's property.

我的看法是这样的:

<ItemsControl x:Name="Items">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Left" Value="{Binding Path=X}" />
            <Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
            <Setter Property="Width" Value="{Binding Path=Width}" />
            <Setter Property="Height" Value="{Binding Path=Height}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <AdornerDecorator>
                <Border x:Name="DraggableBorder" Background="{Binding Path=BackgroundColor}">
                    <!-- contents -->

                    <i:Interaction.Behaviors>
                        <behaviors:DragOnCanvasBehavior DraggableItem="{Binding}">
                            <behaviors:DragOnCanvasBehavior.MouseOverAdornerTemplate>
                                <DataTemplate>
                                    <Border DataContext="DraggableBorder"
                                        BorderBrush="#B0000000"
                                        Width="{Binding Path=Width}"
                                        Height="{Binding Path=Height}" />
                                </DataTemplate>
                            </behaviors:DragOnCanvasBehavior.MouseOverAdornerTemplate>
                            <behaviors:DragOnCanvasBehavior.SelectedAdornerTemplate>
                                <DataTemplate>
                                    <Border DataContext="DraggableBorder"
                                        BorderBrush="#FF34619E"
                                        Width="{Binding Path=Width}"
                                        Height="{Binding Path=Height}"
                                        Visibility="{Binding Path=Selected,
                                            ElementName=DraggableBorder,
                                            Converter={StaticResource BooleanToVisibilityConverter}}" />
                                </DataTemplate>
                            </behaviors:DragOnCanvasBehavior.SelectedAdornerTemplate>
                        </behaviors:DragOnCanvasBehavior>
                    </i:Interaction.Behaviors>
                </Border>
            </AdornerDecorator>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

此行为将鼠标事件附加到正确的方法,并将鼠标悬停在adorner上。

And the behavior attaches the mouse events to the proper methods, and displays/hides the mouse over adorner.

鼠标悬停/脱落事件和装饰效果正常,但选择装饰物会使我有点麻烦。因为只能选择一个项目,我想依赖于viewmodel的 Selected 属性。我假设上面的示例将工作(我尝试了几个版本),但唉,这不是。

The mouse over/out events and adorners are working fine, but the selection adorners cause me a bit of trouble. Because only one item should be selected, I want to rely on the Selected property of the viewmodel. I assumed what is in the sample above would work (I tried a few versions), but alas, it is not to be.

我应该如何写我的可见性属性?

How should I write my Visibility property ?

全部代码可在此处使用: https://github.com/cosmo0/DragSnap/tree/adorners

Full code available here : https://github.com/cosmo0/DragSnap/tree/adorners

推荐答案

可见性绑定与属性选择的DraggableBorder绑定,该属性不存在。

The Visibility binding binds with a property 'Selected' of DraggableBorder, which does not exist.

此外,边框具有DataContext =DraggableBorder这种情况,是一个简单的字符串。

Furthermore, the border has a DataContext="DraggableBorder" which in this case, is a simple string.

所以,使用边框上的以下属性:

So, use the following properties on your border:

    <Border DataContext="{Binding DataContext, ElementName=DraggableBorder}" Visibility="{Binding Selected, Converter=...}"/>

这篇关于将adorner属性绑定到viewmodel属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆