【问题标题】:Propagate ListBoxItem IsSelected trigger to child control将 ListBoxItem IsSelected 触发器传播到子控件
【发布时间】: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 是 ExecutorObservableCollection,其成员为 EnabledDescription

【问题讨论】:

  • @mm8 介意告诉我们你为什么要 -1 这个?

标签: c# wpf checkbox listbox custom-controls


【解决方案1】:

为了给遇到同样问题的人提供进一步的参考,这是我已经完成的工作 sn-p。我在任何地方都找不到任何工作样本,所以这可能很有用。

这里的主要思想是将选择事件传播到CheckBoxes,这听起来工作量太大,或者简单地扩展CheckBox选择区域以适应ListBoxItem

下面是如何实现第二个选项的示例:

<ListBox x:Name="executors" ItemsSource="{Binding Executors}" HorizontalAlignment="Left" Height="121" Margin="23,19,0,0" VerticalAlignment="Top" Width="362" ScrollViewer.VerticalScrollBarVisibility="Visible">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Height" Value="30"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="9*"/>
                                <ColumnDefinition Width="1*" />
                            </Grid.ColumnDefinitions>
                            <CheckBox Content="{Binding Description}" IsChecked="{Binding Enabled}" VerticalContentAlignment="Center" Margin="4,0"/>
                            <Button Command="{Binding DataContext.RemoveExecutorCommand, ElementName=executors}" CommandParameter="{Binding}" Width="21" Height="21" Background="White" Grid.Column="1">
                                <Image Source="trash.png" Stretch="Fill" Width="14" Height="14" />
                            </Button>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

这应该产生以下内容:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 2011-02-05
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    相关资源
    最近更新 更多