WPF 命令参数相对源绑定



我在ListView的DataTemple中有一个带有复选框的列表视图。我被展示了如何使命令工作。我想捕获列表视图选定项以作为参数传递给命令,但我没有正确...

<ListView x:Name="lvReferralSource" ItemsSource="{Binding ReferralObsCollection}" Style="{StaticResource TypeListViewStyle}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid Width="200">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <CheckBox x:Name="ckbReferralIsChecked" Content="{Binding Value}" IsChecked="{Binding Active}" Style="{StaticResource CheckBoxStyleBase2}"
                                                  Command="{Binding DataContext.CheckBoxIsChecked, RelativeSource={RelativeSource AncestorType=ListView}}" 
                                                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=SelectedItem}">
                                        </CheckBox>
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

再看这个问题,我想我现在理解正确了。这是一种从ListView获取SelectedItem的不同方法然后在CheckBox中,我按如下方式绑定CommandParameter

CommandParameter="{Binding ElementName=lvReferralSource, Path=SelectedItem}"

以下内容将传递与CheckBox相关的对象

CommandParameter="{Binding}"// Full object from the ListView

在与CheckBox相关的Command Method中,您可以将参数 object 强制转换为正确的类型(ListView ItemSource 中对象的类型(,并获取 ValueActive

的值

最新更新