Grid.RowDefinition在运行时和设计时的不同行为

时间:2014-03-30 20:12:10

标签: c# wpf xaml

假设我有一个如下所示的网格:

<Grid>

    <Grid.Resources>
        <converters:MultiplyHeightByThreeConverter x:Key="multiplyHeightByThreeConverter" />
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding ElementName=rowPinCode, Path=ActualHeight, Converter={StaticResource multiplyHeightByThreeConverter}}"/>
        <RowDefinition x:Name="rowPinCode" Height="Auto" />
        .....
        .....
    </Grid.RowDefinitions>

    .....
    .....    

</Grid>

这是我的转换器代码:

public class MultiplyHeightByThreeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (((double)value) * 3);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

在设计时我得到预期的输出:

enter image description here

但是在运行时我没有得到那一行:

enter image description here

我也找到了原因:

我在转换器转换方法的左括号中保留了断点,发现value parameter的值为0.00

如何解决问题?

我还尝试使用Height代替ActualHeight,但后来因为无法将Auto转换为Double

而收到错误

更新:

<Page x:Class="WPF_Client.Pages.Masters.Single.Ledger"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:Self="clr-namespace:WPF_Client"
      xmlns:helpers="clr-namespace:WPF_Client.HelperClasses"
      xmlns:model="clr-namespace:Data;assembly=Data"
      mc:Ignorable="d" 
      d:DesignHeight="760" d:DesignWidth="1366"
      Title="Ledger" DataContext="{StaticResource mainWindowViewModel}">

    <Page.Resources>
        <CollectionViewSource x:Key="GroupNamesWithCorrespondingEffectsCollection" Source="{Binding GroupNamesWithCorrespondingEffects}" />
        <CollectionViewSource x:Key="CreditDebitsCollection" Source="{Binding CreditDebits}" />
    </Page.Resources>

    <Grid DataContext="{Binding CurrentLedger}">

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0">

            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="10" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="10" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="20" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="20" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.3*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="80" />
                <ColumnDefinition Width="0.15*" />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Row="1" Grid.Column="1" Text="Name" />
            <DockPanel Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="3" LastChildFill="True">
                <TextBlock DockPanel.Dock="Left" Text=":" />
                <TextBox Text="{Binding Name}" />
            </DockPanel>

            <TextBlock Grid.Row="3" Grid.Column="1" Text="Group" />
            <DockPanel Grid.Row="3" Grid.Column="2" Grid.ColumnSpan="3" LastChildFill="True">
                <TextBlock DockPanel.Dock="Left" Text=":" />
                <ComboBox ItemsSource="{DynamicResource Items}" 
                      SelectedValue="{Binding DataContext.SelectedGroupIDinLedger, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
                      SelectedValuePath="GroupID" Grid.IsSharedSizeScope="True" TextSearch.TextPath="GroupName">
                    <ComboBox.Resources>
                        <CompositeCollection x:Key="Items">
                            <ComboBoxItem IsEnabled="False" Background="#FF2A2A2A" Foreground="White">
                                <Grid TextElement.FontWeight="Bold" >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition SharedSizeGroup="A" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition SharedSizeGroup="B" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.Children>
                                        <TextBlock Grid.Column="0" Text="Group Name" />
                                        <TextBlock Grid.Column="2" Text="Effect" />
                                    </Grid.Children>
                                </Grid>
                            </ComboBoxItem>
                            <CollectionContainer Collection="{Binding Source={StaticResource GroupNamesWithCorrespondingEffectsCollection}}" />
                        </CompositeCollection>

                        <DataTemplate DataType="{x:Type helpers:GroupNameWithCorrespondingEffect}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition SharedSizeGroup="A" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition SharedSizeGroup="B" />
                                </Grid.ColumnDefinitions>
                                <Grid.Children>
                                    <TextBlock Grid.Column="0" Text="{Binding GroupName}" />
                                    <TextBlock Grid.Column="2" Text="{Binding CorrespondingEffect}" />
                                </Grid.Children>
                            </Grid>
                        </DataTemplate>
                    </ComboBox.Resources>
                </ComboBox>
            </DockPanel>

            <TextBlock Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2" Text="Opening Balance" />
            <DockPanel Grid.Row="5" Grid.Column="3" LastChildFill="True">
                <TextBlock DockPanel.Dock="Left" Text=":" />
                <TextBox Text="{Binding OpeningBalance}"/>
            </DockPanel>
            <ComboBox Grid.Row="5" Grid.Column="4" Margin="10,0,0,0" ItemsSource="{DynamicResource Items}" 
                      SelectedValue="{Binding DataContext.SelectedCreditDebitIDinLedger, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
                      SelectedValuePath="CreditDebitID" Grid.IsSharedSizeScope="True" TextSearch.TextPath="Symbol">
                <ComboBox.Resources>
                    <CompositeCollection x:Key="Items">
                        <ComboBoxItem IsEnabled="False" Background="#FF2A2A2A" Foreground="White">
                            <Grid TextElement.FontWeight="Bold" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition SharedSizeGroup="A" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition SharedSizeGroup="B" />
                                </Grid.ColumnDefinitions>
                                <Grid.Children>
                                    <TextBlock Grid.Column="0" Text="Value" />
                                    <TextBlock Grid.Column="2" Text="Symbol" />
                                </Grid.Children>
                            </Grid>
                        </ComboBoxItem>
                        <CollectionContainer Collection="{Binding Source={StaticResource CreditDebitsCollection}}" />
                    </CompositeCollection>

                    <DataTemplate DataType="{x:Type model:Master_CreditDebits}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition SharedSizeGroup="A" />
                                <ColumnDefinition Width="50" />
                                <ColumnDefinition SharedSizeGroup="B" />
                            </Grid.ColumnDefinitions>
                            <Grid.Children>
                                <TextBlock Grid.Column="0" Text="{Binding Value}" />
                                <TextBlock Grid.Column="2" Text="{Binding Symbol}" />
                            </Grid.Children>
                        </Grid>
                    </DataTemplate>
                </ComboBox.Resources>
            </ComboBox>

            <GroupBox Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="4" Header="Credit Limits" 
                      Visibility="{Binding DataContext.CreditLimitsVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}},
                                           Converter={StaticResource boolToVisibilityConverter}}">

                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="5" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="5" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Credit Days" />
                    <DockPanel Grid.Row="1" Grid.Column="1" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding CreditDays}"/>
                    </DockPanel>

                    <TextBlock Grid.Row="3" Grid.Column="0" Text="Credit Limit" />
                    <DockPanel Grid.Row="3" Grid.Column="1" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding CreditLimit}"/>
                    </DockPanel>

                </Grid>

            </GroupBox>

            <GroupBox Grid.Row="9" Grid.Column="1" Grid.ColumnSpan="4" Header="Bank Details"
                      Visibility="{Binding DataContext.BankDetailsVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}},
                                           Converter={StaticResource boolToVisibilityConverter}}">

                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="5" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="5" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="A/c No." />
                    <DockPanel Grid.Row="1" Grid.Column="1" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding BankAccountNo}"/>
                    </DockPanel>

                    <TextBlock Grid.Row="3" Grid.Column="0" Text="Branch" />
                    <DockPanel Grid.Row="3" Grid.Column="1" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding BranchName}"/>
                    </DockPanel>

                    <TextBlock Grid.Row="5" Grid.Column="0" Text="BSR Code" />
                    <DockPanel Grid.Row="5" Grid.Column="1" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding BSRCode}"/>
                    </DockPanel>

                </Grid>

            </GroupBox>

        </Grid>

        <Grid Grid.Column="1">

            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="20" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="20" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.15*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="0.3*" />
            </Grid.ColumnDefinitions>


            <!--Here is the problem part-->

            <GroupBox Grid.Row="1" Grid.Column="1" Header="General Details"
                      Visibility="{Binding DataContext.GeneralDetailsVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}},
                                           Converter={StaticResource boolToVisibilityConverter}}">

                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="5" />
                        <RowDefinition Height="{Binding ElementName=rowPinCode, Path=ActualHeight, Converter={StaticResource multiplyHeightByThreeConverter}}" />
                        <RowDefinition Height="10" />
                        <RowDefinition x:Name="rowPinCode" Height="Auto"/>
                        <RowDefinition Height="10" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="5" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Address" />
                    <DockPanel Grid.Row="1" Grid.Column="1" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding Address}"/>
                    </DockPanel>

                    <TextBlock Grid.Row="3" Grid.Column="0" Text="Pincode" />
                    <DockPanel Grid.Row="3" Grid.Column="1" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding PINCode}"/>
                    </DockPanel>

                    <TextBlock Grid.Row="5" Grid.Column="0" Text="City" />
                    <DockPanel Grid.Row="5" Grid.Column="1" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <ComboBox />
                    </DockPanel>

                    <TextBlock Grid.Row="7" Grid.Column="0" Text="State" />
                    <DockPanel Grid.Row="7" Grid.Column="1" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <ComboBox />
                    </DockPanel>

                </Grid>

            </GroupBox>

            <GroupBox Grid.Row="3" Grid.Column="1" Header="Contact Details"
                      Visibility="{Binding DataContext.ContactDetailsVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}},
                                           Converter={StaticResource boolToVisibilityConverter}}">


                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="5" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="5" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Contact Person" />
                    <DockPanel Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding ContactPerson}"/>
                    </DockPanel>

                    <TextBlock Grid.Row="3" Grid.Column="0"  Text="Phone No." />
                    <DockPanel Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />

                        <Grid>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <TextBox Grid.Column="0" Text="{Binding DataContext.TelephoneNo1, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"/>
                            <TextBox Grid.Column="1" Text="{Binding DataContext.TelephoneNo2, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}" Margin="5,0,0,0"/>

                        </Grid>

                    </DockPanel>

                    <TextBlock Grid.Row="5" Grid.Column="0" Text="Email Address" />
                    <DockPanel Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding EmailAddress}"/>
                    </DockPanel>

                    <TextBlock Grid.Row="7" Grid.Column="0" Text="Website" />
                    <DockPanel Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="2" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding Website}"/>
                    </DockPanel>

                </Grid>

            </GroupBox>

            <GroupBox Grid.Row="5" Grid.Column="1" Header="Tax Information"
                      Visibility="{Binding DataContext.TaxInformationVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}},
                                           Converter={StaticResource boolToVisibilityConverter}}">

                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="5" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="5" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="PAN / IT No." />
                    <DockPanel Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding PANCardOrITNo}"/>
                    </DockPanel>

                    <TextBlock Grid.Row="3" Grid.Column="0" Text="Sales Tax No." />
                    <DockPanel Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" LastChildFill="True">
                        <TextBlock DockPanel.Dock="Left" Text=":" />
                        <TextBox Text="{Binding SalesTaxNo}"/>
                    </DockPanel>

                </Grid>

            </GroupBox>

        </Grid>

    </Grid>

</Page>

更新2:

提出建议后:

在设计时:

enter image description here

在运行时:

enter image description here

1 个答案:

答案 0 :(得分:2)

你不需要转换器。如果您希望第一行是第二行的三倍,您可以使用3*给出相对宽度。

<Grid.RowDefinitions>
   <RowDefinition Height="3*"/>
   <RowDefinition x:Name="rowPinCode" Width="Auto" Height="*" />
   .....
</Grid.RowDefinitions>

<强>更新

使用上面提到的行定义在我的结尾完全正常。定义的声明:

<Grid.RowDefinitions>
    <RowDefinition Height="5" />
    <RowDefinition Height="3*" />
    <RowDefinition Height="10" />
    <RowDefinition x:Name="rowPinCode" Height="*"/>
    <RowDefinition Height="10" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="10" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="5" />
</Grid.RowDefinitions>

快照:

enter image description here