我在WPF中有一个具有DataGrid的用户控件。 我在TabControl的TabItem中的一个Window中添加了这个用户控件。 垂直滚动条不可见。我调整了窗口的大小,看起来用户控件在考虑DataGrid的高度后才占据完整的高度。
如何使垂直滚动条出现并依旧根据用户控件和窗口高度可用的高度保持DataGrid的高度?
编辑: 主窗口:
<Window>
<Grid>
<TabControl Name="tc"
SelectionChanged="tc_SelectionChanged">
<TabItem Header="ABC">
<local:uc1 />
</TabItem>
<TabItem Header="DEF">
<local:uc2 />
</TabItem>
</TabControl>
</Grid>
</Window>
用户控制:
<UserControl>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0"
Grid.Column="0"
Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Button>Add</Button>
<Button>Remove</Button>
<Button>Refresh</Button>
</StackPanel>
<Label Name="lblTopMessage">some message</Label>
<DataGrid Name="dg"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"
Header="Name"
Width="*"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Value}"
Header="Value"
Width="130"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding List}"
Header="List"
Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
<Grid Grid.Row="0"
Grid.Column="1"
Name="gridTS">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Name="lblNewSource"
Grid.Row="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
Padding="0 5 0 0"
FontWeight="Bold">New Source</TextBlock>
<TextBlock Grid.Row="1"
Grid.Column="0"
Padding="10 10 0 0">Name:</TextBlock>
<TextBlock Grid.Row="2"
Grid.Column="0"
Padding="10 10 0 0">Value:</TextBlock>
<TextBlock Grid.Row="3"
Grid.Column="0"
Padding="10 10 0 0">List:</TextBlock>
<TextBox Grid.Row="1"
Grid.Column="1"
Name="txtName"
IsEnabled="False"></TextBox>
<ComboBox Grid.Row="2"
Grid.Column="1"
Name="cbValue"></ComboBox>
<ListBox Grid.Row="3"
Grid.Column="1"
Name="lbList">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Margin="3 5 0 3"
Content="{Binding Name}"
IsChecked="{Binding IsActive}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="btnSave"
Click="btnSave_Click"
Grid.Row="4"
Grid.Column="1">Save</Button>
<TextBlock TextWrapping="WrapWithOverflow"
Name="lblMessage"
Grid.Row="5"
Grid.Column="1"
Margin="0 10 10 0"></TextBlock>
</Grid>
</Grid>
</UserControl>
答案 0 :(得分:6)
<强>更新:强> 我刚从StackPanel中取出了DataGrid。试试这段代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="45.361"></RowDefinition>
<RowDefinition Height="102.639" />
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0"
Grid.Column="0"
Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Button>Add</Button>
<Button>Remove</Button>
<Button>Refresh</Button>
</StackPanel>
<Label Name="lblTopMessage">some message</Label>
</StackPanel>
<DataGrid Grid.Row="1"
Name="dg"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"
Header="Name"
Width="*"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Value}"
Header="Value"
Width="130"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding List}"
Header="List"
Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<Grid Grid.Row="0"
Grid.Column="1"
Name="gridTS"
Grid.RowSpan="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Name="lblNewSource"
Grid.Row="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
Padding="0 5 0 0"
FontWeight="Bold">New Source</TextBlock>
<TextBlock Grid.Row="1"
Grid.Column="0"
Padding="10 10 0 0">Name:</TextBlock>
<TextBlock Grid.Row="2"
Grid.Column="0"
Padding="10 10 0 0">Value:</TextBlock>
<TextBlock Grid.Row="3"
Grid.Column="0"
Padding="10 10 0 0">List:</TextBlock>
<TextBox Grid.Row="1"
Grid.Column="1"
Name="txtName"
IsEnabled="False"></TextBox>
<ComboBox Grid.Row="2"
Grid.Column="1"
Name="cbValue"></ComboBox>
<ListBox Grid.Row="3"
Grid.Column="1"
Name="lbList">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Margin="3 5 0 3"
Content="{Binding Name}"
IsChecked="{Binding IsActive}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="btnSave"
Grid.Row="4"
Grid.Column="1">Save</Button>
<TextBlock TextWrapping="WrapWithOverflow"
Name="lblMessage"
Grid.Row="5"
Grid.Column="1"
Margin="0 10 10 0"></TextBlock>
</Grid>
</Grid>
我希望有所帮助。
答案 1 :(得分:4)
使垂直滚动条出现并仍然保持DataGrid的高度自动 答案是不。
只要高度是自动的,它就会增长,因为自动意味着我得到了我想要的所有高度。
<RowDefinition Height="Auto"></RowDefinition>
如果您希望它使用可用空间,请使用*
<RowDefinition Height="*"></RowDefinition>
答案 2 :(得分:0)
将VerticalScrollBarVisibility设为Visible
。
<DaraGrid VerticalScrollBarVisibility="Visible" ... >
...
</DataGrid>
默认值为Auto
。
<强>更新强>
在查看我发现的代码后,滚动条visibility
不是您真正的问题。您的问题是 DataGrid在StackPanel中没有固定高度。它需要显示DataGrid
中所有项目所需的所有空间,这就是ScrollBar
不是Visible
的原因。正如我之前所说,VerticalScrollBarVisibility
的默认值为Auto
,这意味着:
<强> MSDN 强>: 自动 - 当视口无法显示所有内容时,将显示ScrollBar并将ScrollViewer的尺寸应用于内容。