【问题标题】:Combobox ItemSource not binding with data from ViewModel组合框 ItemSsource 未与 ViewModel 中的数据绑定
【发布时间】:2016-12-17 17:43:16
【问题描述】:

我有一个组合框:

<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 绑定到 View。其他与按钮等的绑定正在工作。

更新后:

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" />

并绑定 ViewModel:

public PathSelectionPage()
{
    InitializeComponent();
    this.DataContext = new PathSelectionPageViewModel(this);
}

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

NotifyOfPropertyChange 正在抛出does not exists in current context 和:

class PathSelectionPageViewModel : ObservableObject, INavigable, INotifyPropertyChanged

【问题讨论】:

  • DriveSelection_SelectionChangedFixedDrivesPathSelectionPageViewModel 类的属性吗?此外,NotifyOfPropertyChange 应使用属性名称调用,因此 NotifyOfPropertyChange(() =&gt; DriveSelection_SelectionChanged) 应解决 在当前上下文中不存在 问题
  • 是的。他们都在PathSelectionPageViewModel 班级。更改 NotifyOfPropertyChange 不会改变任何东西。仍然说 NotifyOfPropertyChange 在当前上下文中不存在

标签: c# wpf mvvm combobox


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您的 DisplayMemberPath 应该是您的集合的属性,而不是集合本身。

    从这里到:

     DisplayMemberPath="fixedDrives"
    

    类似这样的:

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

    【讨论】:

    • 好的,我检查了 FixedDrives[0] 有属性 Name 所以我检查了 DisplayMemberPathDisplayMemberPath="Name" 并且它仍然是空的
    • @szpic 将您的 ViewModel 发布到您绑定数据的位置!
    【解决方案3】:

    您不需要编写事件处理程序,因为它也不是 MVVM 方式。你必须写这样的东西

       <ComboBox Height="23" 
            Name="DriveSelection" Width="120"
            ItemsSource="{Binding Path=FixedDrives}"
            DisplayMemberPath="PropertyOfDriveInfo"
            SelectedItem="{Binding Path=SelectedInfo}" />
    
    class ViewModel: INotifyPropertyChanged
    {
        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 DriveInfo _selectedInfo
        public DriveInfo SelectedInfo
        {
            get { return _electedInfo; }
            set
            {
                if (value == _electedInfo) return;
                _selectedInfo= value;
                NotifyOfPropertyChange(() => SelectedInfo);//must be implemented
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      相关资源
      最近更新 更多