Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在动态列表框菜单中选择一个项目时,如何控制所有列表框项目中文本的可见性?_Wpf_Wpf Controls_Binding - Fatal编程技术网

Wpf 在动态列表框菜单中选择一个项目时,如何控制所有列表框项目中文本的可见性?

Wpf 在动态列表框菜单中选择一个项目时,如何控制所有列表框项目中文本的可见性?,wpf,wpf-controls,binding,Wpf,Wpf Controls,Binding,我从XML生成列表框菜单。我使用datatemplate设置listboxitems在选择和其他状态下的行为样式。我需要在选择从XML中获取值“retract”的项时隐藏所有listboxitems中的所有文本块。现在,我只能在具有此值的listboxitem中隐藏TextBlock,但不能在其他listboxitems中隐藏TextBlock。我想知道是否有人能帮忙。先谢谢你 <DataTemplate x:Key="ListBoxItemDataTemplate">

我从XML生成列表框菜单。我使用datatemplate设置listboxitems在选择和其他状态下的行为样式。我需要在选择从XML中获取值“retract”的项时隐藏所有listboxitems中的所有文本块。现在,我只能在具有此值的listboxitem中隐藏TextBlock,但不能在其他listboxitems中隐藏TextBlock。我想知道是否有人能帮忙。先谢谢你

<DataTemplate x:Key="ListBoxItemDataTemplate">
        <Grid x:Name="DataItem">
            <Image x:Name="IconImage" Source="{Binding XPath=@icon}" Height="16" Margin="16,0,0,0" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Left" />
            <TextBlock x:Name="ListboxIemtextBlock" Text="{Binding XPath=@name}" />
            <Image x:Name="ArrowImage" Height="10" Source="Resources/Images/arrow_collapsed_grey.png" Visibility="{Binding XPath=@state}"/>
        </Grid>
         <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">
                <Setter TargetName="ListboxIemtextBlock" Property="Foreground" Value="White"/>
                <Setter TargetName="IconImage" Property="Source" Value="{Binding XPath=@iconSelected}"/>
                <Setter TargetName="IconImage" Property="Height" Value="16"/>
                <Setter TargetName="ArrowImage" Property="Source" Value="Resources/Images/arrow_collapsed_white.png"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">
                <Setter TargetName="ListboxIemtextBlock" Property="Foreground" Value="#FF6dacbe"/>
            </DataTrigger>
            <MultiDataTrigger>         
                <MultiDataTrigger.Conditions>           
                    <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True" />           
                    <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True" />         
                </MultiDataTrigger.Conditions>         
                <Setter TargetName="ListboxIemtextBlock" Property="Foreground" Value="White"/>      
            </MultiDataTrigger> 
            <DataTrigger Binding="{Binding XPath=@retract}" Value="True" >             
                <Setter TargetName="ListboxIemtextBlock" Property="Visibility" Value="Hidden"/>      
            </DataTrigger> 
         </DataTemplate.Triggers>
    </DataTemplate>

看起来我无法控制datatemplate中所有TexBlock的可见性。我认为应该采用列表框的方式。我想用第二个根本没有texblock的datatemplate切换datatemplates。我想将multitrigger用于值为isSelected且XML绑定到Binding=“{XPath=@retract}的条件。但是,我无法为Listbox样式的multitrigger分配XPath绑定。此外,您可能会帮助正确绑定它,或者更好地了解如何隐藏TexBlock

<Style x:Key="ListBoxItemContainerStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="ContentTemplate" Value="{StaticResource ListBoxItemDataTemplate}"/>
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <ContentPresenter x:Name="contentPresenter"/>
                    <ControlTemplate.Triggers>      
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Binding="{XPath=@retract}" Value="true"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="ContentTemplate" Value="{StaticResource SelectedListBoxItemDataTemplate}"/> 
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>  
    </Style> 

我使用XMLDataProvider填充了XML。我通过以下方式引用XML:

<XmlDataProvider x:Key="PagesData" XPath="/Pages" Source="Data/DataSource.xml" />

XML:


您可以绑定到父列表框的SelectedItem.retract。这是一个使用路径而不是XPath的工作示例(因为我没有您的XML源代码),但您应该能够以相同的方式让它工作

在DataTemplate中添加此触发器

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=SelectedItem.retract}" Value="True" >
    <Setter TargetName="ListboxIemtextBlock" Property="Visibility" Value="Hidden"/>
</DataTrigger>


感谢您查看我的线程。我添加了XML示例。当lisboxitem的“retract”属性的值等于“true”时,我需要能够在选择lisboxitem时隐藏所有三个listboxItems中的所有TexBlock。在其他情况下,不应更改任何内容。此datatrigger将只隐藏一个选中的listboxitem。我想知道您知道如何在其他listboxitems中隐藏texblock。谢谢。@vladc77,它将隐藏listboxitems的所有TextBlock,因为ListBoxItem中的每个ListBoxItem都在所选项目上触发。我刚刚测试了它,并且它只隐藏ListBoxItem中所选的texblock。我编辑了以前的评论。我在“ListBoxItemD”中添加了此数据触发器ataTemplate'datatemplate@vladc77:我在这里上载了我的示例项目:如果选择ListBoxItem“name3”,则所有文本块都将隐藏。我将查看您的XMLexample@vladc77:我忘了添加图像作为资源..我会解决的
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=SelectedItem.retract}" Value="True" >
    <Setter TargetName="ListboxIemtextBlock" Property="Visibility" Value="Hidden"/>
</DataTrigger>