当ObservableCollection的值发生更改时,UI不会更新

时间:2014-03-02 21:35:47

标签: c# wpf xaml mvvm

我有一个名为MainWindow.xaml的窗口和一个名为Tiles.xaml的页面。

Window和Page共享名为MainWindowViewModel.cs

的ViewModel

这是MainWindowViewModel.cs

public class MainWindowViewModel : MainViewModel
{
    public MainWindowViewModel()
    {
        ERP_Lite_ServiceClient client = new ERP_Lite_ServiceClient();

        Parents = new ObservableCollection<MenuItemDTO>(client.GetAllMenuItems().Where(m => m.ParentID == null));

        if (SelectedParent != null)
        {
            Children = new ObservableCollection<MenuItemDTO>(client.GetAllMenuItems().Where(m => m.ParentID == SelectedParent.MenuItemID));
        }

        client.Close();

    }

    private ObservableCollection<MenuItemDTO> _parents;
    public ObservableCollection<MenuItemDTO> Parents
    {
        get
        {
            return _parents;
        }
        set
        {
            _parents = value;
            OnPropertyChanged("Parents");
        }
    }

    private ObservableCollection<MenuItemDTO> _children;
    public ObservableCollection<MenuItemDTO> Children
    {
        get
        {
            return _children;
        }
        set
        {
            _children = value;
            OnPropertyChanged("Children");
        }
    }

    private MenuItemDTO _selectedParent;
    public MenuItemDTO SelectedParent
    {
        get
        {
            return _selectedParent;
        }
        set
        {
            _selectedParent = value;
            OnPropertyChanged("SelectedParent");

            ERP_Lite_ServiceClient client = new ERP_Lite_ServiceClient();
            Children = new ObservableCollection<MenuItemDTO>(client.GetAllMenuItems().Where(m => m.ParentID == SelectedParent.MenuItemID));
            client.Close();
        }
    }

}

这是MainWindow.xaml

<Window.........>
    <Window.DataContext>
        <loc:MainWindowViewModel />
    </Window.DataContext>

    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" Background="#FF2A2A2A">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="50" />
                <ColumnDefinition Width="50" />
            </Grid.ColumnDefinitions>

            <ListBox Grid.Column="0" ItemsSource="{Binding Parents}" DisplayMemberPath="Title"
                     SelectedItem="{Binding SelectedParent}"
                     Height="35" FontSize="18" BorderThickness="0" Background="#FF2A2A2A" Foreground="White" SelectedIndex="0">

                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel IsItemsHost="True" Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>


                <ListBox.Resources>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                        <Setter Property="Margin" Value="5,0" />
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Foreground" Value="#FF1CB4F7" />
                                <Setter Property="FontWeight" Value="SemiBold" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="#FF87CEEB" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.Resources>

            </ListBox>

        </Grid>

        <Frame Grid.Row="1" Source="/WPF_Client;component/Pages/Tiles.xaml"/>

    </Grid>

</Window>

这是Tiles.xaml

<Page.....>

    <Page.DataContext>
        <self:MainWindowViewModel />
    </Page.DataContext>

    <ListBox ItemsSource="{Binding Children}" DisplayMemberPath="Title">


    </ListBox>

</Page>

我想我已经在MainWindowViewModel.cs上正确实现了INotifyPropertyChanged。我也使用了ObservableCollection for Children Property。但是我仍然不明白为什么当孩子们收集内容时,UI不会被通知。

1 个答案:

答案 0 :(得分:1)

您已在xaml中创建了两个MainWindowViewModel个实例。从Tiles.xaml中删除MainWindowViewModel