【问题标题】:Updating Binded DataGrid Dynamically动态更新绑定的 DataGrid
【发布时间】:2018-09-25 00:12:41
【问题描述】:

我正在尝试将 DataGrid 与 ObservableCollection 列表绑定。我基本上在表单的开头向列表中添加了一些项目(应该向数据网格中添加行),然后在不同的线程中使用此代码动态更新列:

UserDataGrid.Dispatcher.BeginInvoke(new Action(() =>
            {
                UserDataGridCollection[m_iID].Status = Log;
                UserDataGridCollection[m_iID].ID = m_iID;
                UserDataGridCollection[m_iID].User = m_sUser;
            }

如果我以这种方式更新 DataGrid,它可以工作,但会滞后于 UI 线程:

                UserDataGrid.ItemsSource = null;
                UserDataGrid.ItemsSource = UserDataGridCollection;

我已经尝试使用 PropertyChanged 事件,但 DataGrid 没有首先填充,所以我不确定它是否正常工作。这是我的数据类:

public class UserDataGridCategory : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private int id;
    private string user, status;

    public int ID
    {
        get { return id; }
        set { id = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ID")); }
    }
    public string User
    {
        get { return user; }
        set { user = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("User")); }
    }
    public string Status
    {
        get { return status; }
        set { status = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Status")); }
    }
}

这是我创建 ObservableCollection 的方式:

static class UserEngine
{
public static ObservableCollection<UserDataGridCategory> UserDataGridCollection { get; set; }
public static object _lock = new object();

public static void RunEngine(DataGrid UserDataGrid)
{
     UserDataGridCollection = new ObservableCollection<UserDataGridCategory>();
     BindingOperations.EnableCollectionSynchronization(UserDataGridCollection, _lock);

     // Some other  code

     // Spawn thread to invoke dispatcher and do some other stuff
}
}

这是我的 xaml:

<DataGrid Name="UserDataGrid" x:FieldModifier="public" ItemsSource="{Binding UserDataGridCollection}" SelectedItem="{Binding SelectedRow, Mode=TwoWay}" Margin="10,16,22.6,215" AutoGenerateColumns="False" IsReadOnly="True">
                    <DataGrid.Resources>
                        <ContextMenu x:Key="RowMenu" Focusable="False"
        DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                            <MenuItem Header="View Log" Click="ViewLog" Focusable="False"/>
                        </ContextMenu>
                    </DataGrid.Resources>
                    <DataGrid.RowStyle>
                        <Style TargetType="DataGridRow" >
                            <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
                        </Style>

                    </DataGrid.RowStyle>
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="ID #" Binding="{Binding ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="User" Binding="{Binding User, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" Width="150"/>
                        <DataGridTextColumn Header="Status" Binding="{Binding Status,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" Width="250"/>
                    </DataGrid.Columns>
                </DataGrid>

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: c# wpf multithreading datagrid observablecollection


    【解决方案1】:

    也许将你的 itemsource 绑定到一个可观察的集合而不是 CollectionViewSource 会有所帮助。

    public class UserEngine {
    public ICollectionView UserdataCollectionView { get; set; }
    
    public UserEngine()    { 'constructor
        UserdataCollectionView = CollectionViewSource.GetDefaultView(UserDataGridCollection );
    }
    
    public void addnewLinetoOC(){
        // after you add a new entry into your observable collection 
        UserdataCollectionView .Refresh();
    }
    }
    

    你必须在你的 xaml 中设置

    ItemsSource="{Binding UserdataCollectionView }"
    

    不是 100% 确定此解决方案是否适合您的问题,但这就是我解决在我的视图模型中将新项目添加到我的可观察集合的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      相关资源
      最近更新 更多