0
我有一個DataGrid這將有兩個文本框會從列表Silverlight的 - 獲取一個DataGrid
這是簡單而精緻的被綁定內從一個文本框更新單元格內容的價值...
假設用戶將值「bbb」更改爲123,然後刪除記錄「ccc」,此時網格會刷新...此時更改的值將被刪除!原來的價值是綁定的!
我需要收集datagrid的單元格內容的當前值如何?
下面是我的示例代碼:
MainPage.XAML
<UserControl x:Class="SampleDataGridApplication.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="400" Loaded="UserControl_Loaded" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<Border Name="bdAddUsersCSVButtons" Width="380" Height="40" Canvas.Top="60" Margin="12,530,8,30">
<Canvas Width="Auto" Height="Auto">
<Button Content="Remove" Height="30" Width="100" HorizontalAlignment="Left" Name="btnRemove" Canvas.Left="150" Canvas.Top="5" Cursor="Hand"
FontFamily="Lucida Grande" FontSize="13" FontStyle="Normal" FontWeight="Bold" VerticalAlignment="Top" Click="btnRemove_Click" />
<Button Content="Reset" Height="30" Width="100" Name="btnReset" HorizontalAlignment="Left" Canvas.Left="280" Canvas.Top="5" VerticalAlignment="Top" Cursor="Hand"
FontFamily="Lucida Grande" FontSize="13" FontStyle="Normal" FontWeight="Bold" Click="btnReset_Click" />
</Canvas>
</Border>
<sdk:DataGrid AutoGenerateColumns="False" Height="500" HorizontalAlignment="Left" Margin="10,10,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="380" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn CanUserResize="False" Header="" Width="30" CanUserReorder="False">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chboxUser" IsChecked="False" VerticalAlignment="Center" Padding="0,15,0,0" Width="20" Height="20" CommandParameter="{Binding EmailID}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn CanUserResize="False" Header="FirstName" Width="100" CanUserReorder="False" IsReadOnly="True">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Name="txtFirstName" FontFamily="Lucida Grande" Width="80" Height="22" Foreground="#666666" FontSize="9" FontStyle="Normal" Margin="0,3,0,0" TabIndex="0"
FontWeight="Normal" Text="{Binding FirstName}"></TextBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn CanUserResize="False" Header="EmailID" Width="245" CanUserReorder="False" IsReadOnly="True">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Name="txtEmailID" FontFamily="Lucida Grande" Width="80" Height="22" Foreground="#666666" FontSize="9" FontStyle="Normal" Margin="0,3,0,0" TabIndex="0"
FontWeight="Normal" Text="{Binding EmailID}"></TextBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
</UserControl>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SampleDataGridApplication
{
public partial class MainPage : UserControl
{
List<Users> _lstUsers = new List<Users>();
public MainPage()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
GenerateList();
LoadValues();
}
private void LoadValues()
{
dataGrid1.ItemsSource = _lstUsers;
}
private void GenerateList()
{
_lstUsers = new List<Users>
{
new Users { FirstName="aaa", EmailID="[email protected]" },
new Users { FirstName="bbb", EmailID="[email protected]" },
new Users { FirstName="ccc", EmailID="[email protected]" },
new Users { FirstName="ddd", EmailID="[email protected]" },
new Users { FirstName="eee", EmailID="[email protected]" },
new Users { FirstName="fff", EmailID="[email protected]" },
new Users { FirstName="ggg", EmailID="[email protected]" },
new Users { FirstName="hhh", EmailID="[email protected]" },
};
}
private void btnRemove_Click(object sender, RoutedEventArgs e)
{
List<Users> _lstTemp = dataGrid1.ItemsSource as List<Users>;
//Here i'm reading the DataGrid Values; i need to collect current values of the records how?
foreach (Users _RowValue in dataGrid1.ItemsSource)
{
CheckBox _CheckBox = dataGrid1.Columns[0].GetCellContent(_RowValue) as CheckBox;
if (_CheckBox.IsChecked == true)
{
_lstTemp = (from value in _lstTemp.Where(Item=> Item.EmailID!= _CheckBox.CommandParameter.ToString()) select value).ToList();
}
}
dataGrid1.ItemsSource = null;
dataGrid1.ItemsSource = _lstTemp;
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
dataGrid1.ItemsSource = null;
LoadValues();
}
public class Users
{
public string FirstName { get; set; }
public string EmailID { get; set; }
}
}
}