[英]How to bind Selected Items in MVVM
I am using WPF
, MVVM
and DevExpress
GridControl
. 我正在使用
WPF
, MVVM
和DevExpress
GridControl
。 There are two panels in my MainWindow.xaml. 我的MainWindow.xaml中有两个面板。 Panle1 has Grid and Panel2 has
Textbox
. Panle1具有网格,而Panel2具有
Textbox
。 I want that if i select an item from Grid in Panel1 it's name should display in that Panle2 Textbox. 我希望,如果我从Panel1的网格中选择一个项目,则其名称应显示在Panle2文本框中。 Iwrote Code but it is not working.
编写代码,但无法正常工作。 Can you Please help me to solve this?
您能帮我解决这个问题吗?
*In NameModel From Models Folder I wrote: * *在NameModel from Models文件夹中,我写道: *
private NameModelClass _selectedCustomer;
public NameModelClass SelectedCustomer
{
get { return _selectedCustomer; }
set
{
if (_selectedCustomer != value)
{
_selectedCustomer = value;
LastName = value.LastName;
OnPropertyChanged("SelectedCustomer");
}
}
public List<Namess> ListPerson { get; set; }
void CreateList()
{
ListPerson = new List<Namess>();
for (int i = 0; i < 10; i++)
{
ListPerson.Add(new Namess(i));
}
}
public class Namess
{
public Namess(int i)
{
FirstName = "FirstName" + i;
LastName = "LastName" + i;
Age = i * 10;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
}
In MianWindow.xaml I wrote: 我在MianWindow.xaml中写道:
<dxdo:LayoutPanel Caption="Grid" Caption="Panel1" x:Name="abc1">
<Grid>
<dxg:GridControl x:Name="grid" Height="233" ItemsSource="{Binding ListPerson}" AutoGenerateColumns="AddNew" HorizontalAlignment="Left" VerticalAlignment="Top" SelectedItem="{Binding SelectedNames}">
<dxg:GridControl.View>
<dxg:TableView ShowTotalSummary="True"/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</dxdo:LayoutPanel>
<dxdo:LayoutPanel Caption="Panel2" x:Name="abc1">
<TextBox Width="166" Background="White" Height="33" HorizontalAlignment="Right" VerticalAlignment="Bottom" Text="{Binding Path=LastName}"/>
</dxdo:LayoutPanel>
I am new to MVVM
and c#. 我是
MVVM
和c#的新手。 I f my query is not clear to you please ask me. 如果您不清楚我的询问,请问我。 Thank you.
谢谢。
I do it this way: 我这样做:
private Namess _selectedCustomer;
public Namess SelectedCustomer
{
get { return _selectedCustomer; }
set
{
if (_selectedCustomer != value)
{
_selectedCustomer = value;
OnPropertyChanged("SelectedCustomer");
}
}
public List<Namess> ListPerson { get; set; }
void CreateList()
{
ListPerson = new List<Namess>();
for (int i = 0; i < 10; i++)
{
ListPerson.Add(new Namess(i));
}
}
public class Namess : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
public Namess(int i)
{
FirstName = "FirstName" + i;
LastName = "LastName" + i;
Age = i * 10;
}
public string FirstName { get; set; }
private string _lastName;
public string LastName
{
get
{
return _lastName;
}
set
{
if(value==_lastName)
return;
_lastName=value;
OnPropertyChanged("LastName");
}
}
public int Age { get; set; }
}
}
and in your view: 并且在您看来:
<dxdo:LayoutPanel Caption="Grid" Caption="Panel1" x:Name="abc1">
<Grid>
<dxg:GridControl x:Name="grid" Height="233" ItemsSource="{Binding ListPerson}" AutoGenerateColumns="AddNew" HorizontalAlignment="Left" VerticalAlignment="Top" SelectedItem="{Binding SelectedNames,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<dxg:GridControl.View>
<dxg:TableView ShowTotalSummary="True"/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</dxdo:LayoutPanel>
<dxdo:LayoutPanel Caption="Panel2" x:Name="abc1">
<TextBox Width="166" Background="White" Height="33" HorizontalAlignment="Right" VerticalAlignment="Bottom" Text="{Binding Path=SelectedCustomer.LastName,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
</dxdo:LayoutPanel>
Bsically I changed the type of SelectedCustomer to one of the collection of items. 基本上,我将SelectedCustomer的类型更改为项目集合之一。 In the view you can set the binding of your TextBox directly to a property of the SelectedCustomer.
在视图中,您可以将TextBox的绑定直接设置为SelectedCustomer的属性。
Have you tried: 你有没有尝试过:
SelectedItem="{Binding SelectedNames, Mode=TwoWay}" SelectedItem =“ {绑定SelectedNames,Mode = TwoWay}”
After looking at it more, your main Namess Class could do with implementing INotifyPropertyChanged 看了更多之后,您的主要Namess类可以实现INotifyPropertyChanged
With each property raising the property changed event when it ahem changes. 随着每个属性提升属性更改事件时, 咳咳变化。
Also using an observable collection so when you add and remove items it also raises changes. 还使用可观察的集合,因此在添加和删除项目时也会引起更改。
That way, the notification change system receives the notify of property changes to change the view accordingly via bindings. 这样,通知更改系统通过绑定接收属性更改的通知,以相应地更改视图。
It looks like you forgot to raise the INPC (INotifyPropertyChanged) event for the "LastName" string. 您似乎忘记了为“ LastName”字符串引发INPC(INotifyPropertyChanged)事件。
So try this (changed is in the setter below): 因此,请尝试以下操作(已在以下设置方法中进行了更改):
public NameModelClass SelectedCustomer
{
get { return _selectedCustomer; }
set
{
if (_selectedCustomer != value)
{
_selectedCustomer = value;
LastName = value.LastName;
OnPropertyChanged("SelectedCustomer");
OnPropertyChanged("LastName"); //<-- new
}
}
}
You have to send out INPCs so that the binding knows to update to the new value. 您必须发出INPC,以便绑定知道更新为新值。 The displayed binding won't "grab" the new value for LastName unles you raise that event.
显示的绑定不会“抓住”您引发该事件的姓氏的新值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.