我有一个特定的场景。我的应用程序看起来像这样。
在左侧有一些用户列表,它是一个列表框,在右侧有几个字段是数据绑定(bind)到左侧的。它的工作原理是,如果您在右侧选择“用户 1”,用户 1 的相关信息将出现,您可以修改该信息及其与 “UpdateSourceTrigger=PropertyChanged” 的数据绑定(bind),因此它会立即反射(reflect)出来也在左边。其他用户的情况相同。
现在的问题是,如果我选择多个用户并编辑一个字段,比如字段 3,它是一个可编辑的文本框。现在,如果我选择用户 1 并编辑此文本框,它会反射(reflect)在用户 1“注意:...”中,如果我选择用户 2 并编辑字段 3,它会更新用户 2“注意:...”,但如果多选 我如何实现它?假设我想同时选择用户 1 和用户 2 并编辑注释字段它应该同时更新用户 1 和用户 2 的注释字段并且数据绑定(bind)也应该工作我的意思是它应该立即将我输入文本框的文本。有什么想法可以实现吗?
目前在我的 viewModel 中
型号
public String Note
{
get
{
return (String)GetValue(NoteProperty);
}
set { SetValue(NoteProperty, value); }
}
查看
在 XAML 中,User ListBox Items 模板是这样定义的
<TextBlock Text="{Binding Note, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
在 XAML 中,右侧文本框(字段 3)以相同方式绑定(bind)数据
<TextBox Text="{Binding Note, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
如何实现多用户数据绑定(bind)?
请帮助并给我一些想法。
编辑:
转换器:
public class MultiBindingConverter : IValueConverter
{
ObservableCollection<Info> mycollection;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var coll = (ObservableCollection<Info>)value;
mycollection = coll;
if (coll.Count == 1)
{
if (parameter.ToString() == "FNote")
return coll[0];
}
else if (coll.Count > 1)
{
// string name = coll[0].FirstName;
if (parameter.ToString() == "FNote")
{
string name = coll[0].Note;
foreach (var c in coll)
{
if (c.Note != name)
return null;
else continue;
}
return name;
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter.ToString() == "FNote")
{
foreach (var c in mycollection)
{
c.Note = value.ToString();
}
return mycollection;
}
return null;
}
}
对我来说,只有一个 TextBox Editable NoteTextBox 需要与多个用户进行数据绑定(bind)。
在我的 View 模型中
我写过
View 模型
private Command selectionChangedCommand;
public Command SelectionChangedCommand
{
get
{
if (selectionChangedCommand == null)
{
selectionChangedCommand = new Command(SelectionChanged, true);
}
return selectionChangedCommand;
}
set { selectionChangedCommand = value; }
}
public void SelectionChanged(object value)
{
selectedItem = new ObservableCollection<Info>((value as IEnumerable).OfType<Info>());
}
private ObservableCollection<Info> selectedItem;
public ObservableCollection<Info> SelectedItem
{
get { return selectedItem; }
set
{
selectedItem = value;
PropertyChanged("SelectedItem");
}
}
在Info类中有一个属性Note需要绑定(bind)到View的两个地方。
请您参考如下方法:
我完全同意@GazTheDestroyer ...这种数据绑定(bind)不能仅通过数据绑定(bind)来实现。 @Kumar 建议的是作为 POC 工作,但是当你在一个实时项目中并且你使用模型、viewModel 和 View 以及许多 UserControl 和一个 View 模型或一个 User 控件和两个 ViewModel 时,那么实现这个场景的难度是无法猜测的。
好了,不多说了。我已经做到了这一点,我将分享我是如何做到的。
一对一的 DataBinding 非常完美并且工作正常。当您选择用户 4 时,此用户 Note 字段和 Field3 Editable NoteBox 绑定(bind)到相同的属性,因此它可以完美运行。
多选时说先选User4,再选User3和user1,我在代码后面写了多选时的逻辑项目被选中 注意 文本为空。这不反对 MVVM 作为基于某些 View 标准更新 View 并没有破坏 MVVM 模式。所以现在当可编辑文本框更新为一些文本时 user4 属性在 viewModel 中更新。现在困难的部分是更新其他选定的用户。这是将更新所选用户的代码,并将如我提到的那样反射(reflect) Mode="TwoWay", UpdateSourceTriger="PropertyChanged"
if (listUser.SelectedItems.Count > 1)
{
for (int i = 0; i < listUser.SelectedItems.Count; i++)
{
Info info = listUser.SelectedItems[i] as Info;
info.Note = (string)tbNote.Text;
}
}
以这种方式,Editable note textbox 的值在所有用户的属性 Note Property 中更新,并且由于绑定(bind)是双向的,它将反射(reflect)在其他用户中也是如此。
可能有很多方法可以解决它,但我发现了这种方法并且效果很好,所以我想我会回答我自己的问题。
