【问题标题】:Issue with nested grid on WPFWPF上的嵌套网格问题
【发布时间】:2016-02-15 21:40:51
【问题描述】:

我正在使用嵌套网格。在主网格中我有两个单选按钮,而在第二个中我有两个额外的单选按钮和其他控件..

我希望总网格为 3 行 3 列。

相关代码为:

    <xctk:WizardPage Name="Page3" PageType="Interior"
            Title="Stages" 
            Background="#FF27E0E0">
        <xctk:WizardPage.CanSelectNextPage>
            <MultiBinding Converter="{StaticResource NextFromPage3}">
                <Binding ElementName="HR" Path="IsChecked" Mode="OneWay"/>
                <Binding ElementName="LR" Path="IsChecked" Mode="OneWay"/>
                <Binding ElementName="yes" Path="IsChecked" Mode="OneWay"/>
                <Binding ElementName="no" Path="IsChecked" Mode="OneWay"/>
            </MultiBinding>
        </xctk:WizardPage.CanSelectNextPage>

        <Grid ShowGridLines="True" Margin="-5 -10">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Label x:Name="qual" Content="Select Quality:"  FontSize="13.333" Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left"/>
            <RadioButton x:Name="HR" Content="High-Resolution" FontSize="13.333" Grid.Column="1" Margin="5 10" VerticalAlignment="Top"/>
            <RadioButton x:Name="LR" FontSize="13.333" Content="Low-Resolution" Grid.Column="2" Margin="5 10" VerticalAlignment="Top"/>
            <Grid Grid.Row="1">
                <Grid.Visibility>
                    <MultiBinding Converter="{StaticResource FilterConverter}">
                        <Binding ElementName ="HR" Path="IsChecked" Mode="OneWay"/>
                        <Binding ElementName ="LR" Path="IsChecked" Mode="OneWay"/>
                    </MultiBinding>
                </Grid.Visibility>
                <Label x:Name="symbol" Content="Select Symbol:" Grid.Row ="1" Grid.Column="0" FontSize="13.333" Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left"/>
                <ComboBox x:Name="symbolsCmbBox" FontSize="13.333" Grid.Row="1" Grid.Column="1"  Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left" ItemTemplate="{StaticResource cmbTemplate}" IsSynchronizedWithCurrentItem="True" SelectedIndex="0">
                    <ComboBox.ItemsSource>
                        <MultiBinding Converter="{StaticResource SymbolComboboxItemsFilter}">
                            <Binding ElementName ="HR" Path="IsChecked" Mode="OneWay"/>
                            <Binding ElementName ="LR" Path="IsChecked" Mode="OneWay"/>
                        </MultiBinding>
                    </ComboBox.ItemsSource>
                </ComboBox>
                <Label x:Name="isExists" Content="Select Yes if process will perform:" Grid.Row ="2" FontSize="13.333" Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left"/>
                <RadioButton x:Name="yes" Content="Yes" FontSize="13.333" Grid.Row="2"  Grid.Column="1" Margin="5 10" VerticalAlignment="Top"/>
                <RadioButton x:Name="no" Content="No" FontSize="13.333" Grid.Row="2" Grid.Column="2" Margin="5 10" VerticalAlignment="Top"/>
            </Grid>
        </Grid>

以上代码是 WPF 工具包中存在的向导的一部分。 我与上面那个控件的嵌套网格问题没有按预期定位。 虽然我只使用了一个主网格,但这个问题并没有发生,但我出于其他原因需要嵌套网格..

编辑-第一个答案: 我希望主网格是第一行(3 列),内部网格将从第二行开始,总共包含两行(3 列),那么如何指定呢?

编辑-第二个答案:

第一行(主网格)与第二行和第三行(内网格)不直 - 似乎主网格的两个单选按钮都仅位于 column=2 上,即使它看起来不是xml代码。我的代码现在就像出现在下面的第二个答案中一样。

感谢您的建议!

【问题讨论】:

    标签: c# wpf grid nested wpftoolkit


    【解决方案1】:

    您尝试在内部网格上使用 Grid.Row 和 Grid.Column,但没有指定 RowDefinitions 或 ColumnDefinitions。如果您希望该网格的子项能够位于特定位置,则需要为内部网格指定它们。

    编辑:如果我了解您当前的要求,您需要一个 3x3 网格,其中一个 2x3 网格从内部开始,在第二行。 (你可能只需要一个 2x3 的网格,用于外部,但让我们使用你所拥有的。)

    您需要我上面提到的行和列定义,但您还需要做另外两件事。 1. 确保您孩子的 Grid.Row 设置为 0 和 1,而不是分别设置为 1 和 2,以便它们与内部网格重合。 2. 确保内部网格使用 2 行跨度和 3 列跨度来填充外部网格拥有的区域。

    它应该看起来像这样:(如果您需要特定大小,请编辑行和列定义)

    <Grid ShowGridLines="True" Margin="-5 -10">
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
      </Grid.ColumnDefinitions>
      <Label x:Name="qual" Content="Select Quality:"  FontSize="13.333" Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left"/>
      <RadioButton x:Name="HR" Content="High-Resolution" FontSize="13.333" Grid.Column="1" Margin="5 10" VerticalAlignment="Top"/>
      <RadioButton x:Name="LR" FontSize="13.333" Content="Low-Resolution" Grid.Column="2" Margin="5 10" VerticalAlignment="Top"/>
      <Grid Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="3">
        <Grid.RowDefinitions>
          <RowDefinition/>
          <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition/>
          <ColumnDefinition/>
          <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.Visibility>
          <MultiBinding Converter="{StaticResource FilterConverter}">
            <Binding ElementName ="HR" Path="IsChecked" Mode="OneWay"/>
            <Binding ElementName ="LR" Path="IsChecked" Mode="OneWay"/>
          </MultiBinding>
        </Grid.Visibility>
        <Label x:Name="symbol" Content="Select Symbol:" Grid.Row ="0" Grid.Column="0" FontSize="13.333" Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <ComboBox x:Name="symbolsCmbBox" FontSize="13.333" Grid.Row="0" Grid.Column="1"  Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left" IsSynchronizedWithCurrentItem="True" SelectedIndex="0">
          <ComboBox.ItemsSource>
            <MultiBinding Converter="{StaticResource SymbolComboboxItemsFilter}">
              <Binding ElementName ="HR" Path="IsChecked" Mode="OneWay"/>
              <Binding ElementName ="LR" Path="IsChecked" Mode="OneWay"/>
            </MultiBinding>
          </ComboBox.ItemsSource>
        </ComboBox>
        <Label x:Name="isExists" Content="Select Yes if process will perform:" Grid.Row ="1" FontSize="13.333" Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <RadioButton x:Name="yes" Content="Yes" FontSize="13.333" Grid.Row="1"  Grid.Column="1" Margin="5 10" VerticalAlignment="Top"/>
        <RadioButton x:Name="no" Content="No" FontSize="13.333" Grid.Row="1" Grid.Column="2" Margin="5 10" VerticalAlignment="Top"/>
      </Grid>
    </Grid>
    

    【讨论】:

    • 我编辑了我的问题-我希望主网格是第一行(3 列),内部网格将从第二行开始,总共包含两行(3 列)所以如何指定?
    【解决方案2】:

    指定的嵌套网格的行号和列号是指嵌套网格而不是父主网格。您需要将网格定义如下:

    <Grid ShowGridLines="True" Margin="-5 -10">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Label x:Name="qual" Content="Select Quality:"  FontSize="13.333" Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left"/>
    <RadioButton x:Name="HR" Content="High-Resolution" FontSize="13.333" Grid.Column="1" Margin="5 10" VerticalAlignment="Top"/>
    <RadioButton x:Name="LR" FontSize="13.333" Content="Low-Resolution" Grid.Column="2" Margin="5 10" VerticalAlignment="Top"/>
    <Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.Visibility>
            <MultiBinding Converter="{StaticResource FilterConverter}">
                <Binding ElementName ="HR" Path="IsChecked" Mode="OneWay"/>
                <Binding ElementName ="LR" Path="IsChecked" Mode="OneWay"/>
            </MultiBinding>
        </Grid.Visibility>
        <Label x:Name="symbol" Content="Select Symbol:" Grid.Row ="0" Grid.Column="0" FontSize="13.333" Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <ComboBox x:Name="symbolsCmbBox" FontSize="13.333" Grid.Row="1" Grid.Column="1"  Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left" ItemTemplate="{StaticResource cmbTemplate}" IsSynchronizedWithCurrentItem="True" SelectedIndex="0">
            <ComboBox.ItemsSource>
                <MultiBinding Converter="{StaticResource SymbolComboboxItemsFilter}">
                    <Binding ElementName ="HR" Path="IsChecked" Mode="OneWay"/>
                    <Binding ElementName ="LR" Path="IsChecked" Mode="OneWay"/>
                </MultiBinding>
            </ComboBox.ItemsSource>
        </ComboBox>
        <Label x:Name="isExists" Content="Select Yes if process will perform:" Grid.Row ="1" FontSize="13.333" Margin="5 10" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <RadioButton x:Name="yes" Content="Yes" FontSize="13.333" Grid.Row="1"  Grid.Column="1" Margin="5 10" VerticalAlignment="Top"/>
        <RadioButton x:Name="no" Content="No" FontSize="13.333" Grid.Row="1" Grid.Column="2" Margin="5 10" VerticalAlignment="Top"/>
    </Grid>
    

    【讨论】:

    • 它不能解决我的问题——内部网格现在实际上是从第二行开始的(这很好),但它只在第一列上延伸,而不是在第二列和第三列上。我相应地编辑了我的问题。有什么想法吗?
    • 要使内部网格伸展并占据主网格的所有 3 列,我们需要指定 Grid.ColumnSpan="3"。
    猜你喜欢
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多