将焦点矩形设置为红色/透明并确保可见

时间:2016-07-21 13:24:07

标签: c# wpf xaml

我有一个相关的问题,提出了答案here,但我的问题更复杂,我认为最好开始一个新问题。我希望没关系。

我认为在视觉上向您展示我首先要做的事情非常重要。以下是编辑的概述:

Overview of the editor

左下方有几个控件,用于显示所选学生(以及适用的助手)。编辑为学生提供3个班级。因此,我让每个班级的学生都在TabItem控制。

示例:

Sample section

如您所见,它有一个TabControl有3个项目(主要大厅,1级和2级)。在每个标签项上,他们都有自己的TabControl和3个学生项目。

你看到文本框旁边的那些小图标?如果单击这些,则会影响编辑器右侧的组合:

Assignments

因此,当您单击一个小图标时,它会自动选择右侧组合中的右侧条目。然后,当您从网格中选择一个学生时,它将被填充到编辑器的右侧:

Selecting a student

问题

当用户手动更改右侧的组合时,则需要在左侧正确设置焦点矩形。因此,当选择一个项目时,它需要:

  1. 将标签项设置为选中(如果需要)。
  2. 将矩形的边框画笔设置为红色而不是透明。
  3. 我已根据需要设置矩形颜色,但我还没有设法使用行为(根据相关问题)。另外,当我尝试摆弄“TabItem”的IsSelected属性时。对象我发现它们有时会在屏幕上崩溃。我已经注释掉了我的代码。

    我很抱歉这个更有问题的问题,但我会被要求澄清,所以我想尽力掩盖它。

    所以:

    有两个主要TabControl个对象:

    首先是圣经阅读项目:

    <TabControl Grid.Row="1" Grid.ColumnSpan="2">
        <TabItem x:Name="tabTFGWReadingMainHall" Header="Main Hall" DataContext="{Binding Meeting}">
            <TabItem.Style>
                <Style TargetType="{x:Type TabItem}">
                    <Setter Property="IsEnabled" Value="False"/>
                    <!--<Setter Property="IsSelected" Value="False"/>-->
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding NumberClasses, ConverterParameter=1, Converter={StaticResource IsEqualOrGreaterThanConverter}}" Value="True">
                            <Setter Property="IsEnabled" Value="True" />
                        </DataTrigger>
                        <!--<DataTrigger Binding="{Binding SelectedItem, ElementName=comboActiveStudentAssignmentType}" Value="{x:Static StudentInfoEnums:StudentAssignmentType.BibleReadingMain}">
                            <Setter Property="IsSelected" Value="True"/>
                        </DataTrigger>-->
                    </Style.Triggers>
                </Style>
            </TabItem.Style>
            <Border x:Name="borderBibleReadingMain" BorderThickness="5">
                <Border.Style>
                    <Style TargetType="Border">
                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedItem, ElementName=comboActiveStudentAssignmentType}" Value="{x:Static StudentInfoEnums:StudentAssignmentType.BibleReadingMain}">
                                <Setter Property="BorderBrush" Value="Red"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <StackPanel>
                    <StackPanel.Style>
                        <Style TargetType="{x:Type StackPanel}">
                            <Setter Property="IsEnabled" Value="False"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding NumberClasses, ConverterParameter=1, Converter={StaticResource IsEqualOrGreaterThanConverter}}" Value="True">
                                    <Setter Property="IsEnabled" Value="True" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <Label Content="Student:"/>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBox x:Name="textBibleReadingMain" Grid.Column="0" Margin="2" IsEnabled="False"
                            Text="{Binding BibleReadingMainName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
                        <Button x:Name="buttonBibleReadingMain" Grid.Column="1" Background="Transparent"
                            DataContext="{Binding DataContext, ElementName=oclmEditor}"
                                Command="{Binding ApplicationCommand}" CommandParameter="BibleReadingMain">
                            <Image Source="Images/AssignmentTypeBibleReading16.png" Margin="2"/>
                        </Button>
                    </Grid>
                    <Label Content="Study:"/>
    
                    <ComboBox DataContext="{Binding DataContext, ElementName=oclmEditor}"
                          ItemsSource="{Binding ReadingStudyPointsList}"
                          ItemContainerStyle="{StaticResource StudyPointComboBoxStyle}"
                          ItemTemplate="{StaticResource StudyPointComboItem}"
                          Validation.ErrorTemplate="{StaticResource StudyPointValidationTemplate}"
                          Tag="{Binding Meeting.BibleReadingMainName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                          SelectedValue="{Binding Meeting.BibleReadingMainStudyPoint, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          SelectedValuePath="Number">
                        <!--<ComboBox.Style>
                        <Style 
                            TargetType="ComboBox" 
                            BasedOn="{StaticResource {x:Type ComboBox}}">
                            <Style.Triggers>
                                <Trigger Property="Validation.HasError" Value="True">
                                    <Setter 
                                        Property="ToolTip"
                                        Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ComboBox.Style>
                    <ComboBox.SelectedValue>
                        <Binding Path="Meeting.BibleReadingMainStudyPoint"
                             Mode="TwoWay"
                             UpdateSourceTrigger="PropertyChanged"
                             NotifyOnValidationError="True">
                            <Binding.ValidationRules>
                                <ValidationRules:StudyPointValidationRule BibleReading="True"/>
                            </Binding.ValidationRules>
                        </Binding>
                    </ComboBox.SelectedValue>-->
                    </ComboBox>
                </StackPanel>
            </Border>
        </TabItem>
        <TabItem x:Name="tabTFGWReadingClass1" Header="Class 1" DataContext="{Binding Meeting}">
            <TabItem.Style>
                <Style TargetType="{x:Type TabItem}">
                    <Setter Property="IsEnabled" Value="False"/>
                    <Setter Property="IsSelected" Value="False"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding NumberClasses, ConverterParameter=2, Converter={StaticResource IsEqualOrGreaterThanConverter}}" Value="True">
                            <Setter Property="IsEnabled" Value="True" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding SelectedItem, ElementName=comboActiveStudentAssignmentType}" Value="{x:Static StudentInfoEnums:StudentAssignmentType.BibleReadingClass1}">
                            <Setter Property="IsSelected" Value="True"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TabItem.Style>
            <Border x:Name="borderBibleReadingClass1" BorderThickness="5">
                <Border.Style>
                    <Style TargetType="Border">
                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedItem, ElementName=comboActiveStudentAssignmentType}" Value="{x:Static StudentInfoEnums:StudentAssignmentType.BibleReadingClass1}">
                                <Setter Property="BorderBrush" Value="Red"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <StackPanel>
                    <StackPanel.Style>
                        <Style TargetType="{x:Type StackPanel}">
                            <Setter Property="IsEnabled" Value="False"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding NumberClasses, ConverterParameter=2, Converter={StaticResource IsEqualOrGreaterThanConverter}}" Value="True">
                                    <Setter Property="IsEnabled" Value="True" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <Label Content="Student:"/>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBox x:Name="textBibleReadingClass1" Grid.Column="0" Margin="2" IsEnabled="False"
                        Text="{Binding BibleReadingClass1Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
                        <Button x:Name="buttonBibleReadingClass1" Grid.Column="1" Background="Transparent"
                        DataContext="{Binding DataContext, ElementName=oclmEditor}"
                            Command="{Binding ApplicationCommand}" CommandParameter="BibleReadingClass1">
                            <Image Source="Images/AssignmentTypeBibleReading16.png" Margin="2"/>
                        </Button>
                    </Grid>
                    <Label Content="Study:"/>
                    <ComboBox DataContext="{Binding DataContext, ElementName=oclmEditor}"
                          ItemsSource="{Binding ReadingStudyPointsList}"
                          ItemContainerStyle="{StaticResource StudyPointComboBoxStyle}"
                          ItemTemplate="{StaticResource StudyPointComboItem}"
                          Tag="{Binding Meeting.BibleReadingClass1Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                          SelectedValue="{Binding Meeting.BibleReadingClass1StudyPoint, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          SelectedValuePath="Number"/>
                </StackPanel>
            </Border>
        </TabItem>
        <TabItem x:Name="tabTFGWReadingClass2" Header="Class 2" DataContext="{Binding Meeting}">
            <TabItem.Style>
                <Style TargetType="{x:Type TabItem}">
                    <Setter Property="IsEnabled" Value="False"/>
                    <Setter Property="IsSelected" Value="False"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding NumberClasses, ConverterParameter=3, Converter={StaticResource IsEqualOrGreaterThanConverter}}" Value="True">
                            <Setter Property="IsEnabled" Value="True" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding SelectedItem, ElementName=comboActiveStudentAssignmentType}" Value="{x:Static StudentInfoEnums:StudentAssignmentType.BibleReadingClass2}">
                            <Setter Property="IsSelected" Value="True"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TabItem.Style>
            <Border x:Name="borderBibleReadingClass2" BorderThickness="5">
                <Border.Style>
                    <Style TargetType="Border">
                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedItem, ElementName=comboActiveStudentAssignmentType}" Value="{x:Static StudentInfoEnums:StudentAssignmentType.BibleReadingClass2}">
                                <Setter Property="BorderBrush" Value="Red"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <StackPanel>
                    <StackPanel.Style>
                        <Style TargetType="{x:Type StackPanel}">
                            <Setter Property="IsEnabled" Value="False"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding NumberClasses, ConverterParameter=3, Converter={StaticResource IsEqualOrGreaterThanConverter}}" Value="True">
                                    <Setter Property="IsEnabled" Value="True" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <Label Content="Student:"/>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBox x:Name="textBibleReadingClass2" Grid.Column="0" Margin="2" IsEnabled="False"
                    Text="{Binding BibleReadingClass2Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
                        <Button x:Name="buttonBibleReadingClass2" Grid.Column="1" Background="Transparent"
                    DataContext="{Binding DataContext, ElementName=oclmEditor}"
                        Command="{Binding ApplicationCommand}" CommandParameter="BibleReadingClass2">
                            <Image Source="Images/AssignmentTypeBibleReading16.png" Margin="2"/>
                        </Button>
                    </Grid>
                    <Label Content="Study:"/>
                    <ComboBox DataContext="{Binding DataContext, ElementName=oclmEditor}"
                          ItemsSource="{Binding ReadingStudyPointsList}"
                          ItemContainerStyle="{StaticResource StudyPointComboBoxStyle}"
                          ItemTemplate="{StaticResource StudyPointComboItem}"
                          Tag="{Binding Meeting.BibleReadingClass2Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                          SelectedValue="{Binding Meeting.BibleReadingClass2StudyPoint, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          SelectedValuePath="Number"/>
                </StackPanel>
            </Border>
        </TabItem>
    </TabControl>
    

    第二个是tabControlAYFMStudents控件,但我必须限制我的问题。所以我没有表现出来。

    我不希望任何人阅读我的所有代码。所以如果你认为我应该删除这个问题那么请告诉我。或者如果你认为我可以在其他地方问它。

    我希望能够根据所选的当前组合项将矩形边框设置为透明(或红色)。并且,确保它是可见的。

    唷!谢谢。

1 个答案:

答案 0 :(得分:0)

我决定在晚上使用背后的代码对我的问题是最简单的解决方案。我为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; } } } 事件添加了一个处理程序:

Border

}

这很好用。当您更改组合时,正确的TabItem设置为红色,并且相关联的Selected设置为Expander

此外,我将关联的borders[iBorder].BringIntoView(); 设置为已展开。这意味着我95%在我想要的地方。我可以从XAML中删除所有DataTriggers,因为它现在都在这一个处理程序中进行管理。

但是,此调用似乎不起作用:

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

所以即使上面会设置边框属性并确保选中它的标签项,但我无法确保控件在屏幕外是可见的。

更新

这个answer解释了如何将矩形变为视图:

{{1}}