Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListBox赢得';t以多列WPF水平显示数据_C#_Wpf_Xaml_Listbox - Fatal编程技术网

C# ListBox赢得';t以多列WPF水平显示数据

C# ListBox赢得';t以多列WPF水平显示数据,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,我在这里读了一些关于这个问题的帖子,仍然没有找到解决我问题的方法,我也用过这个 我的列表框仍然显示为一个带有ScrollViewer的长列表,而不是我所指的水平列表 XAML:已编辑 <DockPanel LastChildFill="False" IsEnabled="{Binding IsWindowEnabled}" MinWidth="724" Background="Red"> <!-- save notifictaion popup --> &

我在这里读了一些关于这个问题的帖子,仍然没有找到解决我问题的方法,我也用过这个

我的列表框仍然显示为一个带有ScrollViewer的长列表,而不是我所指的水平列表

XAML:已编辑

<DockPanel LastChildFill="False" IsEnabled="{Binding IsWindowEnabled}" MinWidth="724" Background="Red">
    <!-- save notifictaion popup -->
    <Label DockPanel.Dock="Top" Margin="10" FontSize="15" Foreground="Green" Content="{x:Static ml:MultiLang._123}" x:Name="ML_0003" />
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="10 0">
        <Label Content="Select Constant Type" Margin="0 0 10 0" />
        <ComboBox Width="150" ItemsSource="{Binding ComboNames}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding SelectedComboType}" />
    </StackPanel>
    <ListBox DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Stretch"  Margin="10" ItemsSource="{Binding Combos}" SelectedItem="{Binding SelecetedCombo}" ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Margin="0 0 0 5">
                    <Label Content="{Binding Index}" Margin="10 0" Width="20" />
                    <TextBox Width="150" Margin="0" IsEnabled="{Binding IsEditable}" Text="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DockPanel>

但我的清单仍然是这样的:

图像:第2期编辑

我做错了什么


多谢各位

请尝试从更改ItemPanel

<ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>


忽略上述建议

编辑

请尝试为DataTemplate中使用的内部堆栈面板提供背景色,好吗?像

<DataTemplate>
                <StackPanel Background="Blue" Orientation="Horizontal" Margin="0 0 0 5">
                    <Label Content="{Binding Index}" Margin="10 0" Width="20" />
                    <TextBox Width="150" Margin="0" IsEnabled="{Binding IsEditable}" Text="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
                </StackPanel>
            </DataTemplate>

它将让您了解stackpanel是如何占用ListBox项中的空间的。然后,我们可以尝试调整每个ListBoxItem的宽度

编辑2

这就是我所看到的

XAML是

<Window x:Class="WPF_StackOverFlow_Lab.Test1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPF_StackOverFlow_Lab"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<DockPanel LastChildFill="False" IsEnabled="{Binding IsWindowEnabled}" MinWidth="724" Background="Red">
    <!-- save notifictaion popup -->
    <Label DockPanel.Dock="Top" Margin="10" FontSize="15" Foreground="Green" Content="Constants Configuration" x:Name="ML_0003" />
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="10 0">
        <Label Content="Select Constant Type" Margin="0 0 10 0" />
        <ComboBox Width="150" ItemsSource="{Binding ComboNames}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding SelectedComboType}" />
    </StackPanel>
    <ListBox DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Stretch"  Margin="10" ItemsSource="{Binding Combos}" SelectedItem="{Binding SelecetedCombo}" ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Margin="0 0 0 5">
                    <Label Content="{Binding Index}" Margin="10 0" Width="20" />
                    <TextBox Width="150" Margin="0" IsEnabled="{Binding IsEditable}" Text="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DockPanel>


问题是,您在列表框中设置了220的宽度,由于项目位于“包裹”面板中,它们将垂直显示,请尝试增加宽度

<ListBox Width="220" [OTHER PROPERTIES]>
<ListBox Width="520" [OTHER PROPERTIES]>

增加宽度

<ListBox Width="220" [OTHER PROPERTIES]>
<ListBox Width="520" [OTHER PROPERTIES]>


或者,如果您的设计允许,请删除width属性并让listbox填充其父项。默认情况下,listbox使用名为VirtualzingStackPanel的面板来排列其项 垂直的

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>


首先尝试将
HorizontalAlignment=“Stretch”
放在列表框上。然后它应该水平拉伸以适合其父对象。在
DockPanel
中以您想要的方式对齐也可能很棘手。我已经尝试过了,但没有成功。DockPanel是否在网格中?还是怎样在WPF中,所有内容都是相对于其父对象进行布局的。这整件事都在一个标签里吗?或者什么?不,DockPanel在UserControl中,周围没有任何东西。@Tuco找到了。唯一的错误是ListBox上的宽度属性。您将固定宽度设置为220,因此当然这就是它的大小。只需删除该属性,忽略他的建议,设置一个不同的错误值。他根本不应该使用固定宽度。他应该删除Width属性,使其填充父对象。好主意,这样做,但列表仍然只在一个大列上。@Orionlk为了测试,请尝试将DockPanel的背景设置为红色,以查看其是否填充父对象。如果它没有填充自己的父对象,请将DockPanel的水平对齐设置为StretchGood thinking!但在将DockPanel的背景更改为红色后,它确实填充了其父对象。试图将DockPanel的水平对齐设置为拉伸,但仍然没有任何结果。仅供参考-此UserControl有一个调用it@EdPlunkett将我的XAML和图片编辑为当前状态。将其更改为StackPanel,但仍然没有成功,宽度也更改为更大的值,还尝试了“自动”,还删除了此属性,仍然是一列:(@Orionlk这是错误的。
WrapPanel
StackPanel
在与问题相关的各个方面的行为都相同。尝试了Background=“LightBlue”东西,在我的帖子中编辑了我的图片,仍然不起作用。@Orionlk,我认为内部堆栈面板正在将其扩展到所有空间。做一件事。将此堆栈面板的宽度设置为
width=190
,然后让我们看看是什么happens@MuhammadUmmar,它只是在项目周围为StackPanel着色,这取决于ListBox父项的宽度如果或多或少。列表框都按预期工作,则它拒绝在多个列中显示。