簡體   English   中英

僅當不使用gridsplitter時,網格列和行長度上的WPF轉換器才有效

[英]WPF converter on grid column and row length only works if gridsplitter is not used

在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

編輯:附加信息,當我希望擴展網格部分時,我將所有布爾值設置為false並將我想要的部分設置為true。 例如,如果我希望最大化Grid.Column 2和Grid.Row 2,我將所有布爾值設置為false,除了ShowColumn2和ShowRow2。 這可以按預期工作,當布爾值全部設置為true時,為了獲得原始視圖,它也可以按預期工作。 問題是,一旦我使用gridsplitter調整列的大小,它就像以前一樣不起作用。 當我最大化相同的部分時,它工作正常,但當我嘗試調整大小恢復到原始大小Grid.Column 2和Grid.Row 2占用整個底部行。 除了它們之外的兩列最小化。

這讓我相信,當使用網格探測器時,轉換器將無法再將列的值設置為true。

在你ColumnDefinition您需要將設置MaxWidth零隱藏ColumnMaxHeight的零Row

嘗試以下轉換器:

[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:

<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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM