在wpf中,listbox中可以添加checkbox吗
2个回答
2015-09-14 · 知道合伙人软件行家
关注
展开全部
当然可以,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>
展开全部
简单的例子
因为你要绑定数组到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;
因为你要绑定数组到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;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询