首页编程checklistbox?c#wpf 中有没有checklistbox

checklistbox?c#wpf 中有没有checklistbox

编程之家 2023-11-06 201次浏览

大家好,今天小编来为大家解答以下的问题,关于checklistbox,c#wpf 中有没有checklistbox这个很多人还不知道,现在让我们一起来看看吧!

checklistbox?c#wpf 中有没有checklistbox

CheckBox和CheckBoxList有什么区别

楼主应该说错了吧,应该是CheckBox和CheckListBox。

CheckBoxshi复选框,用户可以通过CheckBox指示某个特定条件是处于打开状态还是关闭状态,它常用于为用户提供是/否真/假

选项,因为CheckBox彼此是独立工作,所以用户可以同时选择一个或多个CheckBox。

CheckListBox控件几乎具有ListBox控件的所有功能,它可以看做每项都是CheckBox控件的ListBox,在应用程序中,CheckListBox控件用于提供一组并列的可选项。

C# winform中,checkedListBox控件搞不懂

1:设置多行,点击CheckListBox控件右上角的三角符号,选择编辑项,在弹出的对话框里面输入你要生成的行就可以了,换行表示开始1个新行,如下

ni

checklistbox?c#wpf 中有没有checklistbox

hao

生成的就是2个选项框。

2:我看了下CheckListBox的事件,checkedListBox1_SelectedIndexChanged可以是勾选后触发。不过前提是点击的时候必须点的是复选框,这样才能勾选,如果没有点击复选框,点的是文字,那么还是没有勾选。

大概看了下,checkedListBox不是太好用。我也没能好好使用,希望上面的回答对你有帮助,如果有更好的方法也写出来分享下

c#wpf 中有没有checklistbox

实际项目中常常要实现有CheckBox列表框。但是WPF没有自带这样的一个控件,下面就用Style来实现这样的功能。而对于CheckBox列表框,又常常会有一个Select All的CheckBox来表示当前列表框的选择状态。这个功能也会被包含在下面的示例之中。效果如下图所示。

对于单纯的,没有后台数据绑定的情况下,这个功能可以用ItemContainerStyle来实现。代码如下:

checklistbox?c#wpf 中有没有checklistbox

复制代码

CheckListBoxItemContainerStyle

<Style x:Key="CheckListBoxItemContainerStyle"

TargetType="{x:Type ListBoxItem}">

<!--Set it un-focusable, becaues the CheckBox in it should be focusable and only it.-->

<Setter Property="Focusable" Value="False"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type ListBoxItem}">

<CheckBox Content="{TemplateBinding Content}"

ContentTemplate="{TemplateBinding ContentTemplate}"

ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"

IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}"/>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

复制代码

其中要对Content和ContentTemplate等属性进行绑定,以方便对其进行扩展,保证其通用性。这个Style一般会放在Application级别的Resource中。

对于有后台数据绑定的情况,一般会有双个属性要绑定,一个是CheckBox里的Content,一个是CheckBox的IsChecked。绑定的路径,只有在用一个Style的ListBox那里才知道,所以并不能写在这个Style里,否则会破坏这个Style的通用性。比较合理的方式是基于这个现有的Style进行修改。

对于下面的数据类。

DataItem Class

我们需要下面这个有针对性的Style来应用数据绑定。

复制代码

DataItemCheckListBoxStyle

<Style x:Key="DataItemCheckListBoxStyle"

TargetType="{x:Type ListBox}"

BasedOn="{StaticResource{x:Type ListBox}}">

<Setter Property="ItemTemplate">

<Setter.Value>

<DataTemplate>

<TextBlock Text="{Binding Name}"/>

</DataTemplate>

</Setter.Value>

</Setter>

<Setter Property="ItemContainerStyle">

<Setter.Value>

<Style TargetType="{x:Type ListBoxItem}"

BasedOn="{StaticResource CheckListBoxItemContainerStyle}">

<Setter Property="IsSelected"

Value="{Binding IsEnabled}"/>

<Setter Property="Margin" Value="2,2,0,0"/>

</Style>

</Setter.Value>

</Setter>

<Setter Property="SelectionMode" Value="Multiple"/>

</Style>

复制代码

在上面的Style中,使用了ItemTemplate来指定CheckBox里的Content绑定到的属性,并把ListBoxItem的IsSelected绑定数据的相应属性上。由于这个Style是针对特定数据写的,所以应当放置在使用这个Style的ListBox所在的Window的Resource中。

当然,也可以为ListBox添加两个绑定类型的Attached Property来实现一个通用的Style。不过这个Property一样要在使用的地方设置,其实没有太大区别。有兴趣的读者可以自己试一下。

对于Select All这个CheckBox而言,用Attached Property倒是很方便。给CheckBox添加一个SyncTarget属性指向要同步的ListBox,就可以在Window.xaml.cs之外的地方同步CheckBox和ListBox了。代码如下:

ToggleButtonProperty

使用方式也很简单。如下代码所示。

复制代码

用法

<DockPanel Margin="12">

<CheckBox Content="Select All"

Margin="0,0,0,5"

DockPanel.Dock="Top"

ext:ToggleButtonProperty.SyncTarget="{Binding ElementName=checkListBox}"/>

<ListBox x:Name="checkListBox"

Style="{StaticResource DataItemCheckListBoxStyle}"

ItemsSource="{Binding Path=Items, ElementName=mainWindow}"/>

</DockPanel>

怎么让checklistBox的某一项始终是选中的

checklistBox?这是什么控件?你说的是CheckBoxList吧?如果是下面是我的思路;让它某一项始终选中:

<asp:CheckBoxList ID="chklist" runat="server">

<asp:ListItem Selected="True" Enabled="false">只读</asp:ListItem>

<asp:ListItem Selected="True">对比</asp:ListItem>

</asp:CheckBoxList>

这是前台代码。

将你要始终选择选中的那一项Selected属性设为True;Enabled设为False;那么此项就无法被改变;这样便可以达到始终选中的效果;

不知道你要的是不是这个效果。。希望对你有帮助。。。。。

文章到此结束,如果本次分享的checklistbox和c#wpf 中有没有checklistbox的问题解决了您的问题,那么我们由衷的感到高兴!

西安网站公司,西安有哪些做网站好的公司 代码整洁之道 代码整洁之道的作者简介