XAML可见性绑定无法正常工作

时间:2017-09-06 11:16:08

标签: c# wpf xaml

我在我的xaml代码中绑定了两个按钮的Visiblity我正在尝试使用播放/暂停按钮,我想要做的是在点击后隐藏播放按钮并显示暂停按钮,不完全确定是否这个这是最好的方式,但嘿,如果它有效,我们会很好!

这里:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Grid Width="50" Height="50" VerticalAlignment="Center" HorizontalAlignment="Center">
            <Button x:Name="PlayButton" Style="{DynamicResource MetroCircleButtonStyle}" Visibility="{Binding PlayButtonVisibility}"
                    Width="50" Height="50" HorizontalAlignment="Center"
                    VerticalAlignment="Center" Click="OnPlayButtonClicked"
                    d:DataContext="{d:DesignInstance local:MainWindow}">
                <Rectangle Width="20"
                           Height="20">
                    <Rectangle.Fill>
                        <VisualBrush Stretch="Fill"
                                     Visual="{StaticResource appbar_control_play}" />
                    </Rectangle.Fill>
                </Rectangle>
            </Button>

            <Button x:Name="PauseButton" Style="{DynamicResource MetroCircleButtonStyle}" Visibility="{Binding PauseButtonVisibility}"
                    Width="50" Height="50" HorizontalAlignment="Center"
                    VerticalAlignment="Center" Click="OnPauseButtonClicked"
                    d:DataContext="{d:DesignInstance local:MainWindow}">
                <Rectangle Width="20"
                           Height="20">
                    <Rectangle.Fill>
                        <VisualBrush Stretch="Fill"
                                     Visual="{StaticResource appbar_control_pause}" />
                    </Rectangle.Fill>
                </Rectangle>
            </Button>
        </Grid>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

C#代码在这里:

  private Visibility _playButtonVisiblity;
    private Visibility _pauseButtonVisiblity = Visibility.Hidden;

    public Visibility PlayButtonVisibility
    {
        get
        {
            return _playButtonVisiblity;
        }
        set { _playButtonVisiblity = value;
            NotifyPropertyChanged("playButton");
        }
    }

    public Visibility PauseButtonVisibility
    {
        get
        {
            return _pauseButtonVisiblity;
        }
        set
        {
            _pauseButtonVisiblity = value;
            NotifyPropertyChanged("pauseButton");
        }
    }

但是出于某种原因我每次尝试使用代码更改可见性时,它都没有生效,也不知道为什么这会考虑到目前为止看起来正确的所有内容。

3 个答案:

答案 0 :(得分:3)

你打电话给你通知错误。

public Visibility PauseButtonVisibility
{
    get
    {
        return _pauseButtonVisiblity;
    }
    set
    {
        _pauseButtonVisiblity = value;
        NotifyPropertyChanged("PauseButtonVisibility"); // <---- This is what you want.
    }
}

答案 1 :(得分:0)

您需要在通知事件中使用正确的属性名称

NotifyPropertyChanged("PlayButtonVisiblity");

答案 2 :(得分:0)

您将Button的Visibility属性绑定到ViewModel中的属性,但在setter中,您通知View私有字段值已更改。

您必须通知有关房产更改的信息,因为这只是视图可以访问的内容:

NotifyPropertyChanged("PauseButton");

在ViewModel中绑定到Visibility枚举也不是最佳做法,因为此枚举严格与View相关。我建议您只需绑定到bool属性并使用converter类将值从Visibility枚举转换为布尔值。