繁体   English   中英

如何将ListItem绑定到CommandParameter

[英]How do I bind ListItem to CommandParameter

我有一个ListBox

<ListBox Margin="10" Padding="10" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!-- Data template -->
            <Grid>
                <Grid.ColumnDefinitions>
                    <!--Image-->
                    <ColumnDefinition Width="auto" />
                    <!--Info-->
                    <ColumnDefinition Width="500" />
                    <!--Options-->
                    <ColumnDefinition Width="*" /
                </Grid.ColumnDefinitions>
                <Image Height="50" Width="50" />
                <StackPanel Grid.Column="1" HorizontalAlignment="Left">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Name: " />
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="ID: " />
                        <TextBlock Text="{Binding ID}" />
                    </StackPanel>
                </StackPanel>
                <Button Grid.Column="2" Content="{Binding Status}" Command="{Binding CompleteCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainPageViewModel}}}" CommandParameter="{Binding}" HorizontalAlignment="Right" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我需要CommandParameter作为此ListBoxListItem

AncestorType设置为ListBox并在路径中包括DataContext

<Button ... Command="{Binding DataContext.CompleteCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}" CommandParameter="{Binding}" HorizontalAlignment="Right"></Button>

然后,如果CompleteCommand属性与Items属性属于同一类,则Command绑定应该起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM