BringIntoView不起作用

时间:2016-07-22 17:27:13

标签: c# wpf xaml code-behind

我在事件处理程序后面有这个代码:

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个对象的示例:

<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对象的颜色都设置正确,但程序显然没有尝试反对这行代码。

为您提供此Border对象所在位置的上下文:

\\ 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

1 个答案:

答案 0 :(得分:2)

BringIntoView(),根据MSDN

  

尝试将此元素置于其所包含的任何可滚动区域内。

因此,它不会选择标签项,展开扩展器等。您需要自己动手。

请注意,由于调度程序的优先级队列,一旦您进行更改(例如选择选项卡),内容可能就不可用。在这种情况下,您可能希望以较低优先级发布带入视图的请求:

var localBorderIndex = iBorder; // copy to avoid closure of loop variable
Dispatcher.InvokeAsync(() => borders[localBorderIndex].BringIntoView(),
    DispatcherPriority.Background);