我正在構建一個 WPF MVVM 應用程式。
我擁有的:
我有一個ShellWindow看起來像這樣:

它由 2 行組成:
1:漢堡選單(不重要)有 Height="*"
2:控制臺 Height="100"
控制臺是一個UserControl:
<UserControl
//namespaces>
<Grid Name="LoggingGrid" Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Margin="{StaticResource SmallLeftMargin}">
<Button
x:Name="CollapseBtn"
Width="25"
Height="25"
Click="CollapseBtn_Click"
Content="▲">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Fill="White" />
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
<StackPanel Margin="5,0,0,0" Orientation="Horizontal">
<Image
Height="25"
Source="/Images/console-icon.png"
Visibility="Visible" />
<Label
Content="Console"
FontSize="16"
Foreground="White" />
</StackPanel>
</TextBlock>
<Border Grid.Row="1">
<ListView
x:Name="LoggingList"
Margin="5"
Background="Black"
BorderThickness="0"
Foreground="White"
ItemsSource="{Binding Logs, UpdateSourceTrigger=PropertyChanged}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto" />
</Border>
</Grid>
</UserControl>
我省略了不重要的事情。
我想做的事:
每當用戶單擊按鈕時,控制臺應折疊并如下所示:

箭頭也改變了。
我該如何實施?使用 MVVM 的最佳方法是什么?
我嘗試過的:
我嘗試在后面的代碼中使用按鈕單擊事件處理程式 - CollapseBtn_Click,只是為了看看會發生什么:
private void CollapseBtn_Click(object sender, System.Windows.RoutedEventArgs e)
{
LoggingGrid.Visibility = System.Windows.Visibility.Hidden;
}
顯然,它洗掉了用戶控制元件并在原來的位置留下了白色背景。
uj5u.com熱心網友回復:
您應該將LoggingList的 設定為Visibility,而不是將整個 LoggingGrid 的Hidden設定Visibility為Collapsed。(有關 Hidden 和 Collapsed 之間的區別,請參見此處:Visibility.Collapsed 和 Visibility.Hidden 之間的區別)。
根據您在 ShellWindow 中的布局,您可能必須在 UserControl 中調整行高配置,以便折疊的 LoggingGrid 導致行高度為零。
關于 MVVM,最好的方法是將 Button 系結到ConsoleVisibleViewModel 上的 bool 屬性,以便單擊按鈕在 true 和 false 之間切換屬性。按鈕的樣式可以系結到相同的屬性。對于 LoggingList Visibility,您可以BooleanToVisibilityConverter在同一屬性上使用 Binding 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315345.html
上一篇:無法更改按鈕高度WPF
