WPF/개발

WPF ItemsControl Item Index Binding 방법

푸코잇 2024. 1. 12. 00:10
728x90

ItemsControl를 사용하다 보면 CommandParameter로 Item Index를 넘겨야 되는 경우가 있다.

ItemsControl의 Item Index 바인딩 방법에 대해 배워보자.

 

<ItemsControl ItemsSource="{Binding ItemCollection}" AlternationCount="{Binding ItemCollection.Count}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="15" VerticalAlignment="Center">
                <TextBlock Text="{Binding Name}" Margin="0,0,10,0"/>
                <Button Command="{Binding DataContext.ButtonClickCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
                                    CommandParameter="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=TemplatedParent}}">
                    <Image Source="{dx:DXImage Image=Refresh_32x32.png}" Height="20" Stretch="Uniform"/>
                </Button>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

 

ItemsControl의 AlternationCount에 ItemsSource 컬렉션의 Count를 바인딩해줘야 한다.

그리고 CommandParamter에 ItemsControl.AlternationIndex를 바인딩해줘야 한다.

 

  • 실행결과