C# 从DataTemplate中的userControl触发listboxItem可见性
我需要当DataTemplate中的UserControl保持不可见时,整个ListBoxItem保持不可见,但我所尝试的都失败了C# 从DataTemplate中的userControl触发listboxItem可见性,c#,wpf,binding,C#,Wpf,Binding,我需要当DataTemplate中的UserControl保持不可见时,整个ListBoxItem保持不可见,但我所尝试的都失败了 <ListBox HorizontalContentAlignment="Center"> <ListBox.ItemTemplate> <DataTemplate> <Visual:uc_acessorio_fechado Acessorio="{Binding}"/>
<ListBox HorizontalContentAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<Visual:uc_acessorio_fechado Acessorio="{Binding}"/> //when this element is collapsed, make the ListBoxItem collapsed
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
//折叠此元素时,使ListBoxItem折叠
根据控件设置自身可见性的方式,这可能会起作用:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Visual:uc_acessorio_fechado
Acessorio="{Binding}"
Visibility="{Binding Visibility,
RelativeSource={RelativeSource AncestorType=ListBoxItem},
Mode=OneWayToSource}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
它不适用于样式设置器,但适用于指定本地属性值
或者,您可以使用以下选项:
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Visual:uc_acessorio_fechado x:Name="uc" Acessorio="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Visibility" Value="{Binding Visibility, ElementName=uc}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>