这是我绑定到集合的控件。
<ListBox Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Path=Pallets}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel>
<Button Content="C" DockPanel.Dock="Left"/>
<Button Content="D" DockPanel.Dock="Right"/>
<TextBlock Text="{Binding Path=Name}"/>
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
这就是它的样子
我想要达到的目的是默认隐藏两个按钮(左和右),但应该在所选项目上显示。
我认为,可以使用触发器来完成吗?
答案 0 :(得分:0)
我自己得到了
<Button Content="C" DockPanel.Dock="Left">
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"
Value="False">
<Setter Property="Button.Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
答案 1 :(得分:0)
你可以使用绑定和转换器来实现它。在你的userControl中declay这个资源
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
</UserControl.Resources>
<ListBox Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Path=Pallets}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel>
<Button Content="C" DockPanel.Dock="Left" Visibility={Bindin Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Converter{StaticResource BoolToVis }"}/>
<Button Content="D" DockPanel.Dock="Right" Visibility={Bindin Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Converter{StaticResource BoolToVis }"}/>
<TextBlock Text="{Binding Path=Name}"/>
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
请注意已经完成了。我现在没有访问vs检查语法