MVVM Prism 5 WPF和组合框

时间:2015-11-09 18:16:46

标签: c# wpf mvvm prism

当我在另一个组合框中进行选择时,我不明白为什么我的组合框不会更新。我还是MVVM的新手,但理论上我的代码应该可行。我可以在表单加载时填充组合,但我需要使用新值刷新组合,但这不起作用。我可以看到它检索新值,但它从不在表单上显示它们。

我的XAML看起来像这样:

<ComboBox Grid.Column="1" ItemsSource="{Binding Path=Vendors}" SelectedItem="{Binding SelectedVendor, Mode=TwoWay}"   HorizontalAlignment="Left" Margin="24,12,0,11" Grid.Row="3" VerticalAlignment="Center" Width="293" />
<ComboBox x:Name="VendorProductServiceCB" HorizontalAlignment="Left" Margin="20.6,16.2,0,55.4" VerticalAlignment="Center" Width="293" Grid.Row="7" Grid.Column="1" ItemsSource="{Binding Path=VendorProductServices, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedVendorProductService, Mode=TwoWay}" Height="22"/>

我的ViewModel代码是这样的:

public ObservableCollection<string> Vendors { get; set; } 
public ObservableCollection<VendorProductService> VendorProductServices { get; private set; }

public VendorProductService SelectedVendorProductService
    {
        get { return _selectedVendorProductService; }
        set { SetProperty(ref _selectedVendorProductService, value); }
    }

public string SelectedVendor
    {
        get { return _selectedVendor; }
        set
        {
            SetProperty(ref _selectedVendor, value);            
            SelectionChangedCommand.Execute(this);

        }               
    }

public FormSelectionViewModel()
    {
        Vendors = new ObservableCollection<string>(FetchVendors());

        VendorProductServices = new ObservableCollection<VendorProductService>(FetchVendorProductServices(_selectedVendor));  

        SelectionChangedCommand = new DelegateCommand(SelectionChanged);
    }
public  void SelectionChanged()
    {

        VendorProductServices = new ObservableCollection<VendorProductService>(FetchVendorProductServices(_selectedVendor)); 

    }

1 个答案:

答案 0 :(得分:0)

您没有提出PropertyChanged的{​​{1}}事件,因为它是一个自动财产。

将其更改为:

VendorProductServices

或者将您的集合属性更改为只读并使用.Clear()和.Add()而不是创建新集合。