c# - WPF 中的控件布局帮助

标签 c# .net wpf xaml

我的布局有问题

<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.Horizo​​ntalScrollBarVisibility 属性手动禁用它:

    <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/

相关文章:

javascript - 禁用 dropzone 页面刷新

.net - 拆分容器,制作固定面板

WPF:如何在弹出窗口中淡入和缩放用户控件

WPF 用户控件与 MVVM ViewModel 的数据绑定(bind)

c# - 高效的运行时类型检查

c# - 如何禁用 gif 图像的循环并将其保存在 C# 中

c# - 不明确调用的默认重载

C# 控制台应用程序在 Windows Server 上无法正常工作(在 win XP 和 win 7 上工作正常)

c# - 排除在调试中构建的文件

c# - 将 WPF 应用程序作为 Windows 服务运行