首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF CommandParameter RelativeSource绑定

WPF CommandParameter RelativeSource绑定
EN

Stack Overflow用户
提问于 2019-06-04 06:34:28
回答 1查看 628关注 0票数 0

我在ListView的DataTemple中有一个带有CheckBox的ListView。他们向我展示了如何使命令工作。我想要捕获ListView SelectedItem作为参数传递给命令,但我没有这样做……

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-04 14:34:50

再看一遍这个问题,我想我现在理解正确了。下面是从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的值

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56435313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档