在WPF中正确填充堆栈面板中的项目

时间:2013-03-18 19:27:53

标签: wpf xaml

我正在尝试将网格视图下方的文本块,文本框和按钮对齐。现在他们的顺序是正确的,但他们是在左边。我希望它们从左到右均匀地间隔在网格视图下方,并在整个窗口上伸展。这是xaml。

 <Grid>        
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto"/>                                     
        </Grid.RowDefinitions>
        <StackPanel>          
           <telerik:RadGridView x:Name="radGridView" 
                             GroupRenderMode="Flat"
                             ShowGroupPanel="True"                                                      
                             ColumnWidth="*" 
                             IsReadOnly="True"                                                                                     
                             Grid.Row="0"                                                         
                             CanUserFreezeColumns="False"
                             RowIndicatorVisibility="Collapsed"
                             CanUserResizeColumns="True"
                             ShowGroupFooters="True"                              
                             ShowColumnFooters="True" 
                             ItemsSource="{Binding Path=Tester}" />
            <telerik:RadDataPager x:Name="radDataPager"
                              Grid.Row="1"                                                           
                              PageSize="10"
                              Source="{Binding Items, ElementName=radGridView}"
                              DisplayMode="All"
                              IsTotalItemCountFixed="True"/> 
           <StackPanel Orientation="Horizontal">                           
            <TextBlock Grid.Row="2" 
                       TextWrapping="Wrap"
                       HorizontalAlignment="Stretch"
                       Text="Search Testing"/>
            <TextBox ToolTip="Enter Search into box"
                    x:Name="txtName" 
                    Grid.Row="2"                    
                    FontFamily="Courier New"
                    HorizontalAlignment="Stretch"
                    TextWrapping="Wrap" 
                    Text="{Binding SearchText}"/>
            <telerik:RadButton ToolTip="Click To Search"
                    x:Name="btnSearch"           
                    Content="Search"
                    Grid.Row="2"  
                    HorizontalAlignment="Stretch"           
                    Click="RadButton_Click_1"/>
            </StackPanel>
        </StackPanel>
    </Grid>

1 个答案:

答案 0 :(得分:3)

使用网格而不是堆叠面板:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TextBlock TextWrapping="Wrap"
               Width="auto"
               HorizontalAlignment="Stretch"
               Text="Search Testing"/>
    <TextBox ToolTip="Enter Search into box"
             Grid.Column="1"
             Width="auto"
             x:Name="txtName"               
             FontFamily="Courier New"
             HorizontalAlignment="Stretch"
             TextWrapping="Wrap" 
             Text="{Binding SearchText}"/>
    <telerik:RadButton ToolTip="Click To Search"
               Grid.Column="2"
               Width="auto"
               x:Name="btnSearch"           
               Content="Search" 
               HorizontalAlignment="Stretch"           
               Click="RadButton_Click_1"/>
</Grid>