wpf编程,ListBox里面的checkbox怎么绑定到一个bool的数组啊
我在Listbox里面放了几个checkbox,打算把他们绑定到一个bool的数组。可是不知道怎么实现。。。嗯,谢谢。不过这个例子不是twoway的,就是说,我改变che...
我在Listbox里面放了几个checkbox,打算把他们绑定到一个bool的数组。
可是不知道怎么实现。。。
嗯,谢谢。
不过这个例子不是two way的,就是说,我改变checkbox的状态,但后面的bool的值不会相应改动。要怎么处理呢 展开
可是不知道怎么实现。。。
嗯,谢谢。
不过这个例子不是two way的,就是说,我改变checkbox的状态,但后面的bool的值不会相应改动。要怎么处理呢 展开
展开全部
简单的例子
因为你要绑定数组到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;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfProject
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
-------------
ViewModel.cs
-------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfProject
{
class ViewModel
{
public List<ListItem> BooleanArray
{
get
{
List<ListItem> lst = new List<ListItem>();
lst.Add(new ListItem(true, "第一项"));
lst.Add(new ListItem(false, "第二项"));
return lst;
}
}
}
class ListItem
{
public bool IsSelected{get;set;}
public string Text {get;set;}
public ListItem(bool isSelected, string text)
{
this.IsSelected = isSelected;
this.Text = text;
}
}
}
------------
因为你要绑定数组到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;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfProject
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
-------------
ViewModel.cs
-------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfProject
{
class ViewModel
{
public List<ListItem> BooleanArray
{
get
{
List<ListItem> lst = new List<ListItem>();
lst.Add(new ListItem(true, "第一项"));
lst.Add(new ListItem(false, "第二项"));
return lst;
}
}
}
class ListItem
{
public bool IsSelected{get;set;}
public string Text {get;set;}
public ListItem(bool isSelected, string text)
{
this.IsSelected = isSelected;
this.Text = text;
}
}
}
------------
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询