c# - 如何在网格中设置显示器宽度的 50%?

标签 c# wpf

我想将显示器的宽度设置为 50%。

第一行应与第二行相同:

The first line should be the same as the second

我的代码:

<DataTemplate x:Key="RowTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="0"
                 Grid.Row="1"
                 TextWrapping="Wrap"
                     Text="{Binding Mask}"/>
        <TextBox Grid.Column="1"
                 Grid.Row="1"
                 TextWrapping="Wrap"
                     Text="{Binding Value}"/>
    </Grid>
</DataTemplate>

It should be like that

最佳答案

我使用列表框重现了您的问题。

这种行为是因为列表框中有一个滚动查看器,它告诉内容它可以具有任意的宽度(和高度)。因此,* 措施没有设定一半的金额。

您可以通过禁用水平滚动来避免此行为。我还必须强制该行拉伸(stretch)以适合其父级。

工作标记:

<Window.Resources>
    <DataTemplate x:Key="RowTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0"
             TextWrapping="Wrap"
                 Text="{Binding Mask}"/>
            <TextBox Grid.Column="1"
             TextWrapping="Wrap"
                 Text="{Binding Value}"/>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ListBox ItemTemplate="{StaticResource RowTemplate}"
             ItemsSource="{Binding MyRows}"
             HorizontalContentAlignment="Stretch"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
    </ListBox>
</Grid>
</Window>

这里的两行关键是:

             HorizontalContentAlignment="Stretch"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled" >

通过一些测试数据,我看到:

enter image description here

关于c# - 如何在网格中设置显示器宽度的 50%?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63372077/

相关文章:

c# - SignalR - 更改服务器超时响应

wpf - 在 WPF DataGrid 中自定义 Ctrl+C 行为

wpf - 需要良好的WPF MVVM教程

c# - ASP.Net MVC 中模型启动的数据绑定(bind)(更新 UI)

wpf - MVVM Silverlight 框架选择

.net - WPF触发器更改父属性

C#读写给定缓冲区长度的文件并剪切溢出

c# - WEB API & BL关系

c# - 如何在 C# 中声明具有特殊字符的字符串常量

c# - C#中继承的性能考虑