窗口通过按钮添加网格行时自动调整大小

时间:2017-05-10 17:16:33

标签: wpf

我有一个带有基本网格的窗口和一个向网格添加网格行和用户控件的按钮。所有用户控件的大小都相同。如何自动增加添加的每个用户控件的窗口大小。

XAML for window:

<DockPanel>
        <Grid DockPanel.Dock="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="75"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="75"/>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="1" DockPanel.Dock="Top"  Height="22" x:Name="AddRow" Click="AddRow_Click">
                <TextBlock Text="Add Skill"/>
            </Button>
            <Button Grid.Column="0" DockPanel.Dock="Top"  Height="22" x:Name="Close" Click="Close_Click" >
                <TextBlock Text="Exit"/>
            </Button>
            <Button Grid.Column="2" DockPanel.Dock="Top"  Height="22" x:Name="Minimize" Click="Minimize_Click" >
                <TextBlock Text="Minimize"/>
            </Button>
        </Grid>


            <Grid x:Name="SkillGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">                
                <Grid.RowDefinitions> 
                </Grid.RowDefinitions>
            </Grid>


    </DockPanel>

用于usercontrol的Xaml:

<Grid x:Name="grid1">

        <StackPanel>           
            <StackPanel.Style>
                <Style TargetType="StackPanel">
                    <Setter Property="Orientation" Value="Horizontal"/>
                    <Setter Property="DockPanel.Dock" Value="Top"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=grid1,Path=ActualWidth,Converter={StaticResource RestrictedWidthOverflowConverter}}" Value="True">
                            <Setter Property="Orientation" Value="Vertical"/>
                            <Setter Property="DockPanel.Dock" Value="Left"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Style>    

            <Grid Margin="3" Grid.Column="0">
                <Button  HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="AgentWindow" Click="AgentWindow_Click">
                    <TextBlock x:Name="button" Text="agent"/>
                    <Button.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Open Full Agent List" Click="MenuItem_Click"/>
                            <MenuItem Header="Open Aux Only" Click="MenuItem_Click_1"/>
                        </ContextMenu>
                    </Button.ContextMenu>
                </Button>
            </Grid>

            <Grid Margin="3" Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <TextBlock TextWrapping="Wrap" Grid.Row="0" x:Name="TeamNameText" VerticalAlignment="Bottom" HorizontalAlignment="Center" Foreground="White" Text="Team Name"/>
                <TextBlock Grid.Row="1" x:Name="SkillNum"  HorizontalAlignment="Center" Foreground="White" Text="Skill Number" VerticalAlignment="Top" FontSize="15"/>
            </Grid>
            <Grid Margin="3" Grid.Column="2">
                <Image Margin="8, 8, 8, 8" x:Name="CallImage" VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Grid>
            <Grid Margin="3" Grid.Column="3">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Bottom" Grid.Row="0" Grid.ColumnSpan="2" x:Name="WaitTimeText" Text="Wait Time"/>

                <TextBlock Margin="5" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" Grid.Row="1" Grid.Column="0" FontSize="15" x:Name="AgentAvailableData" Text="Available Number"/>
                <TextBlock Margin="5" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" Grid.Row="1" Grid.Column="1" FontSize="15" x:Name="WaitTimeData" Text="Wait Time"/>
            </Grid>

            <Grid Margin="3" Grid.Column="4">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Column="4" Grid.RowSpan="2" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="HotSeat" Text="HotSeat" MouseDown="HotSeat_MouseDown" MouseLeave="HotSeat_MouseLeave"/>
            </Grid>

        </StackPanel>
    </Grid>

添加用户控件的代码:

private void AddRow_Click(object sender, RoutedEventArgs e)
        {                
            RowDefinition c1 = new RowDefinition();
            c1.Height = new GridLength(20, GridUnitType.Star);
            SkillGrid.RowDefinitions.Add(c1);    

            var uc = new ScoreSingleLineDisplayUCV2();
            SkillGrid.Children.Add(uc);
            Grid.SetRow(uc, SkillGrid.RowDefinitions.Count - 1);
        }

0 个答案:

没有答案