滚动并拉伸 Expander 的内容
这就是我想要的:
1.) 当我单击“扩展器”按钮并且它展开时,它应该向下延伸到网格的末尾,
请参阅示例图像 =>
2.) 当我编写的文本多于 Expander 内的 RichTextBox 中的可用空间时,滚动条必须可见,并且我应该能够向下滚动。
在扩展器内容周围放置滚动查看器并不难,但即使设置为“自动”,它们也不会出现,这也没有帮助。将滚动查看器设置为“可见”我无法滚动,因为扩展器的内容会无休止地下降。
这是我的示例代码:
<Grid Margin="30,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<StackPanel Background="LightCoral" Orientation="Horizontal">
<TextBlock Grid.Column="0" Text="Incident type:" VerticalAlignment="Center" />
<ComboBox Grid.Column="1" IsEditable="True" Margin="0,7" Text="{Binding SelectedIncidentReport.IncidentType,Mode=TwoWay}" />
<TextBlock Grid.Column="0" Grid.Row="1" Text="Teachers name:" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="1" Height="25" Text="{Binding SelectedIncidentReport.TeacherName,Mode=TwoWay}" />
<TextBlock Grid.Column="0" Grid.Row="2" Text="Tutor group:" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="2" Margin="0,7" Text="{Binding SelectedIncidentReport.TutorGroup,Mode=TwoWay}" />
</StackPanel>
<Grid Background="LightBlue" Grid.Row="1" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Expander Background="Purple" Grid.Row="0" Height="Auto" Header="Report details" IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=1}">
<Controls:RichTextBox
VerticalScrollBarVisibility="Auto"
Text="{Binding SelectedIncidentReport.ReportDetails,UpdateSourceTrigger=LostFocus,IsAsync=True}"
AcceptsReturn="True"
AutoWordSelection="True"
AllowDrop="False"
SelectionBrush="#FFAC5BCB"
HorizontalScrollBarVisibility="Auto" />
</Expander>
<Expander Background="Red" Grid.Row="1" Header="Action taken" IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=2}">
<Controls:RichTextBox
VerticalScrollBarVisibility="Auto"
Text="{Binding SelectedIncidentReport.ActionTaken,UpdateSourceTrigger=LostFocus,IsAsync=True}"
AcceptsReturn="True"
AutoWordSelection="True"
AllowDrop="False"
SelectionBrush="#FFAC5BCB"
HorizontalScrollBarVisibility="Auto" />
</Expander>
<Expander Background="Lavender" Grid.Row="2" Header="Further action" IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=3}" >
<Controls:RichTextBox
VerticalScrollBarVisibility="Auto"
Text="{Binding SelectedIncidentReport.FurtherAction,UpdateSourceTrigger=LostFocus,IsAsync=True}"
AcceptsReturn="True"
AutoWordSelection="True"
AllowDrop="False"
SelectionBrush="#FFAC5BCB"
HorizontalScrollBarVisibility="Auto" />
</Expander>
<Expander Background="YellowGreen" Grid.Row="3" Header="Home contact" IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=4}" >
<Controls:RichTextBox
VerticalScrollBarVisibility="Auto"
Text="{Binding SelectedIncidentReport.HomeContact,UpdateSourceTrigger=LostFocus,IsAsync=True}"
AcceptsReturn="True"
AutoWordSelection="True"
AllowDrop="False"
SelectionBrush="#FFAC5BCB"
HorizontalScrollBarVisibility="Auto" />
</Expander>
</Grid>
<Grid Background="LawnGreen" Grid.Row="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Documents:" Grid.Column="0" />
<View:DocumentComboView DataContext="{Binding Path=SelectedIncidentReport.Documents}" Grid.Column="1" HorizontalAlignment="Stretch" />
</Grid>
</Grid>
This is what I want:
1.) When I click my Expander button and it expands it should stretch down to the end of the Grid
see sample image =>
2.) When I write more Text than space is available in the RichTextBox within the Expander ScrollBars must be visible and I should be able to scroll down.
Putting a scrollviewer around the Expander content ain't hard but it does not help even with setting on "Auto" they never appear. Set the scrollviewer on "Visible" I can't scroll because the content of the Expander goes down endlessly.
Thats my Sample code:
<Grid Margin="30,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<StackPanel Background="LightCoral" Orientation="Horizontal">
<TextBlock Grid.Column="0" Text="Incident type:" VerticalAlignment="Center" />
<ComboBox Grid.Column="1" IsEditable="True" Margin="0,7" Text="{Binding SelectedIncidentReport.IncidentType,Mode=TwoWay}" />
<TextBlock Grid.Column="0" Grid.Row="1" Text="Teachers name:" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="1" Height="25" Text="{Binding SelectedIncidentReport.TeacherName,Mode=TwoWay}" />
<TextBlock Grid.Column="0" Grid.Row="2" Text="Tutor group:" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="2" Margin="0,7" Text="{Binding SelectedIncidentReport.TutorGroup,Mode=TwoWay}" />
</StackPanel>
<Grid Background="LightBlue" Grid.Row="1" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Expander Background="Purple" Grid.Row="0" Height="Auto" Header="Report details" IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=1}">
<Controls:RichTextBox
VerticalScrollBarVisibility="Auto"
Text="{Binding SelectedIncidentReport.ReportDetails,UpdateSourceTrigger=LostFocus,IsAsync=True}"
AcceptsReturn="True"
AutoWordSelection="True"
AllowDrop="False"
SelectionBrush="#FFAC5BCB"
HorizontalScrollBarVisibility="Auto" />
</Expander>
<Expander Background="Red" Grid.Row="1" Header="Action taken" IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=2}">
<Controls:RichTextBox
VerticalScrollBarVisibility="Auto"
Text="{Binding SelectedIncidentReport.ActionTaken,UpdateSourceTrigger=LostFocus,IsAsync=True}"
AcceptsReturn="True"
AutoWordSelection="True"
AllowDrop="False"
SelectionBrush="#FFAC5BCB"
HorizontalScrollBarVisibility="Auto" />
</Expander>
<Expander Background="Lavender" Grid.Row="2" Header="Further action" IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=3}" >
<Controls:RichTextBox
VerticalScrollBarVisibility="Auto"
Text="{Binding SelectedIncidentReport.FurtherAction,UpdateSourceTrigger=LostFocus,IsAsync=True}"
AcceptsReturn="True"
AutoWordSelection="True"
AllowDrop="False"
SelectionBrush="#FFAC5BCB"
HorizontalScrollBarVisibility="Auto" />
</Expander>
<Expander Background="YellowGreen" Grid.Row="3" Header="Home contact" IsExpanded="{Binding SelectedExpander, Mode=TwoWay, Converter={StaticResource ExpanderToBooleanConverter}, ConverterParameter=4}" >
<Controls:RichTextBox
VerticalScrollBarVisibility="Auto"
Text="{Binding SelectedIncidentReport.HomeContact,UpdateSourceTrigger=LostFocus,IsAsync=True}"
AcceptsReturn="True"
AutoWordSelection="True"
AllowDrop="False"
SelectionBrush="#FFAC5BCB"
HorizontalScrollBarVisibility="Auto" />
</Expander>
</Grid>
<Grid Background="LawnGreen" Grid.Row="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Documents:" Grid.Column="0" />
<View:DocumentComboView DataContext="{Binding Path=SelectedIncidentReport.Documents}" Grid.Column="1" HorizontalAlignment="Stretch" />
</Grid>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在寻找的是自动和*大小的行的混合:折叠时自动,展开时*。有很多方法可以实现这种切换。一种简单的方法是通过转换器将行高绑定到扩展器。 XAML 看起来像这样:
这是转换器的一个简单版本:
现在,可用空间将在打开的扩展器之间分配,而折叠的扩展器将仅占用其标头所需的空间。如果文本对于展开的文本之一而言太长,ScrollViewer 应接管并开始滚动该 * 区域内的文本。
I think what you're looking for is a mix of Auto and * sized Rows: Auto when collapsed, * when expanded. There are a lot of ways you can achieve this switching. A simple one is to bind the row heights to the expanders through a converter. The XAML would look like this:
And here's a simple version of the converter:
Now the available space will be split between the open expanders and the collapsed ones will only take up as much as their header needs. If the text gets too long for one of the expanded ones the ScrollViewer should take over and start scrolling the text inside that * area.
我最近不得不做这样的事情。我使用了与您所拥有的代码非常相似的代码,但能够使用页面后面的代码来实现所需的结果。尝试这样的操作:
WPF
在后面的窗口代码中,我使用了 C# 并具有以下代码:
C#
在此例如,扩展器将全部增长以完全填充网格。如果您愿意,可以修改此设置以在选择一个扩展器时折叠其他扩展器。
I recently had to do something like this. I used very similar code to what you have but was able to achieve the desired result using the code behind the page. Try something like this:
WPF
In the window code behind I used C# and have this code:
C#
In this example the expanders will all grow to fill the grid completely. If you wanted to you could modify this to collapse the other expanders when one is selected.