4

我在绑定 appbar 按钮的可见性属性时遇到了一些问题。我想将 appbar 按钮的可见性绑定到另一个元素的可见性。如果另一个元素是可见的 - 那么 appbar 是可见的。

所以这是我的代码:

<common:LayoutAwarePage.BottomAppBar>
    <AppBar>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Visibility="{Binding ElementName=btnSave, Path=Visibility}"
                    Click="Edit_Click" />
             ...(more buttons)
        </StackPanel>
    </AppBar>
</common:LayoutAwarePage.BottomAppBar>

<Button Grid.Row="7" Grid.Column="0"
     x:Name="btnSave"
     Content="Save"
     Style="{StaticResource EditModeButtonStyle}"
     Click="Save_Click" />

我正在更改后面代码中的 btnSave 可见性,并且在 appbar 按钮的可见性中没有反应。我什至尝试过只用一个文本块来做同样的绑定,而且效果很好。我也尝试在 appbar 上使用转换器(甚至认为我不需要),我看到调试器没有读取转换器的方法。我看到更多人写了类似的 appbar 问题,但没有任何答案对我没有帮助。有人知道我该怎么做吗?(我不想使用后面的代码来更改 appbar 的可见性)。

4

1 回答 1

4

I suspect that appbar elements are not seeing the page's elements and hence element binding is not working. I would recommend you to use independent property which implements INotifyPropertyChanged interface. Bind that property to those elements for which you want to set the visibility.

C#

public sealed partial class BlankPage4 : Page, INotifyPropertyChanged
{
    private Visibility _IsHide;
    public Visibility IsHide
    {
        get { return _IsHide; }
        set
        {
            _IsHide = value;
            OnPropertyChanged("IsHide");
        }
    }

    public BlankPage4()
    {
        this.InitializeComponent();
        DataContext = this;
    }

    private void btnHideAll_Click(object sender, RoutedEventArgs e)
    {
        IsHide = Visibility.Collapsed;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

XAML

<Page.BottomAppBar>
    <AppBar IsSticky="True" IsOpen="True">
        <StackPanel Orientation="Horizontal">
            <Button x:Name="btnHello" Visibility="{Binding IsHide}" Content="Hello" />
            <TextBlock Visibility="{Binding IsHide}" Text="Hello" FontSize="20"/>
        </StackPanel>
    </AppBar>
</Page.BottomAppBar>

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <Button x:Name="btnSave" Visibility="{Binding IsHide}" Content="Save" />
        <Button Content="Hide All" Click="btnHideAll_Click" />
    </StackPanel>
</Grid>
于 2013-09-25T16:56:55.213 回答