我無法重現您的錯誤。它工作正常。在我看來,你在視圖模型的屬性處有一些不正確的初始化。查看MVVM ListView的工作示例。
型號:
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public bool IsClickable { get; set; }
}
視圖模型:
public class MainWindowViewModel:INotifyPropertyChanged
{
private ObservableCollection<Person> _persons=new ObservableCollection<Person>();
public ObservableCollection<Person> Persons
{
get
{
return _persons=GetData();
}
set
{
_persons = value;
OnPropertyChanged("Persons");
}
}
public ObservableCollection<Person> GetData()
{
myDataList.Add(new Person() { ID = 1, Name = "Person1", Author = "Author1", Price = "6.7 TL", Catalog = "IT", IsClickable=true});
myDataList.Add(new Person() { ID = 2, Name = "Person2", Author = "Author2", Price = "9.7 TL", Catalog = "IT", IsClickable = false});
myDataList.Add(new Person() { ID = 3, Name = "Person3", Author = "Author3", Price = "11.7 TL", Catalog = "IT", IsClickable = true});
myDataList.Add(new Person() { ID = 2, Name = "Person4", Author = "Author2", Price = "9.7 TL", Catalog = "IT", IsClickable = true});
myDataList.Add(new Person() { ID = 3, Name = "Person5", Author = "Author3", Price = "11.7 TL", Catalog = "IT", IsClickable = false});
if (myDataList.Count > 0)
{
return myDataList;
}
else
return null;
}
RelayCommand _clickCommand = null;
public ICommand SomeClickCommand
{
get
{
if (_clickCommand == null)
{
_clickCommand = new RelayCommand((p) => OnClick(p), (p) => CanClick(p));
}
return _clickCommand;
}
}
private bool CanClick(object obj)
{
return true;
}
private void OnClick(object obj)
{
MessageBox.Show("You clicked:)");
}
#region OnPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("propertyName"));
}
#endregion
}
查看你的特點:
<Window x:Class="WpfApplication1.MainWindow"
...
xmlns:local="clr-namespace:WpfApplication1.ViewModel"
Title="MainWindow" Height="550" Width="525">
<Window.Resources>
<local:MainWindowViewModel x:Key="mainWindowViewModel"/>
</Window.Resources>
<StackPanel DataContext="{StaticResource mainWindowViewModel}">
<ListView ItemsSource="{Binding Persons}">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" Background="LightGreen" Margin="1"/>
<TextBlock Text="{Binding Author}" Background="LightCyan" Margin="1"/>
<TextBlock Text="{Binding TimeGap}" Background="LightCoral" Margin="1"/>
<CheckBox IsChecked="{Binding IsClickable}" CommandParameter="{Binding}" Command="{Binding DataContext.SomeClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" Margin="1"/>
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</Window>
講究我已將DataContext
綁定到StackPanel
。所以我應該輸入StackPanel
爲AncestorType
:
<CheckBox IsChecked="{Binding IsClickable}" CommandParameter="{Binding}" Command="{Binding DataContext.SomeClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" Margin="1"/>
你從哪裏得到這個錯誤?這是一個編譯錯誤,還是你在調試輸出窗口中看到它?我無法用你的代碼重現這一點。 – Jens
您的datacontext在哪裏定義? – Tomtom
用'ListView.Resources'替換'ListView.ItemContainerStyle' – Tomtom