ここでそれを行うための別の方法があります
別の解決策は、コンバータを使用することができます。あなたはa behavior that applies to WPF container controls like Grid
, StackPanel
, etc., and gives each of the children an attached ChildIndex
propertyで始まります。次にItemContainerStyle
のListBox
にDataTrigger
を追加し、MultiBinding
と多値コンバータを使用して、ListBoxItem
のChildIndex
とビューモデルのHighlightIndex
プロパティをそれぞれ比較します。
それは(本当に、あなたはListBox
の上位クラスItemsControl
にそれを適用したいと思います)ListBox
自体に適用される行動のために、より便利になりますが、それはあるとして、あなたはその事にそれを使用することができますリストボックスにはありません。
XAML:
<ListBox
ItemsSource="{Binding Items}"
>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel
hbo:PanelBehaviors.IsChildPositionIndicated="True"
Orientation="Vertical"
/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style
TargetType="ListBoxItem"
BasedOn="{StaticResource {x:Type ListBoxItem}}"
>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding
Converter="{StaticResource EqualsConverter}">
<Binding
Path="DataContext.HighlightIndex"
RelativeSource="{RelativeSource AncestorType=ListBox}"
/>
<Binding
Path="(hbo:PanelBehaviors.ChildIndex)"
RelativeSource="{RelativeSource Self}"
/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
行動:
using System.Windows;
using System.Windows.Controls;
namespace HollowEarth.Behaviors
{
public static class PanelBehaviors
{
public static void UpdateChildIndexProperties(Panel panel)
{
for (int i = 0; i < panel.Children.Count; ++i)
{
var child = panel.Children[i];
SetChildIndex(child, i);
}
}
#region PanelBehaviors.IsChildPositionIndicated Attached Property
public static bool GetIsChildPositionIndicated(Panel panel)
{
return (bool)panel.GetValue(IsChildPositionIndicatedProperty);
}
public static void SetIsChildPositionIndicated(Panel panel, bool value)
{
panel.SetValue(IsChildPositionIndicatedProperty, value);
}
/// <summary>
/// Behavior which causes the Panel to identify its first and last children with attached properties.
/// </summary>
public static readonly DependencyProperty IsChildPositionIndicatedProperty =
DependencyProperty.RegisterAttached("IsChildPositionIndicated", typeof(bool), typeof(PanelBehaviors),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsArrange, IsChildPositionIndicated_PropertyChanged));
private static void IsChildPositionIndicated_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Panel panel = (Panel)d;
((Panel)d).LayoutUpdated += (s, e2) => UpdateChildIndexProperties(panel);
}
#endregion PanelBehaviors.IsChildPositionIndicated Attached Property
#region PanelBehaviors.ChildIndex Attached Property
public static int GetChildIndex(UIElement obj)
{
return (int)obj.GetValue(ChildIndexProperty);
}
public static void SetChildIndex(UIElement obj, int value)
{
obj.SetValue(ChildIndexProperty, value);
}
public static readonly DependencyProperty ChildIndexProperty =
DependencyProperty.RegisterAttached("ChildIndex", typeof(int), typeof(PanelBehaviors),
new FrameworkPropertyMetadata(-1,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsParentArrange));
#endregion PanelBehaviors.ChildIndex Attached Property
}
}
コンバータ:ObservableCollection<String> Items
、int HighlightIndex { get; set; }
、INotifyPropertyChanged
と:
public class EqualsConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return Object.Equals(values[0], values[1]);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
のviewmodelはちょうど急ごしらえです。
ItemCountが過剰でない場合は、コンバータで 'AlternationIndex'を使用できます。 AlternationIndexの使用例は、[here](http:// stackoverflow。com/a/6512032/302677) – Rachel
@Rachel、ご返信ありがとうございます。私はそれに落とし穴がなかった「クリーナー」なアプローチを望んでいました。 – Cleve
'CurrentIndex'を'ItemCollection [CurrentIndex]'を返す 'CurrentItem'に変更し、' IMultiValueConverter'を使って 'CurrentItem == Value'を参照するようにします。両方の値をConverterに渡すのに十分なはずです。あなたがそれを必要とすれば、私はおそらく何らかの例を見つけることができます。 – Rachel