【发布时间】:2019-07-27 13:46:46
【问题描述】:
目标
CheckBoxColumn 双向绑定,用于访问用户输入。
问题描述
1.空表(所以找不到对象实例)
2. MVVM 如何知道要使用对象的哪个实例?是否有任何调试/帮助选项可以正确输入?
.xaml
<DataGrid x:Name="bGrid" ItemsSource="{Binding
Source=window.obColl_B,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,
Path=BindsDirectlyToSource}" >
<DataGrid.Columns>
<DataGridCheckBoxColumn Width="0.25*" Header="Check"
Binding="{Binding berCheck}" IsReadOnly="False" />
<DataGridTextColumn Width="0.5*" Header="Beruf"
Binding="{Binding berName}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
c#
public class obsColl : ObservableCollection<bEntry>
{
public obsColl(List<string> berList, List<string> bActive) : base()
{
for (int i = 0; i < berList.Count; i++)
{
if (bActive.Contains(berList[i]))
Add(new bEntry(true, berList[i]));
else
Add(new bEntry(false, berList[i]));
}
}
}
public class bEntry
{
private bool _berCheck;
private string _berName;
public bEntry(bool check, string name)
{
this._berCheck = check;
this._berName = name;
}
public bool berCheck
{
get { return _berCheck; }
set { _berCheck = value; }
}
public string berName
{
get { return _berName; }
set { _berName = value; }
}
}
public partial class window : Window
{
obsColl obColl_B;
public AuswahlBerufWindow(List<string> berList, List<string> berActive)
{
obColl_B = new obsColl(cpy_berList, cpy_berActive);
//so how does MVVM find the correct object to bind?
InitializeComponent();
...
}
}
// obColl_B automatically changes due to user input
据我所知。 ObservableCollection 为我处理 IPropertyChanged 和 INotifyChanged,因此我应该能够使用 Mode=TwoWay 将其与用户输入绑定。然而,即使是单个绑定也会失败。 我确实使用的想法来自 https://blogs.u2u.be/diederik/post/Codeless-two-way-data-binding-to-a-WPF-DataGrid
但是我不太明白的是如何分配正确的元素以供 MVVM 使用。 大多数示例确实使用静态输入,这无济于事。从我读到的内容来看,我需要提供相应类型(ObservableCollection)的 .xaml 解释器对象,但我不知道如何通过 Mode=TwoWay 取回这些值。
我尝试了另一种在没有绑定的情况下手动编写 DataTable 的方法,但是这种方式(仅定义类型、标题和只读)只添加了复选框的数量而没有添加内容。有趣的是,它并没有因异常而失败。
更新 1
错误 1. 绑定错误/程序失败并显示“EditItem is not allowed for this View”,并且可能已修复,如果 Path+Mode+UpdateSourceTrigger 未设置。但是,这看起来“已修复”。
更新 2
错误应该在于不正确的对象源。
【问题讨论】:
标签: wpf dynamic binding datagrid observablecollection