[英]MVVM DataBinding
I've started an MVVM project and now I'm stucking with correct DataBinding. 我已经开始了MVVM项目,现在我坚持使用正确的DataBinding。 My project has:
我的项目有:
A UserControl whit a ViewModel as DataContext like: 一个UserControl带有一个ViewModel作为DataContext,例如:
public partial class TestUserControl: UserControl
{
public TestUserControl()
{
this.DataContext = new TestUserControlViewModel();
}
}
ViewModel code is (BaseViewModel class contains PropertyChangedEventHandler): ViewModel代码为(BaseViewModel类包含PropertyChangedEventHandler):
public class TestUserControlViewModel : BaseViewModel
{
public KrankenkasseControlViewModel()
{}
public IEnumerable<DataItem> GetAllData
{
get
{
IGetTheData src= new DataRepository();
return src.GetData();
}
}
}
IGetTheData is the interface to DataContext: IGetTheData是DataContext的接口:
public interface IGetTheData
{
IEnumerable<DataItem> GetData();
}
} }
and finally the DataRepository code: 最后是DataRepository代码:
public class DataRepository : IGetTheData
{
private TestProjectDataContext dax = new TestProjectDataContext();
public IEnumerable<DataItem> GetData()
{
return (from d in this.dax.TestData
select new DataItem
{
ID = d.ID,
SomeOtherData = d.SomeOtherData
});
}
}
My UserControl has a few TextBoxes, but what's the best way to bind correctly? 我的UserControl有几个TextBoxes,但是正确绑定的最佳方法是什么?
Thanks for your help, regards. 谢谢您的帮助,问候。
EDIT: Binding the data against multiple textboxes 编辑:对多个文本框绑定数据
After reading your comment, I will elaborate my example for textboxes. 阅读您的评论后,我将详细说明我的文本框示例。
First important thing is that the ViewModel will model the things in the View, so that the View gets all information it needs in the structure it needs. 首先重要的是ViewModel将对View中的事物进行建模,以便View在所需的结构中获取所需的所有信息。 That means, if you have multiple textboses in the View, you will need multiple string Properties in your ViewModel, one for each textbox.
这意味着,如果视图中有多个文本框,则视图模型中将需要多个字符串属性,每个文本框一个。
In your XAML you could have something like 在您的XAML中,您可能会遇到类似
<TextBox Text="{Binding ID, Mode=TwoWay}" />
<TextBox Text="{Binding SomeOtherData, Mode=TwoWay}" />
and in your ViewModel 并在您的ViewModel中
public class TestUserControlViewModel : BaseViewModel {
private string id;
private string someOtherData;
public TestUserControlViewModel() {
DataItem firstItem = new DataRepository().GetData().First();
this.ID = firstItem.ID;
this.SomeOtherData = firstItem.SomeOtherData;
}
public string ID {
get {
return this.id;
}
set {
if (this.id == value) return;
this.id = value;
this.OnPropertyChangedEvent("ID");
}
}
public string SomeOtherData {
get {
return this.someOtherData;
}
set {
if (this.someOtherData == value) return;
this.someOtherData = value;
this.OnPropertyChangedEvent("SomeOtherData");
}
}
}
Here I assume that in your BaseViewModel
there is an OnPropertyChangedEvent
method to fire the corresponding event. 在这里,我假设您的
BaseViewModel
有一个OnPropertyChangedEvent
方法来触发相应的事件。 This tells the View that the property has changed and it must update itself. 这告诉View属性已更改,并且必须对其进行自我更新。
Note the Mode=TwoWay
in the XAML. 请注意XAML中的
Mode=TwoWay
。 This means, that it doesn't matter on which side the value changes, the other side will reflect the change immediately. 这意味着,值更改的哪一侧都无所谓,另一侧将立即反映更改。 So if the user changes a value in a
TwoWay
bound TextBox
, then the corresponding ViewModel property will automatically change! 因此,如果用户在
TwoWay
绑定的TextBox
更改值,则相应的ViewModel属性将自动更改! And also vice versa: if you change the ViewModel property programmatically, the View will refresh. 反之亦然:如果以编程方式更改ViewModel属性,则视图将刷新。
If you want to show multiple textboxes for more than one data item, then you must introduce more Properties in the ViewModel and bind them accordingly. 如果要为多个数据项显示多个文本框,则必须在ViewModel中引入更多属性,并进行相应的绑定。 Maybe a
ListBox
with a flexible number of TextBox
es inside is a solution then, like @Haspemulator already answered. 那么也许里面有灵活数量的
TextBox
的ListBox
是一个解决方案,就像@Haspemulator已经回答了。
Binding the data against a collection control 将数据绑定到集合控件
In the TestUserControl
I guess you have a control (like a ListView
) to show the list of loaded things. 在
TestUserControl
我想您有一个控件(如ListView
)来显示已加载内容的列表。 So bind that control against the list in the ViewModel with 因此,将控件与ViewModel中的列表绑定在一起
<ListView ... ItemsSource="{Binding GetAllData}" ... />
First you must understand that Binding means not "read the data and then forget the ViewModel". 首先,您必须了解Binding的意思不是“读取数据,然后忘记ViewModel”。 Instead you bind the View to the ViewModel (and its Properties) as long as the View lasts.
相反,只要View持续存在,就将View绑定到ViewModel(及其属性)。 From this point of view,
AllData
is a much better name than GetAllData
(thanks @Malcolm O'Hare). 从这个角度来看,
AllData
比GetAllData
(感谢@Malcolm O'Hare)。
Now in your code, every time the View reads the AllData
property, a new DataRepository
is created. 现在在您的代码中,每次View读取
AllData
属性时,都会创建一个新的DataRepository
。 Because of the Binding, that is not what you want, instead you want to have one instance of DataRepository
for the whole lifetime of the View, which is used to read the initial data and can later be used to update the View, if the underlying database changes (maybe with an event). 由于绑定的原因,这不是您想要的,而是希望在View的整个生命周期中都拥有一个
DataRepository
实例,该实例用于读取初始数据,并且可以在以后用于更新View(如果基础)数据库更改(可能带有事件)。
To enable such a behavior you should change the type of the AllData
property to an ObservableCollection
, so that the View can automatically update the list if changes occur. 要启用这种行为,您应该将
AllData
属性的类型更改为ObservableCollection
,以便在发生更改时View可以自动更新列表。
public class TestUserControlViewModel : BaseViewModel
private ObservableCollection<DataItem> allData;
public TestUserControlViewModel() {
IGetTheData src = new DataRepository();
this.allData = new ObservableCollection<DataItem>(src.GetData());
}
public ObservableCollection<DataItem> AllData {
get {
return this.allData;
}
}
public void AddDataItem(DataItem item) {
this.allData.Add(item);
}
}
Now if you call AddDataItem
later, the ListView will update itself automatically. 现在,如果以后再调用
AddDataItem
,ListView将自动更新。
Your Property Name is bad. 您的财产名称不正确。 You should call it AllData, not GetAllData.
您应该将其命名为AllData,而不是GetAllData。
Since you are returning a collection, you probably should be using some sort of list control (ListBox, ListView). 由于要返回集合,因此可能应该使用某种列表控件(ListBox,ListView)。
In that case you'd be doing 那样的话你会做
<ListBox ItemsSource="{Binding GetAllData}" />
Guten Abend. Guten Abend。 :) As it already mentioned, since you're returning the collection, it's better to use a ListBox.
:)如前所述,由于您要返回集合,因此最好使用ListBox。 The comment about having ObservableCollection as a cache is also absolutely valid.
关于将ObservableCollection作为缓存的评论也绝对有效。 I would add that if you need to have your data editable, you should use TextBox inside the ItemTemplate:
我要补充一点,如果需要编辑数据,则应在ItemTemplate内使用TextBox:
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text={Binding SomeOtherData,Mode=TwoWay} />
</DataTemplate>
</ListBox.ItemTemplate>
In this case if user edits the text in the box, data will be updated in your data object, so that it could be saved in the database later. 在这种情况下,如果用户在框中编辑文本,则数据将在您的数据对象中更新,以便以后可以将其保存在数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.