Silverlight如何将选定的数据网格行传递给视图模型
我的视图中有一个数据网格
<Grid x:Name="LayoutRoot" Background="White" Width="600" MaxHeight="150">
<sdk:DataGrid x:Name="grid1" ItemsSource="{Binding Persons}" SelectedItem="{Binding MyList, Mode=TwoWay}">
</sdk:DataGrid>
</Grid>
在我的 ViewModel 中,我有以下代码
private ObservableCollection<string> myList;
public ObservableCollection<string> MyList
{
get { return myList; }
set
{
myList = value;
NotifyPropertyChanged(vm => vm.MyList);
}
对于 DG,
private ObservableCollection<Person> person;
public ObservableCollection<Person> Persons
{
get { return person; }
set
{
person = value;
NotifyPropertyChanged(vm => vm.Persons);
}
}
我希望从视图中将数据网格的所选项目(行)绑定到我的视图模型中的 MyList 集合(也许 IList 与 OC ?)。我只需要所选行中的一个值,例如地址或邮政编码。但是我是否需要以另一种方式将整个集合传递到视图模型中?
例如,这是我用来填充数据网格的类,
public void CreateList()
{
Person p = new Person();
p.FirstName = "Bob";
p.LastName = "Jones";
p.Address = "123 Main Street";
p.City = "Wilmington";
p.State = "NC";
p.Zip = "12345";
Person p1 = new Person();
p1.FirstName = "Sue";
p1.LastName = "Smith";
p1.Address = "1222 Main Street";
p1.City = "Tampa";
p1.State = "FL";
p1.Zip = "12345";
Person p2 = new Person();
p2.FirstName = "Chris";
p2.LastName = "Jones";
p2.Address = "234 Water Street";
p2.City = "Dallas";
p2.State = "TX";
p2.Zip = "22222";
Person p3 = new Person();
p3.FirstName = "Andy";
p3.LastName = "Jones";
p3.Address = "434 Main Street";
p3.City = "Columbia";
p3.State = "SC";
p3.Zip = "12345";
ObservableCollection<Person> Result = new ObservableCollection<Person>();
Result.Add(p);
Result.Add(p1);
Result.Add(p2);
Result.Add(p3);
Persons = Result;
}
该类在加载视图模型时被触发。我希望能够选择第三行数据并仅将地址值传递回虚拟机。我最初的想法是将整个选定的行传递回虚拟机,然后从该集合中提取地址。
使用 selectedItem 绑定不起作用,选择一行永远不会触发虚拟机中的 inotify 事件,因此我知道集合不会被更新,除非我单击 DG,然后在其外部单击,仅传递空值。其次,与使用后面的代码提取值然后将其传递给虚拟机相比,是否有更有效的方法来做到这一点?我曾想过在视图中使用选择更改事件,然后将列值设置为传递给虚拟机的绑定字段,但这看起来很奇怪。
感谢您的任何建议或想法。
-干杯
I have a datagrid within a view
<Grid x:Name="LayoutRoot" Background="White" Width="600" MaxHeight="150">
<sdk:DataGrid x:Name="grid1" ItemsSource="{Binding Persons}" SelectedItem="{Binding MyList, Mode=TwoWay}">
</sdk:DataGrid>
</Grid>
Within my ViewModel I have the following code
private ObservableCollection<string> myList;
public ObservableCollection<string> MyList
{
get { return myList; }
set
{
myList = value;
NotifyPropertyChanged(vm => vm.MyList);
}
And for the DG
private ObservableCollection<Person> person;
public ObservableCollection<Person> Persons
{
get { return person; }
set
{
person = value;
NotifyPropertyChanged(vm => vm.Persons);
}
}
I was hoping from the View I could bind the selected item(row) of the datagrid to the MyList collection within my view model (Maybe IList vs OC?). I only need one value from the selected row such as address or zip. But do I need to pass the entire collection of can I get just one cell value into the view model another way?
For example here is the class I am using to populate the data grid
public void CreateList()
{
Person p = new Person();
p.FirstName = "Bob";
p.LastName = "Jones";
p.Address = "123 Main Street";
p.City = "Wilmington";
p.State = "NC";
p.Zip = "12345";
Person p1 = new Person();
p1.FirstName = "Sue";
p1.LastName = "Smith";
p1.Address = "1222 Main Street";
p1.City = "Tampa";
p1.State = "FL";
p1.Zip = "12345";
Person p2 = new Person();
p2.FirstName = "Chris";
p2.LastName = "Jones";
p2.Address = "234 Water Street";
p2.City = "Dallas";
p2.State = "TX";
p2.Zip = "22222";
Person p3 = new Person();
p3.FirstName = "Andy";
p3.LastName = "Jones";
p3.Address = "434 Main Street";
p3.City = "Columbia";
p3.State = "SC";
p3.Zip = "12345";
ObservableCollection<Person> Result = new ObservableCollection<Person>();
Result.Add(p);
Result.Add(p1);
Result.Add(p2);
Result.Add(p3);
Persons = Result;
}
This class is fired when the view model is loaded. I would like to be able to select the third row of data and only pass the address value back to the VM. My inital thought was to pass the entire selected row back to the vm and then extract the address from that collection.
Using the selectedItem Binding does not work, selecting a row never triggers the inotify event in the vm so I know the collection is not being updated unless I click in the DG and then outside of it which only passes a null value. Secondly, is there a more effient way to do this vs. using the code behind to pull a value and then pass it to the VM? I had thought of using selection changed event within view and then set the column value to a bound field that is passed to the vm but this seems hacky.
Thanks for any suggestions or ideas.
-cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它认为您已经使用想要“绑定所选项目”的
MyList
属性投入了一些曲线球。在其他地方,包括您自己的解决方案,您都指的是绑定到单个项目。那么为什么要有一个属性来接收列表类型呢?在我看来,您需要的是删除 MyList 并简单地拥有:-
现在您在
DataGrid
上的绑定将有意义:-您的
TextBlock
看起来像:-It think you've thrown in a bit of a curve ball with this
MyList
property to which you want to "bind the selected item". Everywhere else, including your own solution, you refer to binding to a single item. So why have a property to receive it be a list type?It seems to me that what you need is to drop the MyList and simply have:-
Now your binding on the
DataGrid
would make sense:-And your
TextBlock
would look something like:-更新
我目前采取的方法是在视图后面的代码中,我有以下内容
然后在视图中我有一个文本字段
,它绑定到 VM 中的 SelectedID 属性
这可以工作并获取 VM 的值。这在实现中看起来很奇怪(但是使用命令和命令参数的代码要少得多:))。我欢迎任何其他想法。
-干杯
Update
The current approach I am taking for this is within the code behind of the view I have the following
Then within the view I have a text field
Which is bound to the SelectedID property within the VM
This works and gets the value to the VM. This just seems wierd in implementation (but it is a lot less code that using commands and command parameters :) ) . I'd welcome any other ideas.
-cheers