【问题标题】:WPF listbox two items side by sideWPF列表框两个项目并排
【发布时间】:2011-01-16 09:01:42
【问题描述】:

基本上我想创建一个表单。完成后可能需要一段时间,所以我想使用一个列表框,以便表单可以滚动。我想有一个标签,旁边有一个文本框,供用户输入。如何在列表框中并排放置标签和文本框?

另外,如果有人对如何创建表单有任何其他建议,请告诉我。

【问题讨论】:

  • 正如 Erno 所指出的,您不应该使用 ListBox,而且您的问题有点含糊,您能否指定如何添加新行,如果它需要是动态的等等?
  • 行应该是静态的,因为表单永远不会改变。所以我需要一种简单的方法来编辑表单,因为我无法在 Visual Studio 2010 的设计视图中看到整个内容。
  • 嗯,现在现有的答案应该足够了。只需使用包含两列的 Grid 以及表单所需的行数,然后将其放入 ScrollViewer,仅此而已。

标签: c# wpf xaml listbox


【解决方案1】:

不要使用 ListBox 来添加滚动功能,请使用 ScrollViewer。

您能用草图/画一幅图来解释您的想法吗?

<Grid>
    <ScrollViewer>
         <Grid ScrollViewer.VerticalScrollBarVisibility="Auto"
              ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  Width="auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Label Target="{Binding ElementName=textBlock}"
                       VerticalAlignment="Center">_Name:</Label>
                <TextBox Grid.Column="1"
                         x:Name="textBlock"
                         VerticalAlignment="Center"
                         Text="Enter text here" />
            </Grid>                
            <Border Grid.Column="1">
                <TextBlock Text="Anything you like" />
            </Border>
        </Grid>
    </ScrollViewer>
</Grid>

还有很多其他选择。例如,在我的示例中,您可以将 ScrollViewer 放在 Border 内。这将使边框的内容而不是整个表单可滚动。关键是确定您希望它的外观以及您希望它的行为方式。

最好的方法是在 Expression Blend 等设计器中绘制或制作原型。

【讨论】:

  • 这看起来是个好主意。谢谢!但是有没有办法在文本框旁边放一个标签?这个例子是文本框顶部的标签,我希望它是并排的,所以标签与那个特定的文本框相关联。
【解决方案2】:

将包含面板(例如网格)包装在 ScrollViewer - http://msdn.microsoft.com/en-us/library/ms750665.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 2023-04-09
    • 2017-05-01
    相关资源
    最近更新 更多