[英]Binding problems using MVVM with WPF
我是C#的新手。 我在裝訂上遇到麻煩。 我將在此處留下代碼示例,並希望您能幫助我找到麻煩。
好友檢視
<UserControl x:Class="WpfWHERE.View.FriendsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfWHERE.View"
xmlns:ViewModel="clr-namespace:WpfWHERE.ViewModel"
xmlns:data = "clr-namespace:WpfWHERE.Model"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800">
<UserControl.DataContext>
<ViewModel:FriendsViewModel/>
</UserControl.DataContext>
<UserControl.Resources><DataGrid x:Key="friendsList" AutoGenerateColumns="false" ItemsSource = "{Binding Student}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cbName" SelectedItem="{Binding Path=FullName, Mode=OneWay}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" DisplayMemberPath="Name">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
FriendsViewModel
public class FriendsViewModel:AViewModel
{
#region fields
public DelegateCommand DeleteCommand { get; set; }
#endregion fields
#region constructors
public FriendsViewModel()
{
LoadStudents();
DeleteCommand = new DelegateCommand(OnDelete, CanDelete);
}
#endregion constructors
public ObservableCollection<Student> Students
{
get;
set;
}
public void LoadStudents()
{
ObservableCollection<Student> students = new ObservableCollection<Student>();
students.Add(new Student { FirstName = "Mark", LastName = "Allain" ,Place = "Home"});
students.Add(new Student { FirstName = "Allen", LastName = "Brown", Place = "China" });
students.Add(new Student { FirstName = "Linda", LastName = "Hamerski", Place = "Je" });
Students = students;
}
private Student _selectedStudent;
public Student SelectedStudent
{
get
{
return _selectedStudent;
}
set
{
_selectedStudent = value;
DeleteCommand.RaiseCanExecuteChanged();
}
}
private void OnDelete()
{
Students.Remove(SelectedStudent);
}
private bool CanDelete()
{
return SelectedStudent != null;
}
}
學生
public class StudentModel { }
public class Student : INotifyPropertyChanged
{
private string firstName;
private string lastName;
private string place;
public string FirstName
{
get { return firstName; }
set
{
if (firstName != value)
{
firstName = value;
RaisePropertyChanged("FirstName");
RaisePropertyChanged("FullName");
}
}
}
public string LastName
{
get { return lastName; }
set
{
if (lastName != value)
{
lastName = value;
RaisePropertyChanged("LastName");
RaisePropertyChanged("FullName");
}
}
}
public string Place
{
get { return place; }
set
{
if (place != value)
{
place = value;
RaisePropertyChanged("Place");
}
}
}
public string FullName
{
get
{
return firstName + " " + lastName;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
我真的不知道我在做什么錯,我用谷歌搜索並堆積了一些,但無法使其正常工作。 希望您能分享知識並為我提供幫助。
最好的祝福,
注意注釋中的2個更改
a)使用ItemsSource = "{Binding Students}
而不是ItemsSource = "{Binding Student}
b)使用語法為<TextBlock Text = "{Binding Path = FullName, Mode = OneWay}" ></TextBlock>
以下為FriendsView
修改的代碼應提供所需的DataGrid
結果。 經過測試並最終完成工作,以顯示一個帶有顯示FullName
的單列的DataGrid。
<UserControl x:Class="WpfWHERE.View.FriendsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfWHERE.View"
xmlns:ViewModel="clr-namespace:WpfWHERE.ViewModel"
xmlns:data = "clr-namespace:WpfWHERE.Model"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800">
<UserControl.DataContext>
<ViewModel:FriendsViewModel/>
</UserControl.DataContext>
<UserControl.Resources>
<DataGrid x:Key="friendsList" AutoGenerateColumns="false" ItemsSource = "{Binding Students}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text = "{Binding Path = FullName, Mode = OneWay}" ></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</UserControl.Resources>
<ContentControl Content="{StaticResource friendsList}"/>
</UserControl>
請注意:
1)因為不確定您如何顯示它,所以我已經在ContentControl
顯示了DataGrid
。
2)我不確定您的AViewModel
有什么,但不會影響結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.