本文介绍了如何在WPF的文本框中显示复选框列表框的选定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望单击按钮即可在文本框中的复选框列表中显示选定的项目..
谁能帮我..我的数据库名称是DLL,它在访问中,而表是具有单个字段子对象的演示.
我在这里发布我的代码.
谢谢.. MainWindow.xaml
i want to show selected item in check list box in text box on a click of button..
can any one help me..my database name is DLL which is in access and table is demo with single field sub.
i am posting my code here..
thank you..MainWindow.xaml
<Window x:Class="WpfApplication12.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="536" Width="853" Loaded="Window_Loaded">
<Grid>
<TabControl Height="260" HorizontalAlignment="Left" Margin="41,57,0,0" Name="tabControl1" VerticalAlignment="Top" Width="731">
<TabItem Header="old" Name="tabItem1">
<Grid></Grid>
</TabItem>
<TabItem Header="new">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition>
</ColumnDefinition>
<ColumnDefinition>
</ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Height="180" Margin="10,10,24,37" Width="327">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*" />
<ColumnDefinition Width="317*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="84*" />
<RowDefinition Height="81*" />
</Grid.RowDefinitions>
<TextBox Grid.ColumnSpan="2" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="8,60,0,0" Name="textBox3" VerticalAlignment="Top" Width="209" />
<ListBox Grid.Column="1" Grid.RowSpan="2" Height="128" HorizontalAlignment="Left" ItemsSource="{Binding Path=tripList}" Margin="13,6,0,0" Name="listLesson" Selector.IsSelected="{Binding IsChecked, ElementName=checkBox, Mode=TwoWay}" VerticalAlignment="Top" Width="194" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsVisited}" Margin="2" VerticalAlignment="Center" />
<TextBlock FontSize="14" Foreground="Black" Margin="2" Text="{Binding LessonName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="236,69,0,0" Name="button3" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="button3_Click" />
</Grid>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
MainWindow.xaml.cs
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;
using System.Data.OleDb;
using System.Data;
namespace WpfApplication12
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public OleDbConnection conn;
public OleDbDataAdapter da;
public DataSet ds;
public DataSet ds1 = new DataSet();
public DataTable dt;
public OleDbCommand cmd;
public OleDbDataReader dr;
List<LessonInfo> LessonList = new List<LessonInfo>();
public MainWindow()
{
InitializeComponent();
listLesson.ItemsSource = LessonList;
DataContext = this;
conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Software Development\vishal_wpf\dem\WpfApplication11\WpfApplication11\DLL.mdb";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
LessonList.Add(new LessonInfo(false, textBox3.Text));
listLesson.Items.Refresh();
}
public class LessonInfo
{
public LessonInfo(bool isVisited, string lessonName)
{
IsVisited = isVisited;
LessonName = lessonName;
}
public Boolean IsVisited
{ get; set; }
public String LessonName
{ get; set; }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
conn.Open();
try
{
da = new OleDbDataAdapter("SELECT distinct sub FROM demo", conn);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
LessonList.RemoveRange(0, LessonList.Count());
for (int i = 0; i < dt.Rows.Count; i++)
{
LessonList.Add(new LessonInfo(false, dt.Rows[i].ItemArray[0].ToString()));
listLesson.Items.Refresh();
}
conn.Close();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
textBox3.Text = listLesson.SelectedItem.ToString();
}
}
}
推荐答案
这篇关于如何在WPF的文本框中显示复选框列表框的选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!