I have a scenario where I have a Dictionary(Of TextObject, ObservableCollection(Of TextObject), a Parent / Children structure. TextObject is a simple class that exposes a Text property.I also have two Listboxes. One Listbox is bound to the Dictionary Keys and the other is bound to the ObservableCollection associated with the selected Key in the first Listbox. If a Parent has no children (the ObservableCollection.Count = 0) then I would like to change the background of the ListBoxItem to red. I cannot get the Datatrigger binding to work. I can certainly accomplish this with code but I would rather use the Datatrigger if I can. Any help would be appreciated.
Below is the code behind and XAML for a sample application.
XAML
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"><Window.Resources><DataTemplate x:Key="TextBlockTemplate"><TextBlock Text="{Binding Text}" /></DataTemplate><Style x:Key="TestStyle"><Setter Property="Control.Background" Value="LightBlue"/><Style.Triggers><DataTrigger Binding="{Binding Acronyms.Item[DataContext].Count}" Value="0"><Setter Property="Control.Background" Value="Red"/></DataTrigger></Style.Triggers></Style></Window.Resources><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><ListBox Name="lsbParents" Height="200" Grid.Column="0" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemTemplate="{StaticResource TextBlockTemplate}" SelectionChanged="lsbParents_SelectionChanged" ItemContainerStyle="{StaticResource TestStyle}" ItemsSource="{Binding Path=Keys}" /><ListBox Name="lsbChildren" Height="200" Grid.Column="1" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemTemplate="{StaticResource TextBlockTemplate}" /></Grid></Window>
Visual Basic
Imports System.Collections.ObjectModel Imports System.ComponentModel Class MainWindow Dim _Dict1 As Dictionary(Of TextObject, ObservableCollection(Of TextObject)) _ = New Dictionary(Of TextObject, ObservableCollection(Of TextObject))(New TextObject.EqualityCompare) Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. CreateData() Me.DataContext = Dict1 End Sub Public ReadOnly Property Dict1() As Dictionary(Of TextObject, ObservableCollection(Of TextObject)) Get Return _Dict1 End Get End Property Private Sub CreateData() Dim Text1 As TextObject Dim TextCol As ObservableCollection(Of TextObject) For i As Integer = 1 To 5 Text1 = New TextObject("Parent " + i.ToString) TextCol = New ObservableCollection(Of TextObject) For j As Integer = 1 To 5 - i TextCol.Add(New TextObject("Child " + j.ToString)) Next _Dict1.Add(Text1, TextCol) Next End Sub Private Sub lsbParents_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) lsbChildren.ItemsSource = _Dict1(CType(e.AddedItems(0), TextObject)) End Sub End Class Public Class TextObject Implements INotifyPropertyChanged Public Sub New(value As String) _Text = value End Sub Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Protected Sub OnPropertyChanged(ByVal Value As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Value)) End Sub Private _Text As String = "" Public Property Text() As String Get Return _Text End Get Set(ByVal value As String) _Text = value OnPropertyChanged("Text") End Set End Property Public Class EqualityCompare Implements IEqualityComparer(Of TextObject) Public Shadows Function Equals(x As TextObject, y As TextObject) As Boolean Implements IEqualityComparer(Of TextObject).Equals Return x.Text = y.Text End Function Public Shadows Function GetHashCode(obj As TextObject) As Integer Implements IEqualityComparer(Of TextObject).GetHashCode Return obj.Text.GetHashCode End Function End Class End Class