2013-06-25 22 views
0

我有一个ListView和AppBar的页面。我想确保AppBar不能打开/可见,除非ListViews的选定项目不为空。Windows Store应用程序:绑定应用程序栏IsOpen属性到ListView所选项目

所以我实现了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为null,AppBar也会出现。

这里有什么可能是错的?

+1

有一些控制依赖项属性值的优先规则(http://msdn.microsoft.com/zh-cn/library/ms743230.aspx)可能在此处播放。那就是说,你确定你想创造这样的行为吗?这是一个相当主观的,但我认为它可能不是一个用户直观的应用栏有时出现,而不是其他人。也许可以将应用栏上各种命令的启用绑定到选定状态,甚至可以在没有选择的情况下在栏上包含一些文本,以便为用户提供关于如何启用各种功能的提示? –

+0

谢谢尼尔。我有一个项目列表,以及当选择一个项目时变得可见的按钮。我认为将这些按钮放在应用栏中会很明智,然后在选择某个项目时将其打开。事实证明,这很难做到,我一直在寻找XAML中的解决方案。现在,如果我正确地理解了你,你就是说这根本不是一个好主意。保留一组命令的应用栏,并且在选择某个项目时不要更改它。我同意 - 用户会发现这令人困惑。另外,感谢关​​于依赖属性优先规则的链接。 –

回答

0

我刚测试过它,看起来,绑定不起作用AppBar.IsOpen

我势必一个Button.IsEnabledGridView.SelectedItem以及,按钮被正确设置到false,但AppBar.IsOpen没有,转换只按钮绑定调用一次。

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

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

虽然我认为这只意味着另一个方向。 (编辑:这里是一个simialar后: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(); 
    } 
} 
0

绑定Visibility属性具有的SelectedItem和具有转换器崩溃该项目在空值的情况下。此外,我怀疑在selectedItem为null转换器被调用的情况下。只需重新检查一下,就可以解决问题。

相关问题