如何在WindowState最大化时使控件可见,在正常时折叠?

时间:2011-08-19 21:22:28

标签: wpf controls visibility

这是我的观点,我想在窗口状态正常时折叠Rectange中的第二个Button,反之亦然。我错误地将命令名改为ShowCommand,我正在尝试但不能这样做。

xaml:

<StackPanel>
    <WrapPanel HorizontalAlignment="Center">
        <Button Width="30"  Height="20" Margin="0,30" Command="{Binding MinimizeCommand}" >
            <Rectangle Width="8" Stroke="Black" StrokeThickness="2"/>
        </Button>
        <Button Width="30" Height="20" Command="{Binding ShowCommand}" >
            <Grid >
                <Rectangle Width="8" Height="8" Stroke="Black" StrokeThickness="2"
                           HorizontalAlignment="Right"
                           Margin="0,3,5,3" />                        
                <Rectangle Visibility="{Binding MaximizeButtonVisibility}" Stroke="Black"
                           x:Name="RectMaxButton" Margin="10,0,0,8" Width="8" Height="8"
                           StrokeThickness="2" />
            </Grid>
        </Button>
        <Button Width="30" Height="20" Margin="0,30" Command="{Binding ExitCommand}"
                FontWeight="Bold">X</Button>
    </WrapPanel>           
</StackPanel>

CS:

public ICommand ShowCommand
{
    get
    {
        if (_showCommand == null)
            _showCommand = new RelayCommand(para => CanExit(), param => ToggleMaximizeWindwo());
        return _showCommand;
    }
}

public Visibility MaximizeButtonVisibility
{           
    get
    {
        this._maximizeButtonVisibility = Application.Current.MainWindow.WindowState == WindowState.Maximized ? Visibility.Visible : Visibility.Collapsed;
        return this._maximizeButtonVisibility; 
    }
    set
    {
        this._maximizeButtonVisibility = value;
        base.OnPropertyChanged("MaximizeButtonVisility");
    }
}

private void ToggleMaximizeWindwo()
{
    if (Application.Current.MainWindow.WindowState == WindowState.Normal)
    {
        Application.Current.MainWindow.WindowState = WindowState.Maximized;
        MaximizeButtonVisibility = Visibility.Visible;
    }
    else
    {
        Application.Current.MainWindow.WindowState = WindowState.Normal;
        MaximizeButtonVisibility = Visibility.Collapsed;
    }
}

1 个答案:

答案 0 :(得分:0)

看起来你在OnPropertyChanged的MaximizeButtonVisibility设置器中有一个拼写错误。当它应该是“MaximizeButtonVisibility”时,你将它作为“MaximizeButtonVisility”。