开发者

View is not getting updated (MVVM) but property associated is getting updated correctly

开发者 https://www.devze.com 2023-02-06 05:51 出处:网络
I am very new to WPF and working on MVVM, I have a scenario where my datagrid is not getting updated but the property to which it is bound is getting updated correctly, I am able to see new changes in

I am very new to WPF and working on MVVM, I have a scenario where my datagrid is not getting updated but the property to which it is bound is getting updated correctly, I am able to see new changes in the SlotFloorData property during PropertyChanged event.

below is XAML code (grid.row is bind to property SlotFloorData):

<Page x:Class="CasinoCAD.V6.Views.DashboardView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:vm="clr-namespace:CasinoCAD.V6.ViewModels"
      mc:Ignorable="d"
      d:DesignHeight="300" d:DesignWidth="300"
    Title="DashboardView">

    <Page.DataContext>
        <vm:CasinoCADDViewModel x:Name="viewModel"/>
    </Page.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            开发者_如何转开发<RowDefinition Height="200"/>
            <RowDefinition Height="200"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0"  Text="Dashboard View" />

        <DataGrid Grid.Row="1" Name="grdDB" ItemsSource="{Binding Path=SlotFloorData}"  />

        <!--<DataGrid Grid.Row="2" ItemsSource="{Binding Path=Properties }" />-->
   </Grid>
</Page>

I am implementing INotifyPropertyChanged in main model i.e. in CasinoCADDViewModel

and in DashboardView, I have only below (I tried INotifyPropertyChanged here too but it did not work):

Please let me know if I need to put any other information here

TIA!


CollectionChanged
I assume that you change the contents of the SlotFloorData-enumeration and that you expect to see the changes (adding and removing items). Right? If yes, the enumeration must implement some change-notification system for collectons. Try using an ObservableCollection<T>. It has is such notification built in. You also can build your on by implementing INotifyCollectionChanged.

PropertyChanged
If you change the items itselfs and want to see the changes, your items must implement INotifyPropertyChanged or the properties of the items must be DependencyProperties.

ItemsSource Reference Changed
And last there is the case where you really change the ItemsSource. This may be that you have a collection without change-notification and you add or remove items. The you set the ItemsSource to null and then reset the ItemsSource to the ancient collection. This is not nice, but will work. However if you only set the ItemsSource to the same reference without setting it to null before, the contents will not be changed because the property system sees that it is the same reference and does nothing.

I hope, that one of the above statements will lead you to the solution.


Set up as TwoWay Binding.

Example:

ItemsSource="{Binding Path=SlotFloorData, Mode=TwoWay}"

Also, make the collection an ObservableCollection type (If not already).

0

精彩评论

暂无评论...
验证码 换一张
取 消