我有一个组合框:
<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 绑定(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/