itsmatt
itsmatt

Reputation: 31416

WPF ListBox Binding ItemsSource

In a related question I asked about binding to a particular element of an array indexed by another property. The answer provided worked really well for the sample code example provided.

Where I'm running into trouble is that I specify an ItemSource for a ListBox and I get a DependencyProperty.UnsetValue in my converter when I step through it. No doubt this is a problem with my understanding of Binding.

My ListBox looks like so:

<ListBox ItemsSource="{Binding Path=MyList}">
    <ListBox.Resources>
        <local:FoodIndexConverter x:Key="indexConverter" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding Converter="{StaticResource indexConverter}">
                        <Binding Path="MyIndex" />
                        <Binding Path="Fields" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

and the code behind looks as so:

public MainWindow()
{
    InitializeComponent();
    MyList.Add(new SomeData() { Fields = new object[] {"liver", "onions", "cake" } } );
    MyList.Add(new SomeData() { Fields = new object[] {"liver", "onions", "candy" } } );
    MyList.Add(new SomeData() { Fields = new object[] {"liver", "onions", "pie" } } );

    DataContext = this;
}

MyList is a List.

MyIndex is an int.

The converter code is

    public class FoodIndexConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values == null || values.Length != 2)
                return null;

            int? idx = values[0] as int?;
            object[] food = values[1] as object[];

            if (!idx.HasValue || food == null)
                return null;

            return food[idx.Value];
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

When I step through the debugger in the converter code the MyIndex (value[0]) is DependencyProperty.UnsetValue - the object array is as I'd expect.

I'm assuming it is a Binding issue with: in that it doesn't know what MyIndex is.

If MyIndex were a property of the SomeData class, it works as I would expect but it's not, it's a property of the class MainWindow, just like MyList.

How do I specify that I'm interested in the MyIndex property that is part of my DataContext and not the MyData List?

Upvotes: 0

Views: 5823

Answers (1)

Rachel
Rachel

Reputation: 132618

It is looking for the MyIndex property on the ListBoxItem (in this case, a SomeData class) and that property doesn't exist.

Set the ElementName in your binding to the window name to get it to look for the property elsewhere.

<Binding ElementName=RootWindow, Path=DataContext.MyIndex />

Upvotes: 1

Related Questions