【发布时间】:2016-11-29 14:02:56
【问题描述】:
我在这里阅读了一些关于该问题的帖子,但仍然没有找到解决我的问题的方法,我也使用了这个guide。
我的 ListBox 仍然显示为带有 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>
但我的列表仍然是这样的:
图片:EDITED No.2
我做错了什么?
非常感谢!
【问题讨论】:
-
首先尝试将
HorizontalAlignment="Stretch"放在ListBox 上。然后它应该水平拉伸以适合其父级。在DockPanel中以您想要的方式对齐也可能很棘手。 -
我试过了,没有成功。
-
DockPanel 是否在网格内?要不然是啥?在 WPF 中,所有内容都是相对于其父级布局的。这整件事都在 TabItem 内吗?还是什么?
-
不,DockPanel 在 UserControl 内部,周围没有任何东西。
-
@Tuco 明白了。唯一错误的是 ListBox 上的 Width 属性。您设置了 220 的固定宽度,所以它当然有多大。只需删除该属性,忽略他设置不同错误值的建议。