c# - 从列表框项设置复选框文本值

标签 c# wpf xaml

如何为将添加到我的列表框的每个项目绑定(bind)复选框内容值?

<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}"/>

关于c# - 从列表框项设置复选框文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14314443/

相关文章:

c# - 如何在 MVVM 模式 wpf 中绑定(bind) StrokeDashArray 属性

WPF : InputBindings on a StackPanel

c# - 如何在没有浏览器弹出窗口的情况下使用 OAuth2 发送 GMail/GSuite 电子邮件?

c# - 您将如何获得 int 数组中最低值的索引?

c# - LINQ Group By 多个参数或字段

c# - 子执行命令后调用方法

c# - 如何正确地将 IsPressed 属性绑定(bind)到我的命令参数?

c# - 如何为错误创建类似的模板?

wpf - 如何在 XAML 编辑器中查看设计时数据绑定(bind)(它在运行时工作)?

c# - 是否可以获取 Action 的方法体(文本)?