确定ListItem DataContext ID是否与父级';s的DataContext以突出显示该项

本文关键字:DataContext 显示 是否 ID ListItem 确定 | 更新日期: 2023-09-27 18:21:57

我有一个ListBox,它将技能级别的集合显示为单选按钮。我想强调一个学生之前取得的最好成绩的技能水平(并在旁边显示一颗星)。我将ListBox的DataContext设置为Skill对象,并将其ItemsSource绑定到SkillLevels集合。

技能
+ClassSkillLevelID//所选技能等级
+PreviousBestSkillLevelID
+技能水平

我可以使用ListBox的SelectedValue功能绑定ClassSkillLevelID来选择正确的RadioButton。但是,如何使用与PreviousBestSkillLevelID匹配的技能级别ID突出显示RadioButton?

我尝试用DependencyProperty创建一个ValueConverter,这样我就可以将DependencyProperties的值设置为PreviousBestSkillLevelID,并将每个RadioButton的ID与之进行比较。但我认为转换器不在Visual Tree中,因此我无法访问父对象以访问PreviousBestSkillLevelID:

    <ListBox x:Name="lbSkillLevels"
            ItemsSource="{Binding Path=SkillLevels}" 
            SelectedValue="{Binding Path=ClassSkillLevelID, Converter={StaticResource nullEmptyConverter},  ConverterParameter=System.Guid}"
            SelectedValuePath="ID">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    ...
                </Grid.ColumnDefinitions>
                <RadioButton IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem, AncestorLevel=1}, Path=IsSelected}">
                    <RadioButton.Style>
                        <Style TargetType="RadioButton">
                            <Style.Triggers>
                                <DataTrigger Value="True">
                                    <DataTrigger.Binding>
                                        <Binding Path="ID">
                                            <Binding.Converter>
                                                <local:SkillIDMatchConverter ValueToMatch="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox, AncestorLevel=1}, Path=DataContext.PreviousBestSkillLevelID}"/>
                                            </Binding.Converter>
                                        </Binding>
                                    </DataTrigger.Binding>
                                    <DataTrigger.Setters>
                                        <Setter Property="Effect">
                                            <Setter.Value>
                                                <DropShadowEffect BlurRadius="4" Color="Yellow" Direction="0" ShadowDepth="0" />
                                            </Setter.Value>
                                        </Setter>
                                    </DataTrigger.Setters>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </RadioButton.Style>
                </RadioButton>
                <TextBlock Grid.Column="1" Text="{Binding Path=Name}">
                </TextBlock>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

此ListBox是父ItemsControl的DataTemplate的一部分,因此编程解决方案可能更困难。

确定ListItem DataContext ID是否与父级';s的DataContext以突出显示该项

我离开WPF太久了。我太难了。出于某种原因,我在工作时的印象是,我只能使用带有单个绑定或多数据触发器的DataTrigger。这个问题让我回到了正轨。我需要一个带有MultiBinding的DataTrigger来拉入这两个值,然后用一个转换器在它们匹配时将它们转换为true:
<RadioButton IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem, AncestorLevel=1}, Path=IsSelected}" VerticalAlignment="Center" Margin="3,0,3,3">
<RadioButton.Style>
    <Style TargetType="RadioButton">
        <Style.Triggers>
            <DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource guidMatchConverter}">
                        <Binding Path="ID" />
                        <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=ListBox, AncestorLevel=1}" Path="DataContext.PreviousBestSkillLevelID" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <DataTrigger.Setters>
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect BlurRadius="4" Color="Yellow" Direction="0" ShadowDepth="0" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger.Setters>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</RadioButton.Style>

转换器是这样的:

public class GuidMatchConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == null || values.Length < 2) 
            return false;
        else
            return (Guid)values[0] == (Guid)values[1];
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}