BringIntoView无法正常工作 [英] BringIntoView is not working
问题描述
我在事件处理程序后面有以下代码:
I have this code behind event handler:
private void comboActiveStudentAssignmentType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<Border> borders = new List<Border>();
// The list of border (focus rectangles) matches the combo of assignment types
borders.Add(borderBibleReadingMain);
borders.Add(borderBibleReadingClass1);
borders.Add(borderBibleReadingClass2);
borders.Add(borderMainHallStudent1);
borders.Add(borderMainHallAssistant1);
borders.Add(borderMainHallStudent2);
borders.Add(borderMainHallAssistant2);
borders.Add(borderMainHallStudent3);
borders.Add(borderMainHallAssistant3);
borders.Add(borderClass1Student1);
borders.Add(borderClass1Assistant1);
borders.Add(borderClass1Student2);
borders.Add(borderClass1Assistant2);
borders.Add(borderClass1Student3);
borders.Add(borderClass1Assistant3);
borders.Add(borderClass2Student1);
borders.Add(borderClass2Assistant1);
borders.Add(borderClass2Student2);
borders.Add(borderClass2Assistant2);
borders.Add(borderClass2Student3);
borders.Add(borderClass2Assistant3);
// Loop through the borders
for(int iBorder = 0; iBorder < borders.Count; iBorder++)
{
// Is this border the active student assignment?
if (comboActiveStudentAssignmentType.SelectedIndex == iBorder)
{
// Yes, so use a red brush for the background
borders[iBorder].BorderBrush = Brushes.Red;
// Now we must ensure the correct tab item is visible
if(iBorder >= 0 && iBorder <= 2)
{
expandTFGW.IsExpanded = true;
if (iBorder == 0)
tabTFGWReadingMainHall.IsSelected = true;
else if (iBorder == 1)
tabTFGWReadingClass1.IsSelected = true;
else if (iBorder == 2)
tabTFGWReadingClass2.IsSelected = true;
}
else if (iBorder >= 3 && iBorder <= 8)
{
expandAYFM.IsExpanded = true;
tabAYFMStudentsMainHall.IsSelected = true;
if (iBorder == 3 || iBorder == 4)
tabMainHallItem1.IsSelected = true;
else if (iBorder == 5 || iBorder == 6)
tabMainHallItem2.IsSelected = true;
else if (iBorder == 7 || iBorder == 8)
tabMainHallItem3.IsSelected = true;
}
else if (iBorder >= 9 && iBorder <= 14)
{
expandAYFM.IsExpanded = true;
tabAYFMStudentsClass1.IsSelected = true;
if (iBorder == 9 || iBorder == 10)
tabClass1Item1.IsSelected = true;
else if (iBorder == 11 || iBorder == 12)
tabClass1Item2.IsSelected = true;
else if (iBorder == 13 || iBorder == 14)
tabClass1Item3.IsSelected = true;
}
else if (iBorder >= 15)
{
expandAYFM.IsExpanded = true;
tabAYFMStudentsClass2.IsSelected = true;
if (iBorder == 15 || iBorder == 16)
tabClass2Item1.IsSelected = true;
else if (iBorder == 17 || iBorder == 18)
tabClass2Item2.IsSelected = true;
else if (iBorder == 19 || iBorder == 20)
tabClass2Item3.IsSelected = true;
}
borders[iBorder].BringIntoView();
}
else
{
// No, so set the background to transparent so we can't see it.
borders[iBorder].BorderBrush = Brushes.Transparent;
}
}
}
}
XAML中 Border
对象之一的示例:
An example of one of the Border
objects in XAML:
<Border x:Uid="borderMainHallStudent1" x:Name="borderMainHallStudent1" BorderThickness="5">
<Border.Style>
<Style x:Uid="Style_30" TargetType="Border">
<Setter x:Uid="Setter_76" Property="BorderBrush" Value="Transparent"/>
<Style.Triggers>
<DataTrigger x:Uid="DataTrigger_29" Binding="{Binding SelectedItem, ElementName=comboActiveStudentAssignmentType}"
Value="{x:Static StudentInfoEnums:StudentAssignmentType.Student1Main}">
<Setter x:Uid="Setter_77" Property="BorderBrush" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<StackPanel x:Uid="StackPanel_35">
<Label x:Uid="Label_38" Content="Student:"/>
<Grid x:Uid="Grid_15">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Uid="ColumnDefinition_34" Width="*"/>
<ColumnDefinition x:Uid="ColumnDefinition_35" Width="auto"/>
</Grid.ColumnDefinitions>
<TextBox x:Uid="textMainHallStudent1" x:Name="textMainHallStudent1"
Text="{Binding MainHallStudent1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="False" Grid.Column="0" Margin="2" />
<Button x:Uid="buttonMainHallStudent1" x:Name="buttonMainHallStudent1" Grid.Column="1" Background="Transparent"
DataContext="{Binding DataContext, ElementName=oclmEditor}"
Command="{Binding ApplicationCommand}"
CommandParameter="Student1Main">
<Image x:Uid="Image_17" Source="Images/AssignmentTypeStudent16.png" Margin="2"/>
</Button>
</Grid>
<Label x:Uid="Label_39" Content="Study:"/>
<ComboBox x:Uid="ComboBox_9" DataContext="{Binding DataContext, ElementName=oclmEditor}"
ItemsSource="{Binding StudentStudyPointsList}"
ItemContainerStyle="{StaticResource StudyPointComboBoxStyle}"
ItemTemplate="{StaticResource StudyPointComboItem}"
Validation.ErrorTemplate="{StaticResource StudyPointValidationTemplate}"
Tag="{Binding Meeting.MainHallStudent1, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding Meeting.MainHallStudent1StudyNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Number"/>
</StackPanel>
</Border>
此行代码: borders [iBorder] .BringIntoView();
似乎无效。我所有Border对象的颜色都已正确设置,但是程序显然没有尝试反对这一行代码。
This line of code: borders[iBorder].BringIntoView();
It does not seem to work. The colours of all my Border objects is set correctly, but the program evidently makes no attempt to object this line of code.
为您提供边界
对象存在:
\\ Main Window
\\ScrollView
\\ Expander
\\Tab Control
\\Tab Item
\\Border 1
\\Contents
\\Border 2
\\Contents
\\ Expander
\\Tab Control
\\Tab Item
\\Border 1
\\Contents
\\Border 2
\\Contents
推荐答案
BringIntoView()
,根据 MSDN :
尝试将该元素包含在其中包含的任何 scrollable 区域中。
因此它不会选择选项卡项目,展开扩展器等。您需要自己做。
Therefore it will not select tab items, expand expanders, etc. You need to do that yourself.
请注意,由于调度程序的优先级队列,您进行更改(例如选择选项卡)后,该内容可能不可用。在这种情况下,您可能想以较低的优先级发布视图请求:
Note that due to the dispatcher's priority queue, the content may not be available as soon as you make changes (such as select a tab). In that case, you may want to post the bring-into-view request in a lower priority:
var localBorderIndex = iBorder; // copy to avoid closure of loop variable
Dispatcher.InvokeAsync(() => borders[localBorderIndex].BringIntoView(),
DispatcherPriority.Background);
这篇关于BringIntoView无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!