Silverlight获取DataGrid中选定的行数据

发布于 2024-08-20 06:33:32 字数 1042 浏览 3 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2024-08-27 06:33:32

我更喜欢的方法是将 IsChecked 绑定到分配给 ItemsSource 的对象的属性。但在这里我将向您展示实现这一目标的困难方法

编辑:实际上,对于这种情况,以下内容过于复杂,但我现在将其留在这里,请参阅之后的编辑)

首先您需要一个我的 VisualTreeEnumeration 扩展方法:-

public static class VisualTreeEnumeration
{

    public static IEnumerable<DependencyObject> Ancestors(this DependencyObject root)
    {
        DependencyObject current = VisualTreeHelper.GetParent(root);
        while (current != null)
        {
            yield return current;
            current = VisualTreeHelper.GetParent(current);
        }
    }
}

现在,在我的测试中,我刚刚将一个名为 lstOutputListBox 添加到我的 Xaml 中。现在将以下几个事件处理程序添加到您的 UserControl 中:-

    private void rdbIndataGrid_Checked(object sender, RoutedEventArgs e)
    {
        DataGridRow row = ((DependencyObject)sender).Ancestors().OfType<DataGridRow>().FirstOrDefault();
        if (row != null)
            lstOutput.Items.Add(String.Format("Checked: {0}", row.DataContext));
    }

    private void rdbIndataGrid_Unchecked(object sender, RoutedEventArgs e)
    {
        DataGridRow row = ((DependencyObject)sender).Ancestors().OfType<DataGridRow>().FirstOrDefault();
        if (row != null)
            lstOutput.Items.Add(String.Format("Unchecked: {0}", row.DataContext));
    }

最后像这样调整单选按钮 Xaml:-

<RadioButton x:Name="rdbIndataGrid" IsChecked="false" GroupName="myGroup"
    Checked="rdbIndataGrid_Checked" Unchecked="rdbIndataGrid_Unchecked"  />

(Xaml 连接事件的巧妙之处之一是,即使元素是模板的一部分)。

您会注意到,在事件处理程序中,我从发送的 RadioButton 沿着可视化树向上查找包含的 DataGridRowDataGridRow 是其 DataContext 设置为由该行呈现的对象的对象。在您自己的代码中,您可以将数据上下文值转换为正确的类型,并从那里访问有关该行的其他数据。

编辑

实际上,在大多数普通情况下,您不需要寻找访问发送 RadioButton 的 DataContext 属性的拥有 DataGridRow 对象 就足够了:-

    private void rdbIndataGrid_Checked(object sender, RoutedEventArgs e)
    {
        object myData = ((FrameworkElement)sender).DataContext;

        if (myData != null)
            lstOutput.Items.Add(String.Format("Checked: {0}", myData));
    }

    private void rdbIndataGrid_Unchecked(object sender, RoutedEventArgs e)
    {
        object myData = ((FrameworkElement)sender).DataContext;
        if (myData != null)
            lstOutput.Items.Add(String.Format("Unchecked: {0}", myData));
    }

因此您可以省去 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:-

public static class VisualTreeEnumeration
{

    public static IEnumerable<DependencyObject> Ancestors(this DependencyObject root)
    {
        DependencyObject current = VisualTreeHelper.GetParent(root);
        while (current != null)
        {
            yield return current;
            current = VisualTreeHelper.GetParent(current);
        }
    }
}

Now in my testing I've just added a ListBox with the name lstOutput to my Xaml. Now add the following couple of event handlers to you UserControl :-

    private void rdbIndataGrid_Checked(object sender, RoutedEventArgs e)
    {
        DataGridRow row = ((DependencyObject)sender).Ancestors().OfType<DataGridRow>().FirstOrDefault();
        if (row != null)
            lstOutput.Items.Add(String.Format("Checked: {0}", row.DataContext));
    }

    private void rdbIndataGrid_Unchecked(object sender, RoutedEventArgs e)
    {
        DataGridRow row = ((DependencyObject)sender).Ancestors().OfType<DataGridRow>().FirstOrDefault();
        if (row != null)
            lstOutput.Items.Add(String.Format("Unchecked: {0}", row.DataContext));
    }

and finally tweak the Radio button Xaml like so:-

<RadioButton x:Name="rdbIndataGrid" IsChecked="false" GroupName="myGroup"
    Checked="rdbIndataGrid_Checked" Unchecked="rdbIndataGrid_Unchecked"  />

(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 containing DataGridRow. The DataGridRow is the object that its DataContext 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 the DataContext property of the sending RadioButton is sufficient:-

    private void rdbIndataGrid_Checked(object sender, RoutedEventArgs e)
    {
        object myData = ((FrameworkElement)sender).DataContext;

        if (myData != null)
            lstOutput.Items.Add(String.Format("Checked: {0}", myData));
    }

    private void rdbIndataGrid_Unchecked(object sender, RoutedEventArgs e)
    {
        object myData = ((FrameworkElement)sender).DataContext;
        if (myData != null)
            lstOutput.Items.Add(String.Format("Unchecked: {0}", myData));
    }

Hence you can dispense with the Ancestors extension method. However in more complex cases where the DataContext have been changed the original "over-complicated" approach may be needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文