|
|
|
ช่วยดู code WPF ให้หน่อยครับ เกี่ยวกับ class ItemContainerGenerator.ContainerFromItem ผมใช้แล้วมันได้แต่ค่า null อะครับ |
|
|
|
|
|
|
|
จุดประสงค์ของผมคือ เหตุการณ์นี้น่ะครับ
Code (C#)
private void IsvProductList_Drop(object sender, DragEventArgs e)
{
tempdata = (string[])(e.Data.GetData(DataFormats.Text, true));
ListViewItem lvi = (ListViewItem)IsvProductList.ItemContainerGenerator.ContainerFromItem(tempdata);
IsvProductList.Items.Add(lvi);
Array.Clear(tempdata, 0, tempdata.Length);
}
ผมจะให้ใน tempdata มาเพิ่มเป็นแถวใหม่ใน IsvProductList น่ะครับ ไม่ทราบว่าผมเขียนแบบนี้ผิดตรงไหนหรอครับ มัน มีแต่ null ใน IsvProduct แต่ เพิ่มแถวใหม่ครับ เพิ่มค่า null ใน row IsvProdcutList มันไม่นำค่า tempdata มาเพิ่มใน lsvProductList ครับ ช่วยด้วยครับบบบบบ
นี่ code ทั้งหมดครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 Microsoft.Windows.Controls;
using Drewsn32Biz.DataBase;
using Microsoft.Windows.Controls.Primitives;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Controls.Primitives;
using System.Data;
namespace Drewsn32Biz
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DataBase.dbDrewsn32BizDataContext db = new DataBase.dbDrewsn32BizDataContext();
string[] tempdata = new string[4];
public MainWindow()
{
InitializeComponent();
IsvProductList.Drop += new DragEventHandler(IsvProductList_Drop);
dgvProductList.MouseMove += new MouseEventHandler(dgvProductList_MouseMove);
}
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
var cs = from c in db.UserNames
select new
{
UserID = c.UserName1,
Password = c.Password,
IsNormal = c.IsNormal,
HumanID = c.HID
};
if (cs.Count() > 0)
{
dgvProductList.ItemsSource = cs.ToList();
GridView gv = new GridView();
GridViewColumn gvc = new GridViewColumn();
gvc.Header = "UserID";
gvc.Width = Double.NaN;
gv.Columns.Add(gvc);
GridViewColumn gvc1 = new GridViewColumn();
gvc1.Header = "Password";
gvc1.Width = Double.NaN;
gv.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.Header = "IsNormal";
gvc2.Width = Double.NaN;
gv.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.Header = "HumanID";
gvc3.Width = Double.NaN;
gv.Columns.Add(gvc3);
// Setup the GridView Columns
IsvProductList.View = gv;
// Display the Data
}
else
{
dgvProductList.ItemsSource = null;
}
}
private void IsvProductList_Drop(object sender, DragEventArgs e)
{
tempdata = (string[])(e.Data.GetData(DataFormats.Text, true));
ListViewItem lvi = (ListViewItem)IsvProductList.ItemContainerGenerator.ContainerFromItem(tempdata);
IsvProductList.Items.Add(lvi);
Array.Clear(tempdata, 0, tempdata.Length);
}
private void ShowLogin()
{
this.Hide();
Login log = new Login();
log.Owner = this;
log.ShowDialog();
//ถ้า From MainLogin ส่งค่า DialogResult = true มาให้ทำ if
if (log.DialogResult.HasValue && log.DialogResult.Value)
{
this.Show();
return;
}
else
{
this.Close();
}
}
private void dgvProductList_MouseMove(object sender, MouseEventArgs e)
{
#region Algorithum Check Row
DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridCell)
{
// navigate further up the tree
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
try
{
int count = 0;
string data = "";
foreach (var dataGridCellInfo in dgvProductList.SelectedCells)
{
PropertyInfo pi = dataGridCellInfo.Item.GetType().GetProperty(Convert.ToString(dataGridCellInfo.Column.Header));
var temp = Convert.ToString(pi.GetValue(dataGridCellInfo.Item, null));
data += temp;
tempdata[count] = Convert.ToString(temp);
count += +1;
}
textBox2.Text = data;
t1.Text = tempdata[0].ToString();
t2.Text = tempdata[1].ToString();
t3.Text = tempdata[2].ToString();
t4.Text = tempdata[3].ToString();
}
catch (Exception)
{
return;
}
}
#endregion
if (e.LeftButton == MouseButtonState.Pressed)
{
DragDropEffects dds;
DataObject AnyData;
AnyData = new DataObject();
AnyData.SetData(DataFormats.Text, tempdata);
dds = DragDrop.DoDragDrop(dgvProductList, AnyData, DragDropEffects.Copy);
}
}
}
}
Tag : .NET, C#, VS 2010 (.NET 4.x)
|
ประวัติการแก้ไข 2011-04-03 22:10:34
|
|
|
|
|
Date :
2011-04-03 22:02:11 |
By :
Drewsn32 |
View :
1142 |
Reply :
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|