简体   繁体   中英

Datagrid Binding Using MVVM WPF

I'm facing an issue in datagrid binding using mvvm on two different places. Headers of my datagrid (xaml) is:

 <DataGrid Grid.Row="0" Grid.Column="0" AlternationCount="2" Background="White" RowHeight="28" HorizontalGridLinesBrush="Lavender" VerticalGridLinesBrush="Lavender"
           VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="True"
           ScrollViewer.CanContentScroll="True" Name="MainGrid" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Visible" 
           HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="False"
           CanUserDeleteRows="False" CanUserResizeRows="True" ItemsSource="{Binding Path=Configurations, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

which clealy says

 ItemsSource="{Binding Path=Configurations, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

In my viewmodel:

public ObservableCollection<ViewerConfiguration> Configurations
{
    get { return m_tasks; }
    set { m_tasks = value; OnPropertyChanged("Configurations"); }
}

The data that in the list is shown properly on the view but the problem is with the insert and delete (even it updates successfully). I have a function which inserts an item in the Configuration object

private void Refresh()
{
    List<ViewerConfiguration> newlyAddedFiles = GetConfigurations();
    foreach (ViewerConfiguration config in newlyAddedFiles)
    {
        Configurations.Add(config);
    }
}

and removes like:

 Configurations.Remove(configuration);

The problem is really with the insert and delete. On debugging there is no exception and it successfully deletes from collection too but UI doesn't get a notification. Any guesses why is this behavior?

Additionally: I have an event trigger just under the datagrid:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <i:InvokeCommandAction Command="{Binding RenderPdf}" CommandParameter="{Binding SelectedItem, ElementName=MainGrid}"></i:InvokeCommandAction>
    </i:EventTrigger>
</i:Interaction.Triggers>

and I'm calling Refresh function from the constructor of ViewModel just to see if it works or not.

Finally I solved the issue i was having. There were basically two problems. In the getter of my Configurations, i was return a new observable collection after sorting and filtering the previous one. The second main problem:

 'This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread'

That was my best guess and i changed my refresh function to:

 private void Refresh()
    {
        try
        {
            List<ViewerConfiguration> newlyAddedFiles = GetConfigurations();
            //should not update on other thread than on main thread
            App.Current.Dispatcher.BeginInvoke((ThreadStart)delegate()
            {
                foreach (ViewerConfiguration config in newlyAddedFiles)
                {
                    Configurations.Add(config);
                }
            });
        }
        catch (Exception ex)
        {

        }
    }

Now, its working. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM