我的布局有问题
<ListBox Name="Tweets" Grid.Row="1" ItemsSource="{Binding Path=Tweets}" Background="#1F0000FE">
<ListBox.ItemTemplate>
<DataTemplate>
<local:TweetTemplate Margin="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我的模板:
<Grid Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="25*" />
<RowDefinition Height="95*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="110" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="{Binding Path=FromImage}" Grid.RowSpan="2" Margin="7" />
<TextBlock Text="{Binding Path=From}" Margin="5" Grid.Column="1" />
<TextBlock Text="{Binding Path=Text}" TextWrapping="WrapWithOverflow" Grid.Row="1" Margin="7" Grid.Column="1" />
</Grid>
我的问题是当这个 <TextBlock Text="{Binding Path=Text}"
文本很长,它拉伸(stretch)了网格。然后拉伸(stretch)父控件。我希望文本不要拉伸(stretch)列表框,并且当它到达列表框宽度的末尾时,切换到新行。当调整窗口大小时,ListBox 的大小也应增加。这意味着使用所有窗口。
最佳答案
ListBox 创建 ScrollViewer 来显示项目,因此您应该使用 ScrollViewer.HorizontalScrollBarVisibility 属性手动禁用它:
<ListBox Name="Tweets"
Grid.Row="1"
ItemsSource="{Binding Path=Tweets}"
Background="#1F0000FE"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
...
</ListBox>
关于c# - WPF 中的控件布局帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4915587/