簡體   English   中英

WPF CommandParameter RelativeSource綁定

[英]WPF CommandParameter RelativeSource Binding

我在ListView的DataTemple中有一個帶CheckBox的ListView。 向我展示了如何使Command正常工作。 我想捕獲ListView SelectedItem作為參數傳遞給Command,但是我不正確...

<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的另一種方法,然后在CheckBoxCommandParameter綁定如下

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

下面將傳遞與CheckBox相關的對象

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

在與CheckBox相關的Command Method ,可以將參數對象轉換為正確的類型( ListView ItemSource對象的類型)並獲取ValueActive的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM