我有一个ListBox,它将技能级别的集合显示为RadioButtons。我想强调技能水平(并在其旁边显示一个明星),这是学生之前的最佳成就。我将ListBox的DataContext设置为Skill对象,并将其ItemsSource绑定到SkillLevels集合。
技能
+ ClassSkillLevelID //所选技能等级
+ PreviousBestSkillLevelID
+技能等级
我能够使用ListBox的SelectedValue功能绑定ClassSkillLevelID以选择正确的RadioButton。但是,如何突出显示与PreviousBestSkillLevelID匹配的技能等级ID的RadioButton?
我尝试使用DependencyProperty创建一个ValueConverter,这样我就可以将DependencyProperty的值设置为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的一部分,因此编程解决方案可能更难。
答案 0 :(得分:0)
我离WPF太久了。我太难了。出于某种原因,我的印象是我只能使用带有单个Binding或MultiDataTrigger的DataTrigger。 This question让我重回正轨。我需要一个带有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();
}
}