【发布时间】:2019-04-25 21:42:08
【问题描述】:
我正在开发一个 CheckedListBox 可自行拆卸ListBoxItems。问题是只有当用户点击CheckBox 区域时才会检查项目,这有点尴尬。
如何创建 ListBoxItem 触发器 (IsSelected) 来检查“DataSourced”ListBox 上的复选框?例如:
以下是我的控件(为简洁起见,所有其他代码均已省略):
<ListBox x:Name="executors" ItemsSource="{Binding Executors}" HorizontalAlignment="Left" Height="121" Margin="23,19,0,0" VerticalAlignment="Top" Width="362">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Height" Value="30" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<CheckBox Margin="4,8" IsChecked="{Binding Enabled}">
<ContentPresenter Content="{Binding Description}">
</ContentPresenter>
</CheckBox>
<Button Command="{Binding DataContext.RemoveExecutorCommand, ElementName=executors}" CommandParameter="{Binding}" Background="White" Height="22" Width="22" Grid.Column="1">
<Image Source="trash.png" Stretch="Fill" Width="14" Height="14" />
</Button>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Executors 是 Executor 的 ObservableCollection,其成员为 Enabled 和 Description。
【问题讨论】:
-
@mm8 介意告诉我们你为什么要 -1 这个?
标签: c# wpf checkbox listbox custom-controls