从列表框项中设置复选框文本值

本文关键字:复选框 文本 设置 列表 | 更新日期: 2023-09-27 17:53:03

如何为添加到列表框中的每个项目绑定复选框内容值?

<ListBox BorderThickness="2" Height="389" 
 HorizontalAlignment="Left" Margin="10,10,0,0" 
 Name="lboIssues" VerticalAlignment="Top" Width="175"
 SelectionMode="Multiple" ItemsSource="{Binding Mode=OneWay}">

然后这里是我的ItemTemplate

<ListBox.ItemTemplate>
  <DataTemplate>
   <StackPanel Margin="0,5,0,5" Orientation="Horizontal" >
    <CheckBox   Name="checkbox"
                Content="{Binding Path=Value, Mode=OneWay}"
                VerticalContentAlignment="Center"
        Margin="0,0,5,0"
                Width="Auto"
                IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, 
                AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
                IsTabStop="False" Checked="FilterCheckbox_CheckChanged" 
                Unchecked="FilterCheckbox_CheckChanged"/>
   </StackPanel>                         
  </DataTemplate>
</ListBox.ItemTemplate>`

当我勾选时,复选框的内容值为空白

从列表框项中设置复选框文本值

尝试更改为

    <CheckBox   Name="checkbox"
                Content="{Binding Path=Value, Mode=OneWay}"
                VerticalContentAlignment="Center"
                Margin="0,0,5,0"
                Width="Auto"
                IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, 
                    AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
                IsTabStop="False" Checked="FilterCheckbox_CheckChanged" 
                Unchecked="FilterCheckbox_CheckChanged"
                Content="{Binding}"/>