Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4198

Get Item inside IitemControl which is another ItemControl

I have a question I have a simple item list in WPF:

    <ItemsControl ItemsSource="{Binding Details.ModuleFunStateInRack}" x:Name="ModuleFunStateInRackRoot">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <ItemsControl ItemsSource="{Binding ModuleFunState}" x:Name="ModuleFunStateRoot">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <StackPanel Orientation="Horizontal">
                                        <Label Content="Module Functional State Id: "/>
                                        <TextBlock VerticalAlignment="Center" >
                                            <TextBlock.Text>
                                                <Binding>
                                                    <Binding.Converter>
                                                        <helpers:ItemIndexConverter DataContext="{Binding Source={x:Reference ModuleFunStateRoot},Path=(ItemsControl.ItemsSource)}" />
                                                    </Binding.Converter>
                                                </Binding>
                                            </TextBlock.Text>
                                        </TextBlock>
                                    </StackPanel>>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

I have a helper here that numerates values in that and it looks like:

[ValueConversion(typeof(IList), typeof(int))]
public sealed class ItemIndexConverter : FrameworkContentElement, IValueConverter
{
    public Object Convert(object dataItem, Type dataType, object props, CultureInfo _ ) => ((IList)DataContext).IndexOf(dataItem);
    public Object ConvertBack(object obj, Type objectType, object props, CultureInfo _ ) => throw new NotImplementedException();
};

Problem is when I use this helper on inner ItemControl I am getting that he can not find element with name "ModuleFunStateRoot" (Name is there an I copied and pass it to make sure to reference markup), But when I use this on outer(ModuleFunStateInRackRoot) ItemControl it works perfectly. What could be the problem?

Upvotes: 0

Views: 43

Answers (1)

mm8
mm8

Reputation: 169190

You could use a <MultiBinding> and an IMultiValueConverter and bind to both the ItemsSource and the current item:

public sealed class ItemIndexConverter : IMultiValueConverter
{
    public object Convert(object[] dataItems, Type targetType, object props, CultureInfo _) => 
        ((IList)dataItems[1]).IndexOf(dataItems[0]).ToString();

    public object[] ConvertBack(object value, Type[] targetTypes, object props, CultureInfo _) =>
        throw new NotImplementedException();
}

XAML:

<TextBlock VerticalAlignment="Center" >
    <TextBlock.Text>
        <MultiBinding>
            <MultiBinding.Converter>
                <helpers:ItemIndexConverter />
            </MultiBinding.Converter>
            <Binding />
            <Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=ItemsControl}" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

Upvotes: 1

Related Questions