我有一个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 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);
}
经过所有的更新后,Combobox仍然没有任何选项,并且显示为灰色。
NotifyOfPropertyChange
正在投掷does not exists in current context
和
class PathSelectionPageViewModel : ObservableObject, INavigable, INotifyPropertyChanged
答案 0 :(得分:1)
您的DisplayMemberPath
应该是DriveInfo
课程中的媒体资源名称而不是DisplayMemberPath="fixedDrives"
,而SelectedItem
应该是DriveInfo
类型的虚拟机上的属性,而不是{{1}}功能
答案 1 :(得分:1)
你的DisplayMemberPath
应该是你收藏的财产而不是收藏本身。
从此到:
DisplayMemberPath="fixedDrives"
像这样:
<ComboBox Height="23"
Name="DriveSelection" Width="120"
ItemsSource="{Binding Path=FixedDrives}"
DisplayMemberPath="Property1"
SelectedItem="Property"
IsSynchronizedWithCurrentItem="True"/>
答案 2 :(得分:1)
您不需要编写事件处理程序,因为它不是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
}
}
}