如何调整Datagrid绑定数据表(WPF)以显示更多列而不是放大文本? [英] How to resize Datagrid binding a Datatable (WPF) to show more columns instead of magnifying text?

查看:56
本文介绍了如何调整Datagrid绑定数据表(WPF)以显示更多列而不是放大文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个简单的WPF窗口,显示一个按钮,TextBox和DataGrid。 DataGrid由多列DataTable(读取Excel工作表)和数据绑定填充:dg.DataContext:= dt.DefaultView。我事先不知道表格是什么样的(用户
可以选择任何Excel表格)所以我不能预定义列宽。

I have a simple WPF window showing a button, TextBox and DataGrid . The DataGrid is filled from a multi column DataTable (reading an Excel sheet) with databinding : dg.DataContext:=dt.DefaultView. I do not know in advance how the table looks like (the user can select any Excel sheet) so I can't predefine column widths.

当用户调整窗口大小时,DataGrid会随之调整大小但是(当你使窗口变大时)DataGrid中的相同内容仍然可见(相同数量的单元格)但只有更大(更大的字体大小)。当DataGrid获得更多空间来显示它时,我希望DataGrid显示更多列和
行(如果仍然可用),而不是放大现有视图。

When the users resizes the window, the DataGrid resizes with it but (when you make the window larger) the same content in the DataGrid remains visible (same number of cells) but only larger (larger font size). I want the DataGrid to show more columns and rows (if still available of course) when the DataGrid gets more space to display it, not to enlarge the existing view.

我的高度/宽度各不相同,显示/没有显示滚动条,但没有结果。

I've varied height/width and showing/not showing scrollbars, but without result.

我如何实现这一目标?这是XAML:

How do I achieve that? This is the XAML:

推荐答案

你好迪克,你是
而不是ViewBox你可以使用像这个演示中的Slider和LayoutTransform:

Hi Dick,
instead of ViewBox you can use a Slider and LayoutTransform like in this Demo:

<Window x:Class="Window15"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="Window15" Height="450" Width="800">
  <Window.DataContext>
    <local:Window15VM/>
  </Window.DataContext>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="auto"/>
      <RowDefinition/>
      <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Name="tb" HorizontalAlignment="Stretch" Height="19" Margin="5" TextWrapping="Wrap" Text="--" RenderTransformOrigin="3.98,0.09"/>
    <Button Grid.Column="1" Content="Open Excel Sheet" HorizontalAlignment="Right" Margin="5" Width="210" Command="{Binding Cmd}"/>
    <DataGrid Grid.Row="1" Grid.ColumnSpan="2" Name="dg" ItemsSource="{Binding View}" HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="375" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Margin="5">
      <DataGrid.LayoutTransform>
        <ScaleTransform ScaleX="{Binding ElementName=sl, Path=Value}"
                        ScaleY="{Binding ElementName=sl, Path=Value}"/>
      </DataGrid.LayoutTransform>
    </DataGrid>
    <Slider x:Name="sl" Grid.Row="2" Grid.Column="1" Value="1" Minimum=".3" Maximum="5"/>
  </Grid>
</Window>

或者像这样:

<Window x:Class="Window15"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="Window15" Height="450" Width="800">
  <Window.DataContext>
    <local:Window15VM/>
  </Window.DataContext>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="auto"/>
      <RowDefinition/>
      <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Name="tb" HorizontalAlignment="Stretch" Height="19" Margin="5" TextWrapping="Wrap" Text="--" RenderTransformOrigin="3.98,0.09"/>
    <Button Grid.Column="1" Content="Open Excel Sheet" HorizontalAlignment="Right" Margin="5" Width="210" Command="{Binding Cmd}"/>
    <ScrollViewer Grid.Row="1" Grid.ColumnSpan="2" HorizontalScrollBarVisibility="Auto">
      <Grid>
        <Grid.LayoutTransform>
          <ScaleTransform ScaleX="{Binding ElementName=sl, Path=Value}"
                        ScaleY="{Binding ElementName=sl, Path=Value}"/>
        </Grid.LayoutTransform>
        <DataGrid Name="dg" ItemsSource="{Binding View}" HorizontalAlignment="Stretch" VerticalAlignment="Top" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" Margin="5"/>
      </Grid>
    </ScrollViewer>
    <Slider x:Name="sl" Grid.Row="2" Grid.Column="1" Value="1" Minimum=".3" Maximum="5"/>
  </Grid>
</Window>


这篇关于如何调整Datagrid绑定数据表(WPF)以显示更多列而不是放大文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆