【问题标题】:ListView and AppBar cooperation. The simplest possible multiple select scenarioListView 和 AppBar 的合作。最简单的多选场景
【发布时间】:2012-05-20 20:42:56
【问题描述】:

与 AppBar 一起实现列表视图多选场景的最简单方法是什么? So that it behaves exactly as the Windows 8 start screen when multiple items selected (e.g. via the mouse right-click).

我想将应用栏与第一个选定的列表视图项目一起显示,我想保持它与第二个、第三个等一起打开,我想通过任何应用栏按钮操作(执行的上下文操作)来关闭它) 或通过其他系统范围的应用栏关闭操作(例如,右键单击其他位置,这意味着上下文操作已取消)。

我目前的实现太复杂了。我相信我一定错过了一些东西——这样一个基本和常见的场景必须可以以标准化的方式实现。

下面准备的脚手架代码。如果仅使用此代码,则应用栏在右键单击第二个列表视图项之前隐藏,并且需要再次右键单击列表视图(不可接受)。如果与 IsSticky 结合使用,则根本无法选择第二个列表视图项。

<Page
    x:Class="ListViewAndAppBar.ExamplePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ListViewAndAppBar"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    DataContext="{Binding ExamplePageViewModel, Source={StaticResource Locator}}">

    <Grid Background="Gray">
        <ListView
            x:Name="ListView"
            ItemsSource="{Binding Persons}"
            SelectionMode="Multiple"
            SelectionChanged="ListView_SelectionChanged">
        </ListView>
    </Grid>

    <Page.BottomAppBar>
        <AppBar x:Name="BottomAppBar" Padding="10,0,10,0">
            <Button x:Name="BottomAppBarBack" Tag="Back" Style="{StaticResource BackAppBarButtonStyle}" HorizontalAlignment="Left" />
        </AppBar>
    </Page.BottomAppBar>
</Page>

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    this.BottomAppBar.IsOpen = true;
    //this.BottomAppBar.IsSticky = true;
}

【问题讨论】:

    标签: c# xaml windows-8 windows-store-apps appbar


    【解决方案1】:

    回答我自己的问题。发布问题后,我很快找到了解决方案。我会把它留在这里,以防有人犯同样的初学者错误。

    解决方案再简单不过了:IsSticky 必须在 IsOpen 之前调用。在此切换后,一切都按预期工作。

    private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (this.ListBox.SelectedItems.Count > 0)
        {
            this.BottomAppBar.IsSticky = true;
            this.BottomAppBar.IsOpen = true;
        }
        else
        {
            this.BottomAppBar.IsOpen = false;
            this.BottomAppBar.IsSticky = false;
        }
    
        // Or the following if you wish...
        // this.BottomAppBar.IsOpen = this.BottomAppBar.IsSticky = this.ListView.SelectedItems.Count > 0;
    }
    

    【讨论】:

    • 谢谢扬!我只是在处理同样的问题,并更改 IsSticky=true 和 IsOpen=true 的分配顺序,也为我解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 2017-11-14
    • 2011-10-05
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多