垂直滚动条未出现在 wpf 中用户控件内的数据网格中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16082580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vertical scrollbar not appearing in datagrid inside usercontrol in wpf
提问by Brij
I have a user control in WPF that has a DataGrid. I add this user control in a Window inside a TabItem of TabControl. The Vertical scrollbar is not getting visible. I resized the window and it seems the user control is taking its complete height after considering height of DataGrid.
我在 WPF 中有一个用户控件,它有一个 DataGrid。我将此用户控件添加到 TabControl 的 TabItem 内的窗口中。垂直滚动条不可见。我调整了窗口的大小,在考虑了 DataGrid 的高度后,用户控件似乎占据了它的完整高度。
How to make the vertical scrollbar appear and still keep the height of DataGrid to auto depending on the height available as per height of the user control and window?
如何根据用户控件和窗口的高度可用的高度使垂直滚动条出现并仍然保持 DataGrid 的高度为自动?
EDIT: Main Window:
编辑:主窗口:
<Window>
<Grid>
<TabControl Name="tc"
SelectionChanged="tc_SelectionChanged">
<TabItem Header="ABC">
<local:uc1 />
</TabItem>
<TabItem Header="DEF">
<local:uc2 />
</TabItem>
</TabControl>
</Grid>
</Window>
User Control:
用户控制:
<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>
回答by Dilshod
UPDATED:I just took DataGrid out of StackPanel. Try this code:
更新:我刚刚从 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>
I hope that helps.
我希望这有帮助。
回答by paparazzo
Make the vertical scrollbar appear and still keep the height of DataGrid auto.
The answer is NO.
使垂直滚动条出现并仍然保持 DataGrid 的高度自动。
答案是不。
As long as the height is auto it will grow as auto means I get all the height I want.
只要高度是自动的,它就会随着自动增长,这意味着我得到了我想要的所有高度。
<RowDefinition Height="Auto"></RowDefinition>
If you want it to use available space then use *
如果您希望它使用可用空间,请使用 *
<RowDefinition Height="*"></RowDefinition>
回答by Kapitán Mlíko
Set VerticalScrollBarVisibilityto Visible.
将VerticalScrollBarVisibility设置为Visible。
<DaraGrid VerticalScrollBarVisibility="Visible" ... >
...
</DataGrid>
Default value is Auto.
默认值为Auto。
UPDATE
更新
After looking into your code I discovered, that scrollbar visibilityis not your real problem. Your problem is DataGrid without fixed height in StackPanel. It takes all space necessary for all items in DataGridto be displayed and that's the reason why the ScrollBarwas not Visible. As I said earlier default value for VerticalScrollBarVisibilityis Autoand it means:
在查看你的代码后我发现,滚动条visibility不是你真正的问题。您的问题是DataGrid 在 StackPanel 中没有固定高度。它需要DataGrid显示所有项目所需的所有空间,这就是为什么ScrollBarwas not的原因Visible。正如我之前所说的,默认值为VerticalScrollBarVisibilityis Auto,这意味着:
MSDN: Auto - A ScrollBar appears and the dimension of the ScrollViewer is applied to the content when the viewport cannot display all of the content.
MSDN: Auto - 当视口无法显示所有内容时,会出现一个 ScrollBar 并将 ScrollViewer 的尺寸应用于内容。

