如何在Silverlight应用程序中选择数据网格中的行或单元格 [英] How to select a row or cell in the datagrid in a silverlight application
问题描述
大家好,
我是Silverlight的新手,所以我想知道如何在Silverlight应用程序中选择数据网格中的行或单元格...
有谁可以帮助我吗?
问候
Haluk
Hello guys,
I am new on silverlight so I would like to know that how to select a row or cell in the datagrid in a silverlight application...
Is there anyone who can help me about this?
Regards
Haluk
推荐答案
假设您在主页上有一个数据网格和一个按钮:
Hi,
Saying you have a data grid and a button on your main page:
<Grid x:Name="LayoutRoot" Background="White" >
<sdk:DataGrid Name="dg" SelectionChanged="DgSelectionChanged" Width="200" Height="200"/>
<Button Content="Start" Click="ButtonClick" Width="200" Height="20" Margin="12,24,188,256" />
</Grid>
单击按钮时加载一些数据的事件:
Event for loading some data when button is clicked:
private void ButtonClick(object sender, RoutedEventArgs e)
{
var persons = new List<Person>
{
new Person {Name = "Name 1", Address = "Address 1"},
new Person {Name = "Name 2", Address = "Address 2"}
};
dg.AutoGenerateColumns = true;
dg.ItemsSource = persons;
dg.SelectionMode = DataGridSelectionMode.Single;
}
哪里人:
Where person:
public class Person
{
public string Name { get; set; }
public string Address { get; set; }
}
当选择更改触发以下事件将做的工作,你需要:
私有void DgSelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
var dataGrid =发送者为DataGrid;
如果(dataGrid!= null)
{
int selectedIndex = dataGrid.SelectedIndex;
如果(selectedIndex> -1)
{
DataGridColumn列= dataGrid.Columns [0];
FrameworkElement fe = column.GetCellContent(dataGrid.SelectedItem);
FrameworkElement结果= GetParent(fe,typeof(DataGridCell));
如果(结果!=空)
{
var cell =(DataGridCell)result;
//更改前景色
cell.Foreground =新的SolidColorBrush(Colors.Blue);
//如何获取单元格值?
var block = fe as TextBlock;
如果(block!= null)
{
字符串cellText = block.Text;
MessageBox.Show(cellText);
}
}
}
}
}
私有FrameworkElement GetParent(FrameworkElement子元素,类型为targetType)
{
对象parent = child.Parent;
如果(parent!= null)
{
如果(parent.GetType()== targetType)
{
返回(FrameworkElement)parent;
}
返回GetParent((FrameworkElement)parent,targetType);
}
返回null;
}
让我知道这是否对您有帮助.
And when selection changes fire the following event that will do the job you need:
private void DgSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var dataGrid = sender as DataGrid;
if (dataGrid != null)
{
int selectedIndex = dataGrid.SelectedIndex;
if (selectedIndex > -1)
{
DataGridColumn column = dataGrid.Columns[0];
FrameworkElement fe = column.GetCellContent(dataGrid.SelectedItem);
FrameworkElement result = GetParent(fe, typeof(DataGridCell));
if (result != null)
{
var cell = (DataGridCell)result;
//changes the forecolor
cell.Foreground = new SolidColorBrush(Colors.Blue);
//how to get cell value?
var block = fe as TextBlock;
if (block != null)
{
string cellText = block.Text;
MessageBox.Show(cellText);
}
}
}
}
}
private FrameworkElement GetParent(FrameworkElement child, Type targetType)
{
object parent = child.Parent;
if (parent != null)
{
if (parent.GetType() == targetType)
{
return (FrameworkElement)parent;
}
return GetParent((FrameworkElement)parent, targetType);
}
return null;
}
Let me know if this helped you.
这篇关于如何在Silverlight应用程序中选择数据网格中的行或单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!