简体   繁体   English

如何使用触发器来触发对子控件的可见性更改

[英]how to trigger visibility change to child controls with a trigger

I have a databound listbox thet generates items in a datatemplate of type wrappanel with other controls within it. 我有一个数据绑定列表框,它们会在类型为wrappanel的数据模板中生成项目,并在其中包含其他控件。 I would like to have the behavior to, when I change the visibility to affect differently the controls within the wrappanel 当我更改可见性以不同地影响包装面板中的控件时,我希望这样做

    <WrapPanel Orientation="Horizontal" Tag="{Binding .}" HorizontalAlignment="Stretch" Visibility="{Binding editMode, Converter={StaticResource VisibilityConverter}}">
                    <Label Width="150" Content="{Binding Path=avaiableAttribute.Text}" Name="lblName"/>
                    <Label Width="150" 
                           Content="{Binding Path=informationItem.ItemString, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}" 
                           Initialized="Label_Initialized" 
                           Name="lblText" />
                    <ContentPresenter MinWidth="200" MaxHeight="200" Content="{Binding ., Converter={StaticResource InformationItemConverter}, Mode=TwoWay}" HorizontalAlignment="Stretch"
                                      Name="cpValue"
                                      Initialized="ContentPresenter_Initialized"/>

                    <WrapPanel.Style>
                        <Style  TargetType="{x:Type WrapPanel}">
                            <Style.Triggers>
                                <Trigger Property="Visibility" Value="Visible" >
                                    <Trigger.Setters>
                                        <Setter TargetName="lblText" Property="Visibility" Value="Collapsed" />
                                        <Setter TargetName="cpValue" Property="Visibility" Value="Visible" />
                                    </Trigger.Setters>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </WrapPanel.Style>
    </WrapPanel>

I just get the following build error 我刚收到以下构建错误

the property 'targetname' does not represent a valid target for the 'setter' because an element 属性“ targetname”不代表“设置者”的有效目标,因为

When using a Setter , the TargetName only applies to elements in a Template . 使用SetterTargetName仅适用于Template元素。 This means your Trigger has to exist in a DataTemplate or ControlTemplate . 这意味着您的Trigger必须存在于DataTemplateControlTemplate The easiest way to do what you want to do is to create your own IValueConverter for the inverse of BooleanToVisibilityConverter (ie return Visibility.Collapsed when value is true). 做您想做的最简单的方法是为BooleanToVisibilityConverter的逆数创建自己的IValueConverter (即,当value为true时返回Visibility.Collapsed )。

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

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