0

我有一个 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="aaa@gmail.com" },
                    new Users { FirstName="bbb", EmailID="bbb@gmail.com" },
                    new Users { FirstName="ccc", EmailID="ccc@gmail.com" },
                    new Users { FirstName="ddd", EmailID="ddd@gmail.com" },
                    new Users { FirstName="eee", EmailID="eee@gmail.com" },
                    new Users { FirstName="fff", EmailID="fff@gmail.com" },
                    new Users { FirstName="ggg", EmailID="ggg@gmail.com" },
                    new Users { FirstName="hhh", EmailID="hhh@gmail.com" },
            };
        }



     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; }
        }
    }
}
4

1 回答 1

0

我终于得到了答案

向该文本框添加 Key_UP 事件

在后面的代码中:

private void txtFirstName_KeyUp(object sender, KeyEventArgs e)
        {
            List<Users> _lstTemp = dataGrid1.ItemsSource as List<Users>;
            var selectedrow = dataGrid1.SelectedItem as Users;
            TextBox _TextFirstName = dataGrid1.Columns[1].GetCellContent(selectedrow) as TextBox;

            (from value in _lstTemp
             where value.EmailID == selectedrow.EmailID
             select value).ToList().ForEach(value => value.FirstName = _TextFirstName.Text);

            dataGrid1.ItemsSource = _lstTemp;
        }

我很高兴它解决了我的问题,但让我疯狂的是没有人愿意分享一个想法:(

于 2013-06-29T06:47:19.170 回答