【问题标题】:Setting visiblity to section of data template in a listview在列表视图中设置数据模板部分的可见性
【发布时间】:2015-04-20 02:48:49
【问题描述】:

我有一个 WPF/MVVM 应用程序。 When a item is selected in my listview I have a section of the datatemplate I want to be visible, then when a different item is selected it will be collapsed, and the new item will be visible.我将在下面模拟一些代码以帮助展示我正在尝试做的事情。

我的问题是如何仅对留在 mvvm 范围内的选定项目执行此操作。我知道我可以绑定到我的视图模型中的一个属性,但是如果每个项目都具有相同的绑定,它们都打开,我需要它只用于选定的项目,我不确定在 MVVM 中处理这个问题的最佳方法,寻找建议。我的列表视图中的项目源是一个类的 ObservableCollection,它存储了我在选择项目时需要的一些默认数据。

            <ListView Name="lvMain"
    ItemsSource="{Binding MyItemSource, Mode=TwoWay}"
    SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding MyPath, Mode=TwoWay}" />
                    <!-- removing the rest to keep this short -->
                </Grid>
                <!-- This section below is collapsed by default, when selected I'd like to set the visibility, my question is how to only set the visibility in the grid below when the item is selected -->
                <Grid Grid.Row="1" Visibility="{Binding ??}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <!-- removing the rest to keep this short -->
                </Grid>
            </Grid>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    首先,您需要一些布尔到可见性转换器,但如果您没有,您可以使用内置转换器,然后您需要使用 RelativeSource 绑定到可视化树上,找到 ListViewItem 并绑定通过该转换器到其IsSelected 属性

    <ListView Name="lvMain" ...>
        <ListView.Resources>
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        </ListView.Resources>
        <ListView.ItemTemplate>
            <DataTemplate>
                <!-- removed content -->
                <Grid 
                    ...
                    Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}, 
                        Path=IsSelected, 
                        Converter={StaticResource BooleanToVisibilityConverter}}">
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    【讨论】:

    • 谢谢,正是我想要的。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 2017-11-09
    • 2013-04-17
    • 2011-06-04
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多