kleineg
kleineg

Reputation: 491

Why is the binding failing within a ItemsControl

I created an Index property on the object in the list the ItemsControl's ItemsSource is bound to, but when I put a breakpoint in my converter I see the value for that binding is DependancyProperty.UnsetValue. The data context for the ContentPresenter is that object, there is a property on the object, why doesn't it see the Index property?

<ItemsControl ItemsSource="{Binding Charts}" x:Name="ItemsControl">
    <ItemsControl.ItemTemplate>
        <ItemContainerTemplate >
            <ContentPresenter Content="{Binding}">
                <ContentPresenter.Visibility>
                    <MultiBinding Converter="{StaticResource Converter}">
                        <Binding Path="Index"/>
                        <Binding Path="WhichAreVisible" />
                    </MultiBinding>
                </ContentPresenter.Visibility>
            </ContentPresenter>
        </ItemContainerTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 0

Views: 320

Answers (1)

ItemTemplate is the wrong place to do that. That's going to be a DataTemplate or ItemContainerTemplate used to display the content of the items in the ItemsControl, within some other child container control -- one per child. That other container control is what you want to hide, not just the content within it. Of course if it's auto-sized with no padding or margin, it won't take up any space once the content collapses, but then you're counting on that remaining the case.

Try this and see what you get; ItemContainerStyle controls the style of the actual child item. In a ListBox, it would be applied to the ListBoxItem type; in an ItemsControl, the children are ContentPresenters. If you already have an ItemContainerStyle, just add this trigger to it.

<ItemsControl.ItemContainerStyle>
    <Style TargetType="ContentPresenter">
        <Setter Property="Visibility">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource Converter}">
                    <Binding Path="Index"/>
                    <Binding Path="WhichAreVisible" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.ItemContainerStyle>

Upvotes: 2

Related Questions