1
votes

I'm trying to bind a DataGrid with an ObservableCollection list. I basically add a few items to the list (which should add rows to the datagrid) at the start of the form then update the columns dynamically using this code in a different thread:

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

If I update the DataGrid this way it works but lags the UI thread:

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

I've tried using the PropertyChanged event but the DataGrid isn't populating in the first place so I'm not sure if that's working properly. Here is my data class:

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")); }
    }
}

Here is how i'm creating my 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
}
}

And here is my 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>

Any help would be greatly appreciated, thanks!

1

1 Answers

0
votes

maybe it helps to bind your itemsource not to an observable collection but to a 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();
}
}

in your xaml you have to set

ItemsSource="{Binding UserdataCollectionView }"

not 100% sure if this solution fits your problem but that's how I solved adding new items to my observable collection in my viewmodel.