简体   繁体   English

将CheckBox绑定到ListBox中的SelectedItem

[英]Bind CheckBox to SelectedItem in ListBox

I have created a ListBox in WPF, which does the following: 我已经在WPF中创建了一个列表框,它可以执行以下操作:

It binds to a DataSource in my ViewModel. 它绑定到我的ViewModel中的数据源。 The ListBox contains Items and next to each item is a CheckBox. ListBox包含项目,每个项目旁边是一个CheckBox。 If the user clicks on a CheckBox, the Item is being selected and the Command will be executed. 如果用户单击CheckBox,则选中该项目,然后将执行命令。 It works fine and the code for WPF looks like this: 它工作正常,WPF的代码如下所示:

    <ListBox ItemsSource="{Binding OCAntwort}" SelectedItem="{Binding SelectedAntwort}" >
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Grid>
                                <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>

                                    <!--ElementName=Window is the Name of the XAML (in root definition)-->
                                    <CheckBox DataContext="{Binding DataContext, ElementName=Window}" Command="{Binding CheckAnswer}" CommandParameter="{Binding ElementName=cbox, Path=IsChecked}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}" x:Name="cbox" HorizontalAlignment="Right"/>
                                </Grid>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.Resources>
    </ListBox>

Now I'm trying the same on Windows Store App, but there's no AncestorType... If I'm doing it like this: 现在,我正在Windows Store App上尝试相同的操作,但是没有AncestorType ...如果我这样做的话:

<Style TargetType="ListBoxItem" x:Name="lbitem">

<CheckBox DataContext="{Binding DataContext, ElementName=Window}" Command="{Binding CheckAnswer}" CommandParameter="{Binding ElementName=cbox, Path=IsChecked}" IsChecked="{Binding ElementName=lbitem, Path=IsSelected}" x:Name="cbox" HorizontalAlignment="Left"/>

The Checkbox disappears and it'll just display the ItemsSource of OCAntwort. 复选框消失,它将仅显示OCAntwort的ItemsSource。 Clicking on an item won't execute the Command of the CheckBox (since there is no CheckBox). 单击一个项目将不会执行CheckBox的命令(因为没有CheckBox)。 Doing it like this: 这样做:

IsChecked="{Binding ElementName=lbitem, Path=IsSelected,RelativeSource={RelativeSource Mode=Self}}"

would display the CheckBox and execute the Command, but it's not being binded to the SelectedItem. 将显示CheckBox并执行Command,但未将其绑定到SelectedItem。

How can I bind the CheckBox (which is being displayed next to the ListBoxItem for each ListBoxItem) to the SelectedItem of a ListBox? 如何将CheckBox(显示在每个ListBoxItem旁边的ListBoxItem旁边)绑定到ListBox的SelectedItem? Working code is above... I want that the code should work in my Windows Store App (and later in my Windows Phone App). 工作代码在上方...我希望该代码在我的Windows Store应用程序中(以及以后在我的Windows Phone应用程序中)工作。

Example: 例:

You can download a sample here: http://weblogs.asp.net/marianor/archive/2008/02/04/multiselect-listview-with-checkboxes-in-wpf.aspx 您可以在此处下载示例: http : //weblogs.asp.net/marianor/archive/2008/02/04/multiselect-listview-with-checkboxes-in-wpf.aspx

This shows what I want to achieve in Windows Store App. 这显示了我想要在Windows Store App中实现的目标。

Edit: Doing it like this displays the ListBox with a content and a CheckBox, but I still need to bind the CheckBox by the SelectedItem. 编辑:这样做会显示具有内容和CheckBox的ListBox,但是我仍然需要通过SelectedItem绑定CheckBox。 Means when the user clicks on a CheckBox, the ListBoxItem has to be selected or if the user clicks on a ListBoxItem, the CheckBox has to be selected. 意味着当用户单击CheckBox时,必须选择ListBoxItem,或者如果用户单击ListBoxItem,则必须选择CheckBox。

    <ListBox ItemsSource="{Binding OCAntwort}" SelectedItem="{Binding SelectedAntwort}" Grid.Row="2" Grid.Column="1" x:Name="listBox" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Text}" Width="150" VerticalAlignment="Center"/>
                    <CheckBox IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected, Mode=TwoWay}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

不知道您是否找到了解决方案,但是我使用以下代码在CheckBoxIsChecked属性和父级ListBoxItemListViewItemIsSelected属性之间建立了双向绑定。

<CheckBox IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"/>

You should use TemplatedParent as you are writing inside the ListBoxItem. 在ListBoxItem中进行书写时,应使用TemplatedParent。

IsChecked="{Binding ElementName=lbitem, 
            Path=IsSelected,
            RelativeSource={RelativeSource Mode=TemplatedParent}}"

I think this may resolve your problem. 我认为这可以解决您的问题。

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

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