【发布时间】: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 one 或 this 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_SelectionChanged和FixedDrives是PathSelectionPageViewModel类的属性吗?此外,NotifyOfPropertyChange应使用属性名称调用,因此NotifyOfPropertyChange(() => DriveSelection_SelectionChanged)应解决 在当前上下文中不存在 问题 -
是的。他们都在
PathSelectionPageViewModel班级。更改NotifyOfPropertyChange不会改变任何东西。仍然说 NotifyOfPropertyChange 在当前上下文中不存在