我在UserControl中有DataGrid。 Datagrid绑定到ItemsSource,包含50个项目(50行)。 UserControl Width和Height设置为auto。如何在不指定DataGrid的高度和宽度的情况下启用ScrollBar?也许我想念一些东西,因为此时设计师告诉我我的DataGrid高度是5000(基于自动计算),这就是ScrollBar没有显示的原因。我希望我的DataGrid适合主窗口容器 - 具有星号的网格行。 我不想使用固定大小,因为我想在不同的屏幕分辨率上使用此视图。
<UserControl x:Class="Treasury7MD.Views.Form7MDTableHeadeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1" Style="{StaticResource FormGridBorderStyle}" Margin="20,0,0,20" >
<Grid.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Black"/>
</Style>
</Grid.Resources>
<DataGrid x:Name="dg" ItemsSource="{Binding KEKVs}" CanUserAddRows="False" AutoGenerateColumns="False" RowHeaderWidth="0" SelectionUnit="Cell" HorizontalContentAlignment="Stretch" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<DataGrid.Columns>
<DataGridTemplateColumn Header="1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="{Binding ActualWidth, ElementName=IndicatorTbl}" Text="{Binding Indicator, Mode=TwoWay}" TextWrapping="Wrap"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="{Binding ActualWidth, ElementName=KEKVTbl}" Text="{Binding Name, Mode=TwoWay}" TextWrapping="WrapWithOverflow"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="3">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="{Binding ActualWidth, ElementName=RowCodeTbl}" Text="{Binding RowCode, Mode=TwoWay}" TextWrapping="WrapWithOverflow"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="4">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<customControlls:NumericTextBox Style="{StaticResource NumericTextboxStyle}" Width="{Binding ActualWidth, ElementName=AccRecAtTheBeginingTbl}" Text="{Binding AccountsReceivable.AtTheBeginingOfTheYear, UpdateSourceTrigger=LostFocus}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="5">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<customControlls:NumericTextBox Style="{StaticResource NumericTextboxStyle}" Width="{Binding ActualWidth, ElementName=AccRecAtTheEndTbl}" Text="{Binding AccountsReceivable.AtTheEndOfTheReportingPeriod, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="6">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<customControlls:NumericTextBox Style="{StaticResource NumericTextboxStyle}" Width="{Binding ActualWidth, ElementName=AccRecOverdueTbl}" Text="{Binding AccountsReceivable.OverdueAtTheEndOfTheReportingPeriod, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="6">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<customControlls:NumericTextBox Style="{StaticResource NumericTextboxStyle}" Width="{Binding ActualWidth, ElementName=AccRecWrittenOffTbl}" Text="{Binding AccountsReceivable.WrittenOffSinceTheBeginningOfTheYear, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
这是UserControl所在的View
<Window x:Class="Treasury7MD.MainWindow"
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"
mc:Ignorable="d"
Title="MainWindow" WindowState="Maximized">
<Grid>
<Grid Name="g" >
<Grid.RowDefinitions>
<RowDefinition MaxHeight="20"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Name="x" Height="*" MinHeight="100"></RowDefinition>
</Grid.RowDefinitions>
<Button Content="Collapse" Click="Button_Click"></Button>
<views:OrganizationInfoView x:Name="z" Grid.Row="1"></views:OrganizationInfoView>
<ScrollViewer Grid.Row="2" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" >
<views:Form7MDTableHeadeView Grid.Row="2" ></views:Form7MDTableHeadeView>
</ScrollViewer>
</Grid>
</Grid>
答案 0 :(得分:0)
移除了ScrollViewer,其中包含UseControl和DataGrid的ScrollBar按预期工作。