2017-03-16 11 views
6

WPF에서는 열 및 행 정의에 바인딩 한 길이가 부울 변환기가 있습니다.격자 열 및 행 길이의 WPF 변환기는 gridsplitter가 사용되지 않는 경우에만 작동합니다.

이 변환기는 행/열을 숨기거나 표시할지 여부를 결정하기 위해 바인드 된 부울을 읽는 데 사용됩니다.

이렇게하면 그리드의 주어진 부분을 "최대화"하거나 원래 크기로 되돌릴 수 있습니다. 이것은 그리드 스플리터를 사용하여 크기를 조정하지 않는 이상 작동합니다. 이 문제가 발생하면 더 이상 원하는 길이를 설정하지 않습니다. gridsplitter가 사용되면 열 정의에 대한 바인딩이 제거됩니다. 이 문제가 해결 되었습니까?

변환기

[ValueConversion(typeof(bool), typeof(GridLength))] 
public class BoolToGridLengthConverter : IValueConverter 
{ 
    public int Length { get; set; } 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return ((bool)value == true) ? new GridLength(Length == 0 ? 1 : Length, GridUnitType.Star) : new GridLength(0); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { // Don't need any convert back 
     return null; 
    } 
} 

XAML

<Grid> 
    <Grid.Resources> 
     <helpers:BoolToGridLengthConverter x:Key="BoolToGridLengthConverter1" Length="1" /> 
     <helpers:BoolToGridLengthConverter x:Key="BoolToGridLengthConverter2" Length="2" /> 
    </Grid.Resources> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="{Binding ShowColumn1, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" /> 
     <ColumnDefinition Width="5" /> 
     <ColumnDefinition Width="{Binding ShowColumn2, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" /> 
     <ColumnDefinition Width="5" /> 
     <ColumnDefinition Width="{Binding ShowColumn3, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="{Binding ShowRow1, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" /> 
     <RowDefinition Height="5" /> 
     <RowDefinition Height="{Binding ShowRow2, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter2}}" /> 
    </Grid.RowDefinitions> 
    <Grid Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="0"></Grid> 
    <GridSplitter Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="1" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="5" /> 
    <Grid Grid.Column="0" Grid.Row="2"></Grid> 
    <GridSplitter Grid.Column="1" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" /> 
    <Grid Grid.Column="2" Grid.Row="2"></Grid> 
    <GridSplitter Grid.Column="3" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" /> 
    <Grid Grid.Column="4" Grid.Row="2"></Grid> 
</Grid> 

편집 : 구조 조정 더 XAML을 포함하는 질문

편집 : 추가 정보를 원하시면, 내가 그리드 섹션을 확장하고자하는 I 모든 불리언을 거짓으로 설정하고 내가 원하는 섹션을 설정하십시오. 예를 들어 Grid.Column 2와 Grid.Row 2를 최대화하려면 ShowColumn2와 ShowRow2를 제외한 모든 부울을 false로 설정합니다. 이것은 의도 한대로 작동하고 불리언 값이 모두 true로 설정된 경우 원본 뷰를 다시 얻으려고 의도 한대로 작동합니다. 일단 그리드 스플리터를 사용하면 이전과 같이 작동하지 않는 열의 크기를 조정할 수 있습니다. 이전과 같은 섹션을 최대화하면 잘 작동하지만 원래 크기 인 Grid.Column 2로 다시 크기를 조정하려고하면 Grid.Row 2가 전체 하단 행을 차지합니다. 그 외의 두 열은 최소화됩니다.

그리드 분배기를 사용할 때 변환기가 더 이상 열의 값을 true로 설정할 수 없다고 생각합니다. 에서

+0

같은 부울 값을 기반으로 열의 내용 (격자, 스택 패널, 데이터 격자 등)에 가시성 변환기를 사용할 수 있습니다. 이렇게하면 GridLength에 대한 변환기가 필요하지 않습니다. –

+0

가시성 변환기를 사용했지만 다른 열의 크기를 조정하지 않았으므로 열 내용이 사라졌습니다. 그리드 조각가들이이 문제를 일으키고 있다고 생각하니? Visibility Converter를 동일한보기에서 사용하고 의도 한대로 작동하지만 그 그리드에는 그리드 작성자가 없습니다. –

+0

@DavidLee,보기의 xaml 마크 업을 포함하십시오. – ASh

답변

1

당신의 ColumnDefinition 당신이 에 대해 0에 ColumnMaxHeight을 숨기기 위해 MaxWidth 제로에 설정해야합니다.

<Grid> 
    <Grid.Resources> 
     <local:BoolToMaxConverter x:Key="BoolToMaxConverter" /> 
    </Grid.Resources> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition MaxWidth="{Binding ShowColumn1, Converter={StaticResource BoolToMaxConverter}}" /> 
     <ColumnDefinition Width="5" /> 
     <ColumnDefinition MaxWidth="{Binding ShowColumn2, Converter={StaticResource BoolToMaxConverter}}" /> 
     <ColumnDefinition Width="5" /> 
     <ColumnDefinition MaxWidth="{Binding ShowColumn3, Converter={StaticResource BoolToMaxConverter}}" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition MaxHeight="{Binding ShowRow1, Converter={StaticResource BoolToMaxConverter}}" /> 
     <RowDefinition Height="5" /> 
     <RowDefinition MaxHeight="{Binding ShowRow2, Converter={StaticResource BoolToMaxConverter}}" /> 
    </Grid.RowDefinitions> 
    <Grid Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="0"></Grid> 
    <GridSplitter Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="1" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="5" /> 
    <Grid Grid.Column="0" Grid.Row="2"></Grid> 
    <GridSplitter Grid.Column="1" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" /> 
    <Grid Grid.Column="2" Grid.Row="2"></Grid> 
    <GridSplitter Grid.Column="3" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" /> 
    <Grid Grid.Column="4" Grid.Row="2"></Grid> 
</Grid> 

는 또한 TwoWay-Binding을 제거하기 위해 고려해야

[ValueConversion(typeof(bool), typeof(double))] 
public class BoolToMaxConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return ((bool)value == true) ? double.MaxValue : 0d; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { // Don't need any convert back 
     return null; 
    } 
} 

하고 XAML :

는 다음과 같은 변환을 시도합니다.

+0

매력처럼 작동했습니다. 이것을 시험하기 위해 이전 버전을 되살려 야 할 때 이것을 대답으로 표시하는 것에 대한 지연에 대해 나쁘게 생각합니다. 나는 그것이 작동하도록 만들지 못했기 때문에 UI 레이아웃을 완전히 바 꾸었습니다. –