Silverlight获取DataGrid中选定的行数据
在我的 Silverlight 应用程序中,我定义了一个带有包含单选按钮的模板列的数据网格,如下所示:
XAML:
<data:DataGrid x:Name="Grid1" Margin="8">
<data:DataGrid.Columns>
<data:DataGridTemplateColumn Header="RadioButtons">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton x:Name="rdbIndataGrid" IsChecked="false" GroupName="myGroup" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
C#
public MainPage()
{
// Required to initialize variables
InitializeComponent();
string data = "1,2,3,4,5,6,7,8,9";
Grid1.ItemsSource = data.Split(',');
}
单击按钮时,我希望能够:
a) 找出选择了哪个单选按钮。
b) 从网格中与所选单选按钮对应的单元格之一获取数据。
有没有简单的方法可以做到这一点?网格上似乎没有行集合。或者我必须将其绑定到数据源然后检查数据源?
非常感谢。
In my Silverlight application I have defined a datagrid with an template column containing a radio button as follows:
XAML:
<data:DataGrid x:Name="Grid1" Margin="8">
<data:DataGrid.Columns>
<data:DataGridTemplateColumn Header="RadioButtons">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton x:Name="rdbIndataGrid" IsChecked="false" GroupName="myGroup" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
C#
public MainPage()
{
// Required to initialize variables
InitializeComponent();
string data = "1,2,3,4,5,6,7,8,9";
Grid1.ItemsSource = data.Split(',');
}
When a button is clicked I want to be able to:
a) Find out which radio button was selected.
b) Get the data from one of the cells in the grid which corresponds to the selected radio button.
Is there an easy way to do this? There doesnt seem to be a rows collection on the grid. Or do I have to bind it to a datasource and then check the data source?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更喜欢的方法是将 IsChecked 绑定到分配给 ItemsSource 的对象的属性。但在这里我将向您展示实现这一目标的困难方法
(编辑:实际上,对于这种情况,以下内容过于复杂,但我现在将其留在这里,请参阅之后的编辑)
首先您需要一个我的 VisualTreeEnumeration 扩展方法:-
现在,在我的测试中,我刚刚将一个名为
lstOutput
的ListBox
添加到我的 Xaml 中。现在将以下几个事件处理程序添加到您的UserControl
中:-最后像这样调整单选按钮 Xaml:-
(Xaml 连接事件的巧妙之处之一是,即使元素是模板的一部分)。
您会注意到,在事件处理程序中,我从发送的
RadioButton
沿着可视化树向上查找包含的DataGridRow
。DataGridRow
是其DataContext
设置为由该行呈现的对象的对象。在您自己的代码中,您可以将数据上下文值转换为正确的类型,并从那里访问有关该行的其他数据。编辑
实际上,在大多数普通情况下,您不需要寻找访问发送
RadioButton 的
就足够了:-DataContext
属性的拥有DataGridRow
对象因此您可以省去
Ancestors
扩展方法。然而,在更复杂的情况下,DataContext
已更改,可能需要原始的“过于复杂”的方法。The way I'd prefer to do this would be to bind
IsChecked
to a property of objects assigned to the ItemsSource. But here I'll show you the hard way to do it(Edit: Actually the following is over complicated for this scenario but I'll leave it here for now, see edits after)
First you need one of my VisualTreeEnumeration extension methods:-
Now in my testing I've just added a
ListBox
with the namelstOutput
to my Xaml. Now add the following couple of event handlers to youUserControl
:-and finally tweak the Radio button Xaml like so:-
(One of the neat things about Xaml wiring up events is that it works even when the elements are part of a Template).
You'll note that in the event handlers I'm walking up the visual tree from the sending
RadioButton
to find the containingDataGridRow
. TheDataGridRow
is the object that itsDataContext
set to the object being rendered by that row. In your own code you could cast the data context value to the correct type and from there access other data about the row.Edit
Actually in most ordinary cases you don't need to hunt down the owning
DataGridRow
object accessing theDataContext
property of the sendingRadioButton
is sufficient:-Hence you can dispense with the
Ancestors
extension method. However in more complex cases where theDataContext
have been changed the original "over-complicated" approach may be needed.