WPF 그리드 열 가시성을 전환하는 방법
-
10-07-2019 - |
문제
내가 작업중 인 WPF 앱에서 작동하는 데 어려움을 겪고 있습니다. 기본적으로, 내가 뒤 따르는 것은 MMC의 작업 창과 같은 것입니다.
- 앱에는 디스플레이의 기본 부분에 3 개의 열이 있습니다. 오른쪽에 열이 필요합니다. 나는 이것이 그리드 스플리터가있는 그리드를 사용하는 것을 의미한다고 가정하지만 작동하는 것은 무엇이든 할 것이라고 가정합니다.
- 앱을 닫을 때 오른쪽 열의 너비를 저장하고 앱이 열릴 때로드 할 수 있지만 초기 크기가되어야합니다. 사용자는 크기를 조정할 수 있어야합니다.
- 창을 크기를 조정할 때 왼쪽 및 오른쪽 열이 같은 크기와 중간 열이 창 너비로 크기를 조정하기를 원합니다.
- 왼쪽 및 오른쪽 열에는 최소 너비가 필요합니다. 오른쪽 열 크기를 조정할 때 중앙 열이 더 작지만 왼쪽 열이 없어지기를 원합니다.
- 또한 열 바깥에있는 토글 버튼으로 오른쪽 열의 가시성을 전환하고 가시성으로 돌아올 때 이전과 같은 너비가되기를 원합니다.
나는 XAML과 바인딩으로 가능한 한 많은 일을하려고 노력하고 있습니다.
크림, 아이스크림 및 초콜릿 칩을 얹어 줄 수 있습니까? :-)
해결책
내가 당신의 요구 사항을 읽을 때 Grid
, 나는 생각한다 DockPanel
.
<DockPanel>
<Grid Name="right"
DockPanel.Dock="Right" MinWidth="100" />
<Grid Name="Left"
DockPanel.Dock="Left" MinWidth="100" />
<Grid Name="middle" />
</DockPanel>
크기를 조정하는 방법 right
, 그 다음에 middle
변경됩니다 right
크기가 조정되었습니다. 창을 조정하는 경우에만 middle
바뀔 것이다. 저장 및 설정 Width
의 right
당신에게 달려 있지만 힘들어서는 안됩니다.
사용자가 크기를 조정하도록 허용합니다 right
, 그것은 조금 까다로울 것이지만 나는 이 기사 도움이 될 것입니다. 이 다른 기사 더 많은 도움이 될 수 있습니다.
가시성을 위해 right
, 당신은 그것을 설정할 수 있습니다 Visibility
에게 Collapsed
그것을 숨기고 설정하여 복원하려면 Visible
.
참고 : 내부의 패널은 필요하지 않습니다 Grid
s,하지만 당신은 일종의 사용을 원할 것입니다 Panel
각각. 현재 내부에 무엇이든지 Grid
열은 잘 작동해야합니다.
다른 팁
그리드 스플리터가있는 그리드를 사용하여 왼쪽 및 오른쪽 열의 너비를 유지하면서 중간 열을 쉽게 조정하기가 쉽기 때문입니다.
XAML :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MainWindow"
Title="Main Window"
Width="640" Height="480">
<Grid>
<Grid.ColumnDefinitions>
<!-- Left column -->
<ColumnDefinition Width="200" MinWidth="100"/>
<!-- Left GridSplitter column -->
<ColumnDefinition Width="5"/>
<!-- Center column. A width of * means the column will fill
any remaining space. -->
<ColumnDefinition Width="*"/>
<!-- Right GridSplitter column -->
<ColumnDefinition x:Name="RightSplitterColumn" Width="5"/>
<!-- Right column -->
<ColumnDefinition x:Name="RightColumn" Width="200"
MinWidth="100"/>
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" />
<GridSplitter Grid.Column="3" HorizontalAlignment="Stretch" />
<Button x:Name="ToggleButton" Grid.Column="2"
Content="Toggle Right Column" Width="150" Height="25"
Click="ToggleButton_Click" />
</Grid>
</Window>
코드-하인
오른쪽 열을 숨길 때는 그리드 열에 가시성 속성이 없기 때문에 열 폭을 0으로 설정합니다.
public partial class MainWindow : Window
{
private double rightColumnWidth;
private double rightColumnMinWidth;
private bool rightColumnHidden;
public MainWindow()
{
this.InitializeComponent();
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
if (rightColumnHidden)
{
// Restore the widths.
RightColumn.MinWidth = rightColumnMinWidth;
RightColumn.Width = new GridLength(rightColumnWidth);
RightSplitterColumn.Width = new GridLength(5);
}
else
{
// Remember the user-set widths for the columns.
rightColumnWidth = RightColumn.Width.Value;
rightColumnMinWidth = RightColumn.MinWidth;
// Remember to set the minimum width to 0 before changing the actual
// width.
RightColumn.MinWidth = 0;
RightColumn.Width = new GridLength(0);
RightSplitterColumn.Width = new GridLength(0);
}
rightColumnHidden = !rightColumnHidden;
}
}
시작시 열 너비를 저장하고 복원 할 때 너비 변수를 설정 파일에 저장 한 다음 앱을 다시 열 때 적용합니다.
columnDefinition 너비를 자동으로 설정하고 해당 열 안에 컨트롤을 넣고 다른 열에 별을 줘. 컨텐츠로 열을 숨기려면 컨트롤을 설정하고 Visibility = 붕괴되고 열 너비가 자동이므로 열이 표시되지 않으며 나머지 열이 공간을 차지합니다.
3 년 후 CodeProject에서 다른 접근 방식을 찾을 수 있습니다.
http://www.codeproject.com/articles/437237/wpf-grid-column-and-row-hiding
사용자 정의 열 정의에 "보이는"속성을 추가합니다.