在wpf中,listbox中可以添加checkbox吗

 我来答
lgao622
2015-09-14 · 知道合伙人软件行家
lgao622
知道合伙人软件行家
采纳数:1137 获赞数:6548
毕业于武汉工程大学邮电与信息工程学院通信专业,软件行业,4年工作经验。

向TA提问 私信TA
展开全部
  1. 当然可以,wpf以 Content来进行加载界面的,Content可是Object对象,只有是满足其继承框架的控件都可以添加进来

<Window x:Class="WpfDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox>
            <ListBoxItem >
                <CheckBox Content="CheckBox"></CheckBox>
            </ListBoxItem>
            <ListBoxItem >
                <Button Content="Button"></Button>
            </ListBoxItem>
            <ListBoxItem >
                <TextBlock Text="TextBlock"></TextBlock>
            </ListBoxItem>
            <ListBoxItem >
                <StackPanel Orientation="Horizontal">
                    <Image Source="F:/Icon/fileopen.png"></Image>
                    <CheckBox Content="CheckBox" VerticalAlignment="Center"></CheckBox>
                    <Button Content="Button"></Button>
                    <TextBox Text="TextBox"></TextBox>
                </StackPanel>
            </ListBoxItem>
        </ListBox>
    </Grid>
</Window>

蘑菇饭资讯
2014-11-29 · TA获得超过6万个赞
知道大有可为答主
回答量:1.7万
采纳率:90%
帮助的人:1.1亿
展开全部
简单的例子
因为你要绑定数组到ListBox中的CheckBox上。除非你手工对CheckBox绑定,要不然好像实现不了。
给你个通过XAML直接实现绑定的例子。你参考一下吧。希望对你有用。
------------
App.xaml
------------
<Application x:Class="WpfProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>

</Application.Resources>
</Application>
-----------------
App.xaml.cs
-----------------
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace WpfProject
{
public partial class App : Application
{

private void Application_Startup(object sender, StartupEventArgs e)
{
App.Current.MainWindow = new MainWindow();
ViewModel vm = new ViewModel();
App.Current.MainWindow.DataContext = vm;
App.Current.MainWindow.ShowDialog();
}
}
}
-------------
MainWindow.xaml
-------------
<Window x:Class="WpfProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Height="100" HorizontalAlignment="Left" Margin="25,31,0,0"
VerticalAlignment="Top" Width="120" ItemsSource="{Binding BooleanArray}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}" Content="{Binding Text}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
---------------
MainWindow.xaml.cs
---------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式