如何在消息框中显示复选框列表框项目 [英] how to show check list box item in message box

查看:67
本文介绍了如何在消息框中显示复选框列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在消息框中或窗体上的任何位置显示复选框列表框项目,但不阅读我发布的代码..
请帮我..
在此先感谢..

MainWindow.xaml

i want to display check list box item in message box or any where on form but not reading i posting my code..
plz help me ..
thanks in advanced..

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="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="136*" />
            <ColumnDefinition Width="367*" />
        </Grid.ColumnDefinitions>
        <ListBox Height="128" HorizontalAlignment="Left" Margin="13,6,0,0" Name="listLesson" VerticalAlignment="Top" Width="194" DataContext="{Binding}" IsSelected="{Binding IsChecked, ElementName=checkBox, Mode=TwoWay}" Grid.ColumnSpan="2">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Margin="2" VerticalAlignment="Center" IsChecked="{Binding IsVisited}"/>

                        <TextBlock Margin="2" Foreground="Black" FontSize="14"  Text="{Binding LessonName}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="145,60,0,0" Name="button1" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="button1_Click" />
    </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)
        {
            foreach (string item in listLesson.Items)
            {

                System.Windows.MessageBox.Show(item.ToString());

            }
        }
        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();
        }
    }
}

推荐答案


"我想在消息框中或表单上的任何位置显示复选框列表框项目,但不阅读我发布的代码.."
无法在System.Windows.MessageBox中显示类似CheckBox的控件.在表格上的任何地方都可以显示内容.

尝试使用System.Collections.ObjectModel.ObservableCollection而不是List.在WinForms中,您必须使用BindingList.

我可以通过将LessonInfo设置为数据源来自动生成ListView(数据->添加已添加的数据源...).然后,我将执行以下操作.

请记住要投票和发表评论.

Hi
"i want to display check list box item in message box or any where on form but not reading i posting my code.."
Displaying controls like CheckBox in a System.Windows.MessageBox is not possible. Displaying something any where on a form is possible.

Try using System.Collections.ObjectModel.ObservableCollection instead of List. In WinForms you had to use BindingList.

I would have auto-generate the ListView by setting LessonInfo as a data source (Data -> Add ned data source...). Then I would done the following.

Remember to vote and comment, please.

ObservableCollection<lessoninfo> _lessInfoCollection;
public MainWindow()
{
    InitializeComponent();
    _lessInfoCollection = new ObservableCollection<lessoninfo>();
    // For debugging
    /*_lessInfoCollection.Add(
           new LessonInfo(isVisited: true, lessonName: "Coding"));
    _lessInfoCollection.Add(
           new LessonInfo(isVisited: false, lessonName: "Implementing"));*/
    lessonInfoListView.ItemsSource = _lessInfoCollection;
        
    conn = new OleDbConnection();
    conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Software Development\vishal_wpf\dem\WpfApplication11\WpfApplication11\DLL.mdb";
}</lessoninfo></lessoninfo>


这篇关于如何在消息框中显示复选框列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆