【问题标题】:How can I make the RadioButtons on my DataGrid stay usable after sorting?排序后如何使 DataGrid 上的 RadioButtons 保持可用?
【发布时间】:2012-11-04 14:50:18
【问题描述】:

我有一个 DataGrid,我正在使用代码创建和填充。当网格第一次显示 RadioButton 列时,“Include”似乎工作得很好。问题是,在我更改排序后 RadioButton 不再工作 正确。你可以点击它,点击窗口外然后再回来,或者事件只是等待,它最终会起作用,但显然它不是真的可用。这是一个简单的例子:

using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            InitializeDataGrid();
        }

        private void InitializeDataGrid()
        {
            var table = GetTestTable();

            var newGrid = new DataGrid
                              {
                                  CanUserAddRows = false,
                                  CanUserSortColumns = true,
                                  AutoGenerateColumns = true,
                                  ItemsSource = table.AsDataView(),
                                  DataContext = table
                              };
            newGrid.AutoGeneratingColumn += dataGrid_AutoGeneratingColumn;

            UxRootGrid.Children.Add(newGrid);
        }

        private static DataTable GetTestTable()
        {
            DataTable table = new DataTable("name");
            table.Columns.Add("Include", typeof (bool));
            table.Columns.Add("Number", typeof (int));

            for (int i = 0; i < 5; i++)
            {
                var row = table.NewRow();
                row[0] = false;
                row[1] = i;
                table.Rows.Add(row);
            }
            return table;
        }

        void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.PropertyName != "Include")
            {
                return;
            }

            DataTemplate template = GetDataTemplate();
            DataGridTemplateColumn templateColumn = new DataGridTemplateColumn
                                                        {
                                                            Header = "Include",
                                                            CellTemplate = template,
                                                            CellEditingTemplate = template,
                                                        };
            e.Column = templateColumn;
        }

        private static DataTemplate GetDataTemplate()
        {
            var elementFactory = new FrameworkElementFactory(typeof (RadioButton));

            var binding = new Binding("Include")
                              {
                                  Mode = BindingMode.TwoWay,
                                  UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                              };

            elementFactory.SetBinding(ToggleButton.IsCheckedProperty, binding);
            elementFactory.SetValue(RadioButton.GroupNameProperty, "BindingGroup");

            return new DataTemplate {VisualTree = elementFactory};
        }
    }
}

这是 Xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Name="UxRootGrid">
    </StackPanel>
</Window>

所以,看问题:
1.启动应用程序
2. 单击数字 1 旁边的 RadioButton。
3. 单击“数字”标题两次(按降序排序)。
4. 现在单击数字 4 旁边的 RadioButton。
5. 请注意,现在取消选择数字 1 旁边的 RadioButton,但未选择数字 4 旁边的 RadioButton。
6. 单击数字 4 旁边的 RadioButton 几次。最终它会被选中。

更新:我让我的另一位同事在他的机器上尝试了这个,他花了一到 2 次排序/选择单选按钮才能看到问题发生,但最终他的位置和我一样。

我对 WPF 很陌生,所以我希望这只是我的一个简单错误。对于获得解决方法或其他设置方法的任何帮助,我们将不胜感激。

【问题讨论】:

  • 您是否检查过与“包含”的绑定是否有效?在我看来,问题就在那里......
  • 我没有看到任何绑定错误(至少我的输出中没有出现错误)。我假设绑定至少部分有效,因为如果您更改 row[0] = false;到行 [0] = true;然后你会注意到它正确地填充了最后一个 RadioBox。
  • @Bob,我尝试将更新布局作为排序的一部分,但这似乎没有帮助。您建议在哪里尝试添加?
  • @Dusan,我在课堂上添加了一个私有成员来跟踪我创建的 DataTable。然后我向 elementFactory 添加了一个事件处理程序,以便我可以处理 CheckedEvent。在事件中,我刚刚验证了 DataTable 正在正确更新。此外,当您对列进行排序时,该值不会丢失,这表明该值已正确写入。问题只是排序后你很快就失去了选择新值的能力。
  • 你在哪里更新布局?你应该从整体上看。所以 MainWindow.UpdateLayout() 或任何最高级别。还要检查 Visual Studio 中的“输出”选项卡,以确保它不是绑定或其他问题。

标签: c# wpf sorting datagrid radio-button


【解决方案1】:

我能够通过使用 Peter Staev 的 RadioButtonExtended 类来解决问题,该类位于:http://pstaev.blogspot.com/2008/10/binding-ischecked-property-of.html

正如他在帖子中建议的那样,我只是将 RadioButton 的用法替换为 RadioButtonExtended,然后绑定到 IsCheckedReal 而不是 已检查。

【讨论】:

    猜你喜欢
    • 2010-12-15
    • 1970-01-01
    • 2023-01-07
    • 2015-08-27
    • 1970-01-01
    • 2014-01-20
    • 2017-09-19
    • 1970-01-01
    • 2020-05-15
    相关资源
    最近更新 更多