How can I make a control visible when WindowState is Maximized and collapsed when Normal?
This is my view, I want to collapse the second Rectange
in the Button
when windowstate is normal and vice versa. Mistakenly I changed the command name as ShowCommand
, well I'm trying it but can't do it.
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" Comma开发者_运维问答nd="{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;
}
}
It looks like you have a typo in your MaximizeButtonVisibility setter in the OnPropertyChanged. You have it as "MaximizeButtonVisility" when it should be "MaximizeButtonVisibility".
It might be helpful for someone, but if you were to consistently use
OnPropertyChanged(nameof(MaximizeButtonVisibility));
Then the name cannot be wrong, and this will also continue to work if the property is renamed.
精彩评论