LastChild填充控件组

时间:2014-01-05 09:58:46

标签: wpf

我有一个DockPanel,其行定义为LastChildFill =“true”,行中的最后一项是两个独立的控件,表示文件选择器 - 文件路径的TextBox,后跟显示打开文件的Button对话框(填充文本框)。我想将这组控件停靠在行的末尾,以便TextBox而不是Button被拉伸以填充空格。这可能吗?

<DockPanel Grid.Row="1"
       Margin="3"
       LastChildFill="True">
    <!-- * additional controls omitted * -->
    <TextBox x:Name="TExecutable"
             Width="200"
             VerticalContentAlignment="Center"
             Text="{Binding Locale[event_SampleExecutable],
                            Source={StaticResource PluginViewModel}}"/>
    <Button Margin="3 0 0 0"
            VerticalContentAlignment="Center"
            Command="{Binding SelectExecutableCommand}"
            Content="{Binding Locale[event_SelectExecutableText],
                              Source={StaticResource PluginViewModel}}" />
</DockPanel>

DockPanel Row

2 个答案:

答案 0 :(得分:2)

您需要做以下事情:

  1. 首先将按钮放在XAML中,即在TextBox上方,这样 TextBox将成为DockPanel中的LastChild ,并且将成为完全拉伸的候选者。
  2. 从TextBox中删除带编码的宽度,因为您希望将其拉伸。
  3. 在按钮上设置DockPanel.Dock="Right",使其位于textBox右侧。
  4. 在textBox上设置DockPanel.Dock="Left"

    <DockPanel Grid.Row="1"
               Margin="3"
               LastChildFill="True">
        <!-- * additional controls omitted * -->
        <Button Margin="3 0 0 0"
                VerticalContentAlignment="Center"
                DockPanel.Dock="Right"
                Command="{Binding SelectExecutableCommand}"
                Content="{Binding Locale[event_SelectExecutableText],
                              Source={StaticResource PluginViewModel}}" />
        <TextBox x:Name="TExecutable"
                 DockPanel.Dock="Left"
                 VerticalContentAlignment="Center"
                 Text="{Binding Locale[event_SampleExecutable],
                           Source={StaticResource PluginViewModel}}"/>
    </DockPanel>
    

答案 1 :(得分:2)

用Grid包裹:

<DockPanel 
       Margin="3"
       LastChildFill="True">
    <!-- * additional controls omitted * -->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <TextBox x:Name="TExecutable"
                 VerticalContentAlignment="Center"
                 Text="{Binding Locale[event_SampleExecutable],
                                Source={StaticResource PluginViewModel}}"/>
        <Button Margin="3 0 0 0" Grid.Column="1"
                VerticalContentAlignment="Center"
                Command="{Binding SelectExecutableCommand}"
                Content="{Binding Locale[event_SelectExecutableText],
                                  Source={StaticResource PluginViewModel}}" />
    </Grid>
</DockPanel>