Windows应用商店应用程序:将应用程序栏IsOpen属性绑定到ListView所选项目

本文关键字:应用程序 ListView 绑定 项目 选项 属性 应用 Windows IsOpen | 更新日期: 2023-09-27 18:19:45

我有一个包含ListView和AppBar的页面。我想确保AppBar不能打开/可见,除非ListViews的选定项不为null。

所以我实现了这样的AppBar:

<Page.BottomAppBar>
        <AppBar x:Name="bottomAppBar" Padding="10,0,10,0">
            <AppBar.IsOpen>
                <Binding ElementName="MyGrid" Path="SelectedItem" Converter="{StaticResource ValueToBooleanConverter}"/>
            </AppBar.IsOpen>
        </AppBar>
    </Page.BottomAppBar>

ValueToBooleanConverter是一个IValueConverter,它根据GridView的SelectedItem是否为null来检查是否返回布尔值。

即使GridView Selected Item为null,也会显示AppBar。

这里可能出了什么问题?

Windows应用商店应用程序:将应用程序栏IsOpen属性绑定到ListView所选项目

我刚刚测试了它,绑定似乎不适用于AppBar.IsOpen

我也将Button.IsEnabled绑定到GridView.SelectedItem,并且按钮被正确地设置为false,但AppBar.IsOpen不是,转换器只被调用一次用于按钮绑定。

这可能与此有关:http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.appbar.isopen

注意绑定到IsOpen属性没有预期结果,因为设置该属性时不会发生PropertyChanged通知。

尽管我认为这只是另一个方向。(编辑:这是一篇类似的帖子:使用绑定属性在metro风格的应用程序中打开应用程序栏)

这是我使用的代码:

<common:LayoutAwarePage.BottomAppBar>
    <AppBar IsOpen="{Binding SelectedItem, Converter={StaticResource ObjectToBooleanConverter}, ElementName=gridView}"
            IsSticky="True"
            Background="#E5F50000" />
</common:LayoutAwarePage.BottomAppBar>

<Grid Style="{StaticResource LayoutRootStyle}">
    <Grid.RowDefinitions>
        <RowDefinition Height="140" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <GridView x:Name="gridView"
              HorizontalAlignment="Right"
              Margin="0"
              Grid.Row="1"
              Width="469">
        <Button Content="asdf" />
        <Button Content="asdf" />
        <Button Content="asdf" />
    </GridView>
    <Button x:Name="bsetnull"
            Content="setnull"
            HorizontalAlignment="Left"
            Margin="78,10,0,0"
            Grid.Row="1"
            VerticalAlignment="Top" Tapped="bsetnull_Tapped"/>
    <Button x:Name="bsettoone"
            Content="settoone"
            HorizontalAlignment="Left"
            Margin="78,71,0,0"
            Grid.Row="1"
            VerticalAlignment="Top" Tapped="bsettoone_Tapped"/>
    <Button Content="Button"
            HorizontalAlignment="Left"
            Margin="78,147,0,0"
            Grid.Row="1"
            VerticalAlignment="Top"
            IsEnabled="{Binding SelectedItem, Converter={StaticResource ObjectToBooleanConverter}, ElementName=gridView}" />
</Grid>

和转换器

public class ObjectToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value == null)
            return false;
        else
            return true;
    }
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

将Visibility属性与SelectedItem绑定,并具有转换器,在空值的情况下折叠该项。。此外,我怀疑selectedItem是否为null转换器。只要重新检查一下就会解决问题。

您已经发布了microsoft链接(http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.appbar.isopen),所以也许你自己提供了一个属性来绑定并在appbar的Opened和Closed事件中设置它。