c# - DataGrid 的数据绑定(bind)不起作用?

标签 c# wpf xaml data-binding datagrid

我正在使用在简单 WPF 应用程序的 MainWindow 类中定义的 ObservableCollection。由于某种原因,列表会更新(我可以在调试时看到这一点),但 UI 不会更新。

现在,如果我创建一个 Timer 并循环它,我可以通过设置 ItemSource 来更新 DataGrid。这可行,但会导致我的 DataGrid 中出现可怕的闪烁。

public ObservableCollection<CalculatedData> calculatedData { get; set; }

进一步深入我的代码,我使用这一行来实际更新数据或将数据添加到列表中。

calculatedData = await CalculateData();

CalculateData 函数定义如下:

private Task<ObservableCollection<CalculatedData>> CalculateData()
{
    return Task.Run(() =>
    {
        ObservableCollection<CalculatedData> cdList = new ObservableCollection<CalculatedData>();
        // Do a lot of stuff
        return cdList;
    });
}

对于我的 xaml,我有一个简单的 DataGrid,如下所示:

<DataGrid Name="dataGrid" ItemsSource="{Binding calculatedData}" IsReadOnly="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn" />

问题:为什么 DataGrid 没有更新?由于我没有重新绑定(bind),这会解决我的闪烁问题吗?

-

更新

我什至将我的主要声明更改为以下内容(因为我看到它是这样做的),但我仍然没有让它工作。

private ObservableCollection<CalculatedData> calculatedData = new ObservableCollection<CalculatedData>();
public ObservableCollection<CalculatedData> CalculatedData
{
    get { return calculatedData; }
    set
    {
        calculatedData = value;
    }
}

XAML:

<DataGrid Name="dataGrid" ItemsSource="{Binding CalculatedData}" IsReadOnly="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn" />

最佳答案

如果你只是这样做:

    private async void Window_Loaded(object sender, RoutedEventArgs e)
    {
        calculatedData = await CalculateData();
        dataGrid1.ItemsSource = calculatedData;
    }

它会起作用的。我猜您的 XAML 绑定(bind)可能无法正常工作,因为 DataContext 未定义。

编辑:这是完整的代码,

主窗口:

public partial class MainWindow : Window
{
    public ObservableCollection<CalculatedData> calculatedData { get; set; }

    public MainWindow()
    {
        InitializeComponent();
    }

    private async void Window_Loaded(object sender, RoutedEventArgs e)
    {
        calculatedData = await CalculateData();
        dataGrid1.ItemsSource = calculatedData;
    }

    private Task<ObservableCollection<CalculatedData>> CalculateData()
    {
        return Task.Run(() =>
        {
            ObservableCollection<CalculatedData> cdList = new ObservableCollection<CalculatedData>();

            // Do a lot of stuff
            cdList.Add(new CalculatedData { Data1 = 1, Data2 = 2, Data3 = 3 });
            cdList.Add(new CalculatedData { Data1 = 1, Data2 = 2, Data3 = 3 });
            cdList.Add(new CalculatedData { Data1 = 1, Data2 = 2, Data3 = 3 });
            cdList.Add(new CalculatedData { Data1 = 1, Data2 = 2, Data3 = 3 });
            cdList.Add(new CalculatedData { Data1 = 1, Data2 = 2, Data3 = 3 });

            return cdList;
        });
    }
}

XAML:

<Grid>
    <DataGrid x:Name="dataGrid1" Margin="0" />
</Grid>

结果:

enter image description here

编辑 2:添加一个按钮并将代码稍微移动一下,以便您可以测试。

public partial class MainWindow : Window
{
    public ObservableCollection<CalculatedData> calculatedData { get; set; }
    int i;

    public MainWindow()
    {
        InitializeComponent();

        //initialize your binding collection
        calculatedData = new ObservableCollection<CalculatedData>();
    }

    private async void Window_Loaded(object sender, RoutedEventArgs e)
    {
        i = 1;
        //create initial data and bind to data grid
        calculatedData = await CalculateData();
        dataGrid1.ItemsSource = calculatedData;
    }

    private Task<ObservableCollection<CalculatedData>> CalculateData()
    {
        return Task.Run(() =>
        {
            ObservableCollection<CalculatedData> cdList = new ObservableCollection<CalculatedData>();

            // Do a lot of stuff
            for (int j = 0; j < 5; j++)
            {
                cdList.Add(new CalculatedData { Data1 = i, Data2 = i + 1, Data3 = i + 2 });
                i++;
            }

            return cdList;
        });
    }
    private async void button1_Click(object sender, RoutedEventArgs e)
    {
        //place new data in a temporary collection
        ObservableCollection<CalculatedData> newData = await CalculateData();

        //add new data to the collection bound to the data grid
        //preferably, don't just replace it
        //any business logic you may need for adding, 
        //deleting, filtering data, etc goes here 
        foreach (CalculatedData cd in newData)
            calculatedData.Add(cd);
    }
}

关于c# - DataGrid 的数据绑定(bind)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768551/

相关文章:

c# - WPF——绑定(bind)不工作

wpf - 如何禁用某些按钮的加速键

c# - 无法转换类型为 'System.Data.EnumerableRowCollection` 的对象 1[System.Int32 ]' to type ' System.IConvertible'

c# - 与 Google 代码的持续集成(C# 应用程序)

c# - ASP.NET 成员身份按用户角色重定向到页面

wpf - 从 WPF 应用程序引用 Silverlight 类库时出错

c# - 堆栈跟踪中的敏感信息

C# 使用 LiveCharts 将图表转为图像

c# - 从 ViewModel 了解数据绑定(bind)

c# - 非事件项目的列表框系统颜色?