【问题标题】:Windows Store Apps: Bind App bar IsOpen property to ListView Selected ItemWindows 应用商店应用:将应用栏 IsOpen 属性绑定到 ListView 选定项
【发布时间】:2013-06-25 11:19:05
【问题描述】:

我有一个带有 ListView 和 AppBar 的页面。我想确保 AppBar 无法打开/可见,除非 ListViews 的选定项不为空。

所以我这样实现了 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 是否为空来检查是否返回布尔值。

即使 GridView Selected Item 为空,AppBar 也会出现。

这里有什么问题?

【问题讨论】:

  • 这里有precedence rules that govern dependency property values 可以发挥作用。也就是说,您确定要创建这样的行为吗?这是一个相当主观的问题,但我会说应用栏有时出现而不是其他时候出现对于用户来说可能并不直观。也许将应用栏上各种命令的启用绑定到选定状态,甚至在没有选择时在栏上包含一些文本以提示用户如何启用各种功能?
  • 谢谢尼尔。我有一个项目列表,以及在选择项目时变得可见的按钮。我认为将这些按钮放在应用栏中,然后在选择项目时打开它会很聪明。事实证明,这样做非常困难,我一直在寻找 XAML 中的解决方案。现在,如果我对您的理解正确,您是说这根本不是一个好主意。为一组命令保留应用栏,并且在选择项目时不要更改它。我同意——用户会觉得这很混乱。另外,感谢有关依赖属性优先规则的链接。

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


【解决方案1】:

我刚刚对其进行了测试,似乎绑定不适用于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 通知。

虽然我认为这只是另一个方向。 (编辑:这是一个类似的帖子:opening the appbar in metro style apps using binding property

这是我使用的代码:

<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();
    }
}

【讨论】:

    【解决方案2】:

    将 Visibility 属性与 SelectedItem 绑定,并让转换器在 null 值的情况下折叠项目。我也怀疑 selectedItem 是否被调用为 null 转换器。只需重新检查即可解决问题。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2012-12-22
        • 2012-11-01
        • 2015-09-05
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多