如何在DataGridTemplateColumn中创建双击并处理事件
我是C#和WPF的新手,需要帮助.
我有一个DataGrid,其中有3列(A,B,C).
列A = DataGridTextColumn(绑定=名称)
列B = DataGridTemplateColumn(Binding = Value1)
列C = DataGridTemplateColumn(Binding = Value2)
最初加载时,DataGrid由CollectionViewSource填充.
例如Grid的数据如下
A列,B列,C列C
-------------------
Sec1,25.4,36. 3
秒2,12.4,6 3
Sec3,50.7,3. 8
我需要实现以下目标;
1.双击Col B的第0行(25.4),我需要获取Col B的Sec1的一些历史数据.
2.双击Col B,第1行(12.4),我需要获取Sec2的Col B的一些历史数据.
.
.
(n)单击第2行(3.8)的C列时,我需要获取C列Sec3的一些历史数据.
..因此,我相信我需要执行以下操作:
1.单击Col B和Col C的DataGridTemplateColumns时,创建一个MouseEvent或DoubleClick事件.
2.在我的C#代码中,我需要确定MouseEvent/Click是针对相应单元格的,因此我可以根据双击鼠标的Col/Row查找历史数据(我需要知道Col A(Sec1 ... Sec3)和发起双击的Col中的值.
鉴于上述情况,我希望以上所述是有道理的.
希望能对我的实现有所帮助.我正在通过相应的DataGridTemplateColumn的样式声明进行思考,但不确定...在通过google或bing搜索时无法在此上找到太多内容.
谢谢,
Manish
Hi,
I am new to C# and WPF and needs some help.
I have a DataGrid for which I have 3 columns (A, B, C).
Column A = DataGridTextColumn (Binding = NAME)
Column B = DataGridTemplateColumn (Binding = Value1)
Column C = DataGridTemplateColumn (Binding = Value2)
The DataGrid is populated by a CollectionViewSource when initially loaded.
The data for the Grid for example is as follows
Col A, Col B, Col C
-------------------
Sec1 , 25.4 , 36. 3
Sec2 , 12.4 , 6. 3
Sec3 , 50.7 , 3. 8
I need to achieve the following;
1. When I double click on Col B, Row 0 (25.4), I need to get some historical data for Sec1 for Col B.
2. When I double click on Col B, Row 1 (12.4), I need to get some historical data for Sec2 for Col B.
.
.
(n) When I click on Col C, Row 2 (3.8), I need to get some historical data for Sec3, Col C.
..Therefore, I believe I need to do the following:
1. Create a MouseEvent or DoubleClick event when I click in the DataGridTemplateColumns for Col B and Col C.
2. In my c# code, I need to be able to determine that the MouseEvent/Click came for the respective cell, so I can then look for the historical data based on the Col/Row where the mouse was double clicked (I need to know value in Col A (Sec1...Sec3) and the Col where the double click was initiated.
I hope the above makes sense, given the scenario stated.
Would appreciate any help on how I can achieve this. I''m thinking via a Style declaration for the respective DataGridTemplateColumn, but not sure......was not able to find much on this when searched via google or bing.
Thanks,
Manish
来查找发布在MSDN上的解决方案( href ="http://social.msdn.microsoft.com/Forums/en/wpf /thread/7fd1df22-03b9-408b-80a4-e0a642a3a857>).
基本上我做了以下事情:
1.在我的主DataGrid中添加一个MouseDoubleClick(即使您具有DataGridTextColumn或DataGridTemplateColumns,只要在DataGrid中定义并使用了CollectionViewSource,此方法也可以使用).
//在XAML中定义
< UserControl.Resources>
< ResourceDictionary>
< CollectionViewSource x:Key ="TestDataSource"/>
</UserControl.Resources& gt;
</ResourceDictionary>
//在您的DataGrid声明中
< DataGrid名称="DG"
ItemsSource ="{Binding Source = {StaticResource TestDataSource}}" MouseDoubleClick ="CHART_DISPLAY">
注意:TestDataSource是使用以下代码在c#代码中声明和填充的:
Was to find a solution posted on MSDN ( href="http://social.msdn.microsoft.com/Forums/en/wpf/thread/7fd1df22-03b9-408b-80a4-e0a642a3a857">).
Basically I did the following:
1. Add a MouseDoubleClick in my main DataGrid (this works even though you have DataGridTextColumn or DataGridTemplateColumns as long as your CollectionViewSource is defined and used in the DataGrid).
// Define this in the XAML
<UserControl.Resources>
<ResourceDictionary>
<CollectionViewSource x:Key="TestDataSource"/>
</UserControl.Resources>
</ResourceDictionary>
// In your DataGrid Declaration
<DataGrid Name="DG"
ItemsSource="{Binding Source={StaticResource TestDataSource}}" MouseDoubleClick="CHART_DISPLAY">
Note: TestDataSource was declared and populated in the c# code using the following:
// Get a handle on the Data Source in the WPF
CollectionViewSource dataViewSource = (CollectionViewSource)FindResource("TestDataSource");
//Populate the WPF Data
dataViewSource.Source = dataList;
//在您的C#代码中:
2.在主视图/窗口中定义以下方法:
// In your C# code:
2. Define the following method in my mainview/window:
// Clicking on the CHART_DISPLAY to generate the on-the-run graphs
private void CHART_DISPLAY(object sender, MouseButtonEventArgs e)
{
if (DG.SelectedItem == null)
{
Debug.WriteLine("Empty");
return;
}
else
{
Debug.WriteLine("Not Empty");
TestData selectedRow = DG.SelectedItem as TestData;
// Here, you can access any data item / member in the List<testdata> Object
Debug.WriteLine(string.Format("Data = Name={0}, Spread={1}",selectedRow.NAME, selectedRow.SPREAD));
}
}</testdata>
希望这对大家有帮助....为我担心.
Hope this help you all....wored for me.
我认为我们可以使用DataGrid.SelectedCells.
还要检查此链接 [
i think we can use DataGrid.SelectedCells.
Also check this link[^].