【发布时间】:2021-06-24 12:26:28
【问题描述】:
我有以下列表框:
<ListBox Grid.Column="0" BorderBrush="Black" Margin="15,20,122,15" MinHeight="25" Name="tbxFiles"
VerticalAlignment="Stretch"
ItemsSource="{Binding Items}"
SelectionMode="Multiple" Grid.ColumnSpan="2">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<CheckBox Margin="5,2"
IsChecked="{Binding IsSelected,RelativeSource={RelativeSource TemplatedParent}}">
<ContentPresenter />
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
如您所见,我为 ListBox 中的每个项目创建了一个 CheckBox。但是现在我希望在单击按钮时将每个选中 CheckBox 的 ListBox Item 的文本打包到一个列表中。由于我对绑定不太熟悉,所以我问我如何从后面的代码中使用 IsChecked 绑定来访问它?
【问题讨论】:
-
由于您使用的是 2 路绑定,因此在项目模型中您应该能够读取它...所以迭代
Items并检查选择了哪些(通过IsSelected属性) -
@WPFHelp:来自视图的代码隐藏还是来自视图模型?
Items是如何定义的?它是什么类型的? -
你能给我一个 C# 示例,说明我需要如何在后面的代码中编写它以将每个 Checked Item 添加到 List 中吗?
-
@mm8 我猜我没有定义项目
-
那么
ItemsSource="{Binding Items}"是什么?
标签: c# wpf xaml data-binding