将复选框绑定到ListBox中的SelectedItem

本文关键字:中的 SelectedItem ListBox 复选框 绑定 | 更新日期: 2023-09-27 18:19:42

我在WPF中创建了一个ListBox,它执行以下操作:

它绑定到我的ViewModel中的DataSource。列表框包含项目,每个项目旁边都有一个复选框。如果用户单击复选框,则项目被选中,命令将被执行。它运行良好,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>

现在我正在Windows应用商店应用程序上尝试同样的操作,但没有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"/>

复选框消失,只显示OCAntwort的ItemsSource。单击项目不会执行"复选框命令"(因为没有复选框)。这样做:

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

将显示复选框并执行命令,但它没有绑定到SelectedItem。

如何将复选框(显示在每个ListBoxItem的ListBoxItem旁边)绑定到ListBox的SelectedItem?工作代码在上面。。。我希望代码能在我的Windows应用商店应用程序中工作(稍后在我的WindowsPhone应用程序中)。

示例:

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

这显示了我想要在Windows应用商店中实现的目标。

编辑:这样做会显示带有内容和复选框的ListBox,但我仍然需要通过SelectedItem绑定复选框。意味着当用户单击复选框时,必须选择ListBoxItem;或者如果用户单击ListBoxItem,则必须选择复选框。

    <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>

将复选框绑定到ListBox中的SelectedItem

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

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

在ListBoxItem内部编写时,应该使用TemplatedPrent。

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

我认为这可以解决你的问题。