幫助。我一直在尋找谷歌和stackoverflow搜索欄一個星期,沒有找到我的答案。WPF綁定到屬性列表不更新
所以我有一個名爲學生類,屬性是字符串名稱,字符串地址,DoB DateofBirth和列表得分。以及Student類的ViewModel類。
public partial class StudentWindow : Window
{
public class DoB
{
public int Day { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public DoB(int day, int month, int year)
{
Day = day;
Month = month;
Year = year;
}
public override string ToString()
{
return Day + "/" + Month + "/" + Year;
}
}
public class Student
{
public string Name { get; set; }
public string Address { get; set; }
public DoB DateOfBirth { get; set; }
public List<int> Score { get; set; }
}
public class ViewModel : INotifyPropertyChanged
{
private Student entry;
public event PropertyChangedEventHandler PropertyChanged;
public void Notify(string Property)
{
PropertyChanged(this, new PropertyChangedEventArgs(Property));
}
public string Name
{
get { return entry.Name; }
set { entry.Name = value; Notify("Name"); }
}
public string Address
{
get { return entry.Address; }
set { entry.Address = value; Notify("Address"); }
}
public string DateOfBirth
{
get { return entry.DateOfBirth.ToString(); }
set
{
var p = value.Split('/');
int d = 1, m = 1, y = 1;
bool pass = int.TryParse(p[0], out d) && int.TryParse(p[1], out m) && int.TryParse(p[2], out y);
if (pass)
{
entry.DateOfBirth = new DoB(d, m, y);
Notify("DateOfBirth");
}
else
throw new InvalidCastException();
}
}
public List<string> Score
{
get { return entry.Score.Select(sc => "" + sc).ToList(); }
set { entry.Score = value.Select(va => int.Parse(va)).ToList(); Notify("Score"); }
}
public ViewModel(Student entry)
{
this.entry = entry;
}
}
public ObservableCollection<ViewModel> entry { get; set; }
public StudentWindow()
{
InitializeComponent();
entry = new ObservableCollection<ViewModel>();
entry.Add(new ViewModel(new Student() { Name = "First People", Address = "Earth", DateOfBirth = new DoB(13, 11, 1996), Score = new List<int>(new int[] { 100, 90, 100, 90 }) }));
entry.Add(new ViewModel(new Student() { Name = "Second People", Address = "Moon", DateOfBirth = new DoB(13, 11, 1995), Score = new List<int>(new int[] { 90, 80, 100, 100 }) }));
DataContext = this;
}
}
而XAML是
<Grid>
<DataGrid x:Name="dataGridStudent" ItemsSource="{Binding entry}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="210" Width="572"/>
<DataGrid x:Name="dataGridStudentScore" ItemsSource="{Binding ElementName=dataGrid, Path=SelectedItem.Score}" HorizontalAlignment="Left" Margin="10,242,0,0" VerticalAlignment="Top" Height="218" Width="572" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
一切工作正常進行dataGridStudent。
但dataGridStudentScore只顯示值並且無法編輯該值。我必須設置綁定路徑=。 ,否則會拋出Path或Xpath異常。
請幫助你有任何解決方案。我是新手,請告訴我的方法是否有問題。提前致謝。
使用DataTemplate中,而不是datagridTextColumn – Sajeetharan
「請你告訴我們,如果事情是錯我的方法」 - 你殺了MVVM。 「entry = new ObservableCollection();」 - 不不不。請先學習基本的MVVM。 –
niksofteng