I have a Combobox:
<ComboBox Height="23"
Name="DriveSelection" Width="120"
ItemsSource="{Binding Path=FixedDrives}"
DisplayMemberPath="fixedDrives"
SelectedItem="{Binding Path=DriveSelection_SelectionChanged}"
IsSynchronizedWithCurrentItem="True"/>
Here code for 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;
}
}
and here event handler:
private void DriveSelection_SelectionChanged()
{
if (page.DriveSelection.IsEnabled)
{
this.UpdatePathManager();
}
}
I checked similiar questions like this one or this one and didnt find there any answers.
I know that ViewModel is bounded to View. Other binds to buttons etc are working.
After Updates:
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" />
and binding the ViewModel:
public PathSelectionPage()
{
InitializeComponent();
this.DataContext = new PathSelectionPageViewModel(this);
}
After all thouse updates Combobox is still without any options and its greyed out.
And NotifyOfPropertyChange
is throwing does not exists in current context
and:
class PathSelectionPageViewModel : ObservableObject, INavigable, INotifyPropertyChanged
DisplayMemberPath
应该是DriveInfo
类中的属性名称,而不是DisplayMemberPath="fixedDrives"
而SelectedItem
应该是DriveInfo
类型的VM上的属性,而不是函数
Your DisplayMemberPath
Should be a property of your Collection not the Collection Itself.
From this to:
DisplayMemberPath="fixedDrives"
Something Like this:
<ComboBox Height="23"
Name="DriveSelection" Width="120"
ItemsSource="{Binding Path=FixedDrives}"
DisplayMemberPath="Property1"
SelectedItem="Property"
IsSynchronizedWithCurrentItem="True"/>
You don't need write event handler because it is nor MVVM way. You must write something like this
<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
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.