c# - 组合框 ItemSsource 未与 ViewModel 中的数据绑定(bind)

标签 c# wpf mvvm combobox

我有一个组合框:

<ComboBox   Height="23" 
            Name="DriveSelection" Width="120"
            ItemsSource="{Binding Path=FixedDrives}"
            DisplayMemberPath="fixedDrives"
            SelectedItem="{Binding Path=DriveSelection_SelectionChanged}"
            IsSynchronizedWithCurrentItem="True"/>

这里是 ItemsSource 的代码:
private ObservableCollection<DriveInfo> fixedDrives;
public ObservableCollection<DriveInfo> FixedDrives
{
    get
    {
        if(fixedDrives==null)
        {
           var query =
                from driveInfo in DriveInfo.GetDrives()
                //where driveInfo.DriveType == DriveType.Fixed
                select driveInfo;
           fixedDrives= new ObservableCollection<DriveInfo>(query);
           return fixedDrives;
        }

        return fixedDrives;
    }
}

和这里的事件处理程序:
private void DriveSelection_SelectionChanged()
{
    if (page.DriveSelection.IsEnabled)
    {
        this.UpdatePathManager();
    }
}

我查了类似的问题 like this onethis one并没有找到任何答案。

我知道 ViewModel 绑定(bind)到 View。其他绑定(bind)到按钮等正在工作。

更新后:
private DriveInfo driveSelection;
public DriveInfo DriveSelection_SelectionChanged
{
    get
    {
        return driveSelection;
    }
    set
    {
        if (value == driveSelection) return;
        driveSelection = value; 
        NotifyOfPropertyChange(() => UpdatePathManager()); //UpdatePatchmanager is my function and it exists.
        //Notify... throws does not exists in currenct context
    }
}

XAML:
<ComboBox  Height="23" 
           Name="DriveSelection" 
           Width="120" 
           ItemsSource="{Binding Path=FixedDrives}" 
           DisplayMemberPath="Name" 
           SelectedItem="{Binding Path=DriveSelection_SelectionChanged}" 
           IsSynchronizedWithCurrentItem="True" />

并绑定(bind) ViewModel:
public PathSelectionPage()
{
    InitializeComponent();
    this.DataContext = new PathSelectionPageViewModel(this);
}

在所有 thouse 更新之后,Combobox 仍然没有任何选项并且它是灰色的。

NotifyOfPropertyChange正在 throw does not exists in current context和:
class PathSelectionPageViewModel : ObservableObject, INavigable, INotifyPropertyChanged

最佳答案

您的 DisplayMemberPath应该是您的 DriveInfo 中的属性名称类而不是 DisplayMemberPath="fixedDrives"SelectedItem应该是 DriveInfo 类型的 VM 上的属性不是函数

关于c# - 组合框 ItemSsource 未与 ViewModel 中的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075906/

相关文章:

c# - 在 XAML 中指定自定义转换器

c# - 如何将更改从模型获取到模型 View 中?

c# - WPF DataGrid 删除 IEditableCollectionView.CancelNew() 上的 NewItemPlaceholder

c# - 如何在 C#/IL 中改变盒装值类型(原始或结构)

c# - 属性和方法之间的歧义

c# - C#中StrLikeText()方法如何比较 '#'的字符串?

c# - .NET Core 依赖注入(inject)——多个项目

c# - Listbox ItemTemplate Selector 不选择模板

wpf - 从 WPF 3.5 移动到 4.0 会导致错误 "Can' t put a page in a Style"

android - 来自房间数据库的数据不会显示在回收站 View 中。可能出了什么问题?