将adorner属性绑定到viewmodel属性

编程入门 行业动态 更新时间:2024-10-27 08:39:17
本文介绍了将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 ?

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

Full code available here : github/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属性

本文发布于:2023-11-14 16:31:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   绑定   adorner   viewmodel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!