Wpf 当WindowsState最大化并在正常情况下折叠时,如何使控件可见?

Wpf 当WindowsState最大化并在正常情况下折叠时,如何使控件可见?,wpf,controls,visibility,Wpf,Controls,Visibility,这是我的视图,我想在WindowsState正常时折叠按钮中的第二个矩形,反之亦然。我错误地将命令名更改为ShowCommand,我正在尝试,但无法执行 xaml: <StackPanel> <WrapPanel HorizontalAlignment="Center"> <Button Width="30" Height="20" Margin="0,30" Command="{Binding MinimizeCommand}" >

这是我的视图,我想在WindowsState正常时折叠按钮中的第二个矩形,反之亦然。我错误地将命令名更改为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>

看起来您在OnPropertyChanged中的MaximizeButtonVisibility设置器中有一个输入错误。当它应该是MaximizeButtonVisibility时,您将其作为MaximizeButtonVisibility。

它在第一次启动时工作正常,但我希望在Windows状态更改时更改此可见性,因为我已完成此MaximizeButtonVisibility属性,但它在第一次启动后不会生效,当WindowsState更改时,我正在进行最大化/最小化小的重新设置visiblit/hidden。我确实纠正了这一点,但它仍然不起作用,所以我在绑定后放置了UpdateSourceTrigger=PropertyChanged,它起作用了。感谢您的回复1制作一个和2绑定到窗口的窗口状态。过度思考这不是解决办法。
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;
    }
}