我需要根据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属性
发布评论