簡體   English   中英

動態更新綁定的DataGrid

[英]Updating Binded DataGrid Dynamically

我正在嘗試將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>

任何幫助將不勝感激,謝謝!

也許這有助於將您的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%確定此解決方案是否適合您的問題,但這就是我解決了向視圖模型中的可觀察集合添加新項的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM