将ListBoxItem IsSelected触发器传播给子控件 [英] Propagate ListBoxItem IsSelected trigger to child control

查看:93
本文介绍了将ListBoxItem IsSelected触发器传播给子控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有可移动ListBoxItem的CheckedListBox.问题在于,只有在用户单击CheckBox区域时,该项目才会被检查,这有点尴尬.

I'm developing a CheckedListBox with self removable ListBoxItems. The problem is that an item only gets checked if the user clicks on the CheckBox area, which is kind of awkward.

如何创建ListBoxItem触发器(IsSelected)来检查"DataSourced" ListBox上的复选框?例如:

How do I create ListBoxItem triggers (IsSelected) to check the checkboxes on a "DataSourced" ListBox? Eg:

下面是我的控件(为简洁起见,所有其他代码都被省略了):

Below is my control (all other code have been omitted for brevity):

<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>

执行器是ExecutorObservableCollection,具有EnabledDescription作为成员.

Executors is a ObservableCollection of Executor which has Enabled and Description as members.

推荐答案

对于那些遇到相同问题的人,请参考以下代码,以供参考.我在任何地方都找不到任何工作示例,因此这可能很有用.

For further reference for those who come across this same question, here's a working snippet I have done. I couldn't find any working sample anywhere, so this might be of much use.

这里的主要思想是将选择事件传播到CheckBox es,这听起来太多了,或者只是简单地扩展了CheckBox选择区域以适合ListBoxItem.

The main idea here was to either propagate the selection event to the CheckBoxes, which sounds too much of a work, or to simple extend the CheckBox selection area to fit the ListBoxItem.

下面是有关如何实现第二种选择的示例:

Below is a sample on how to achieve the second option:

<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>

这应该产生以下内容:

这篇关于将ListBoxItem IsSelected触发器传播给子控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆