简体   繁体   English

通过绑定设置元素的可见性

[英]Setting Visibility of an Element via Bindings

I got an UIElement with several Canvas which I want to show or hide depending on certain circumstances. 我得到了一个带有几个Canvas的UIElement,我想根据某些情况显示或隐藏它。 I want this to be seen in the designer as well as when the program is running. 我想在设计器以及程序运行时看到这一点。 I tried several Bindings and also the BooleanToVisibilityConverter. 我尝试了几个Bindings和BooleanToVisibilityConverter。 But I'm stuck and can't find my error. 但我被卡住了,无法找到我的错误。 So here's the code: 所以这是代码:

UIElement (with only two Canvas; I got the responding properties in the partial Class) UIElement (只有两个Canvas;我在部分Class中获得了响应属性)

<component:AbstractComponent x:Class="View.LineComponent"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:component="clr-namespace:View"
         x:Name="userControl" Width="70" Height="10"
>
    <component:AbstractComponent.Resources>
        <BooleanToVisibilityConverter x:Key="VisibilityConverter" />
    </component:AbstractComponent.Resources>
    <Canvas Width="{Binding ElementName=userControl, Path=ActualWidth}">
        <Canvas Name="Line"
            Height="{Binding ElementName=userControl, Path=ActualHeight}"
            Width="{Binding ElementName=userControl, Path=ActualWidth}"
            Visibility="{Binding LineVisible, Converter={StaticResource VisibilityConverter}, FallbackValue=Hidden}"
            >
            <!-- Lot of stuff; Not interesting for the question-->
        </Canvas>
        <Canvas Name="Arrow"
            Height="{Binding ElementName=userControl, Path=ActualHeight, TargetNullValue=6.397, FallbackValue=6.397}"
            Width="{Binding ElementName=userControl, Path=ActualWidth, TargetNullValue=16.688, FallbackValue=16.688}"
            Visibility ="{Binding ArrowVisible, Converter={StaticResource VisibilityConverter}, FallbackValue=Hidden}"
            >
            <!-- Lot of stuff; Not interesting for the question-->
        </Canvas>
    </Canvas>
</component:AbstractComponent>

Usage in ParentWindow 在ParentWindow中的用法

<component:LineComponent Height="50" Canvas.Top="100" Width="50" Canvas.Left="50" LineVisible="True"/>

<component:LineComponent Height="50" Canvas.Top="200" Width="50" Canvas.Left="50" ArrowVisible="True"/>

I expect that in the first case the Line -Canvas is shown while in the other the Arrow -Canvas is shown. 我希望在第一种情况下显示Line -Canvas,而在另一种情况下显示Arrow -Canvas。 They both stay Hidden though. 他们都保持Hidden I also tried a different approach, declaring the LineVisible and ArrowVisible properties directly in the C#-Code but that didn't work either. 我也尝试了一种不同的方法,直接在C#-Code中声明LineVisibleArrowVisible属性,但这两种方法都不起作用。 Any ideas? 有任何想法吗?

Your LineVisible and ArrowVisible bindings do not have a source set, so will use the DataContext as source. 您的LineVisibleArrowVisible绑定没有源集,因此将使用DataContext作为源。 You will make life easier for yourself if you set the DataContext of your root Canvas element to the user control allowing you to omit all the repeated ElementName bindings, as described in this blog post I wrote . 如果您将根Canvas元素的DataContext设置为用户控件,允许您省略所有重复的ElementName绑定,您将使自己的生活更轻松,如我写的这篇博文中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM