DataGrid的DataBinding无法正常工作?

时间:2015-08-02 04:26:55

标签: c# wpf xaml data-binding datagrid

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

现在,如果我创建Timer并将其循环播放,我可以通过设置DataGrid来更新ItemSource。这可行,但在我的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没有被提升?这会解决我的闪烁问题,因为我没有重新绑定吗?

-

更新

我甚至将我的主要声明改为以下(因为我看到它以这种方式完成),但我仍然没有让它发挥作用。

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" />

2 个答案:

答案 0 :(得分:2)

如果您只是这样做:

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

它会起作用。我猜你的XAML绑定可能无法正常工作,因为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);
    }
}

答案 1 :(得分:0)

每次尝试从CalculateData();

的结果添加项目时,您都要替换项目源
var data = await CalculateData();
foreach(var d in data)
   calculatedData.Add(d);

也不确定你是否在构造函数中初始化了CalculatedData属性init。

CalculatedData = new ObservableCollection<yourType>();

让我知道这是否有效。