简体   繁体   English

如何在基于SelectedItem的ListBox的ItemTemplate中切换图像的可见性

[英]How can I toggle an Image's visibility in a ListBox' ItemTemplate based on SelectedItem

I have an Image inside an ItemTemplate for a ListBox. 我在ListBox的ItemTemplate内有一个图像。 Is there a way in XAML to toggle its visibility based on the ListBox' SelectedItem? XAML中是否有一种方法可以基于ListBox的SelectedItem来切换其可见性?

In other words, I want the image to be visible only if the ListBoxItem is selected, but want to avoid code-behind for that. 换句话说,我希望图像仅在选择ListBoxItem时才可见,但要避免在代码后面进行隐藏。

Thanks. 谢谢。

In the ItemContainerStyle for the listbox (in Blend, right-click on the ListBox in the Objects and Timeline and select Edit Additional Templates / Edit Generated Item Container (ItemContainerStyle) / Edit a Copy... Choose a name and location for the new style in the enusing dialog box.) 在列表框的ItemContainerStyle中(在Blend中,右键单击“对象和时间线”中的列表框,然后选择“编辑其他模板” /“编辑生成的项目容器(ItemContainerStyle)” /“编辑副本...”,为新样式选择名称和位置在启用对话框中。)

In the generated style, locate the ContentPresenter tag. 在生成的样式中,找到ContentPresenter标签。 Somewhere in that area (depending on your specific layout needs) you will want to drop your Image item. 在该区域中的某个位置(取决于您的特定布局需求),您将需要放置Image项。 Set its visibility to bind to the IsSelected property of the templated parent... 设置其可见性以绑定到模板化父对象的IsSelected属性。

<Image Source="{Binding Property2}" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected, Mode=TwoWay, Converter={StaticResource BooleanToVisibilityConverter}}"/>

As you see in the markup above, you do have to use a value converter, so cannot get away from using SOME code...IsSelected is a Boolean value, Visibility is a member of the System.Windows.Visibility enumeration. 正如在上面的标记中看到的,您必须使用值转换器,因此不能逃避使用某些代码... IsSelected是布尔值,Visibility是System.Windows.Visibility枚举的成员。 To convert form one to the other, you'll need a ValueConverter that switches from one value domain to the other. 要将一种形式转换为另一种形式,您需要一个ValueConverter,它可以从一个值域切换到另一个值域。 The Boolean/Visibility converter is fairly common, and I've included a simple one below (I tend to have a "stock" one that I use that includes a parameter to set which value to map "true" to in order to make it more flexible, but omitted it for conciseness...) 布尔值/可见性转换器相当常见,下面我提供了一个简单的转换器(我倾向于使用“股票”股票,其中包括一个参数来设置要映射“ true”的值以使其成为真实值)更灵活,但为简洁起见省略了它...)

public class BooleanToVisibilityConverter : IValueConverter
{
    public Object Convert(Object value, Type targetType, Object parameter, System.Globalization.CultureInfo culture)
    {
        var booleanValue = (Boolean)value;
        return booleanValue ? Visibility.Visible : Visibility.Collapsed;
    }

    public Object ConvertBack(Object value, Type targetType, Object parameter, System.Globalization.CultureInfo culture)
    {
        var visibilityValue = (Visibility) value;
        return visibilityValue == Visibility.Visible;
    }
}

Edit the ListBox's ItemContainerStyle, and you will then have access to all the States for the ListBox items. 编辑ListBox的ItemContainerStyle,然后您将可以访问ListBox项的所有状态。 These include the focussed and selected states. 这些包括重点和选定状态。 If you're doing this in Blend, it should be under "Edit Additional Templates" on the ListBox's context menu. 如果您在Blend中执行此操作,则它应位于ListBox的上下文菜单中的“编辑其他模板”下。

Then you should be able to change the layout for the Selected state, which should be what you need? 然后,您应该能够更改Selected状态的布局,这应该是您所需要的吗?

You could do this with the SelectionChanged event of the ListBox. 您可以使用ListBox的SelectionChanged事件来执行此操作。

<Grid x:Name="LayoutRoot" Background="White">
    <ScrollViewer Height="500" Width="200">
        <ListBox x:Name="lstItems" SelectionChanged="lstItems_SelectionChanged">
            <ListBoxItem>
                <StackPanel Orientation="Horizontal">
                    <Image Source="Selected.png" Visibility="Collapsed"/>
                    <TextBlock Text="Item 1"/>
                </StackPanel>
            </ListBoxItem>
            <ListBoxItem>
                <StackPanel Orientation="Horizontal">
                    <Image Source="Selected.png" Visibility="Collapsed"/>
                    <TextBlock Text="Item 2"/>
                </StackPanel>
            </ListBoxItem>
        </ListBox>
    </ScrollViewer>
</Grid>

Handle the event: 处理事件:

private void lstItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    HideAllSelected();
    ListBoxItem item = lstItems.SelectedItem as ListBoxItem;
    StackPanel sp = item.Content as StackPanel;
    Image img = sp.Children[0] as Image;
    img.Visibility = Visibility.Visible;
}

private void HideAllSelected()
{
    foreach (ListBoxItem item in lstItems.Items)
    {
        StackPanel sp = item.Content as StackPanel;
        Image img = sp.Children[0] as Image;
        img.Visibility = Visibility.Collapsed;
    }
}

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

相关问题 如何将ListBox的可见性绑定到ApplicationBar按钮? - How can I bind a ListBox's Visibility to an ApplicationBar button? 如何在wp7中更改列表框项目的可见性属性? - How can I change listbox item's visibility property in wp7? 如何将ListBox SelectedItem重置为其原始状态。 WP7 - How to reset ListBox SelectedItem to it's orginal state. WP7 使用DataTemplate作为ItemTemplate时,Silverlight 4和LisBox的SelectedItem - Silverlight 4 and LisBox's SelectedItem when using DataTemplate as ItemTemplate 如何获取对ItemTemplate的根容器的引用,然后修改其属性? - How can i get reference to ItemTemplate's root container and then modify it's properties? 如何在Silverlight中将行样式添加到ListBox ItemTemplate - How to add row style to ListBox ItemTemplate in silverlight Silverlight:如何在ListBox ItemTemplate中动态绑定ComboBox? - Silverlight: How to dynamically bind a ComboBox in a ListBox ItemTemplate? 如何正确使用列表框itemtemplate / datatemplate中的按钮? - How to correctly use a button in a listbox itemtemplate / datatemplate? 如何将列表框的 selectedItem 传递给视图 Model - How to pass the selectedItem of a listbox to the View Model 如何将SelectedItem(从ListBox)绑定到变量? - How to bind SelectedItem (from a ListBox) to a Variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM