MVVM可编辑ComboBox绑定 [英] MVVM Editable ComboBox Bindings
问题描述
这是我的问题...我有一个服务器列表,每个服务器都有一个ID和ServerName.我希望能够从ComboBox中选择一台服务器并在适当位置对其进行编辑,然后使其ID可用于稍后通过SQL更新.因此,假设这是数据:(ID = 1,名称="Server1"),(ID = 2,名称="Server2"),(ID = 3,名称="Server3").如果我从ComboBox中选择Server3,我想将其编辑为"Server4",然后使用SQL查询将其上传(我知道该如何做).我正在使用MVVM,因此所有值都是我的ViewModel的属性.
Here's my issue...I have a list of servers, each with an ID and ServerName. I want to be able to select a server from the ComboBox and edit it in place, then have its ID available to update via SQL later. So let's say this is the data: (ID=1, Name="Server1"), (ID=2, Name="Server2"), (ID=3, Name="Server3"). If I select Server3 from the ComboBox, I'd like to edit it to be "Server4" then upload that with a SQL query (I know how to do this part). I'm utilizing MVVM, so all the values are properties of my ViewModel.
当前,当在ComboBox中修改文本字段时,SelectedServer立即变为null,大概是因为它不再是它可以识别的值.我可以使用一些指导来了解如何使它完成我想做的事情.
Currently, when the text field is modified in the ComboBox the SelectedServer immediately becomes null, presumably because it is no longer a value it recognizes. I could use some guidance on how to get this to do what I'm trying to do.
<ComboBox Grid.Column="1" x:Name="serverNameUpdateBox" HorizontalAlignment="Stretch" Height="23" VerticalAlignment="Center" IsEditable="True"
ItemsSource="{Binding Path=DataContext.SelectedProjectServers, ElementName=main}"
DisplayMemberPath="ServerName"
SelectedValue="{Binding SelectedServer}"
SelectedValuePath="ServerName"
Text="{Binding SelectedServer.ServerName, UpdateSourceTrigger=LostFocus}"
/>
与ViewModel相关的代码:
And ViewModel relevant code:
namespace ViewModel
{
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
SelectedProjectServers = new List<Server>();
SelectedServer = new Server();
private Server _selectedServer;
public Server SelectedServer
{
get { return _selectedServer; }
set
{
if (value == null) { ModifiedServer = _selectedServer; }
_selectedServer = value;
RaisePropertyChanged("SelectedServer");
}
}
private List<Server> _selectedProjectServers;
public List<Server> SelectedProjectServers
{
get { return _selectedProjectServers; }
set
{
_selectedProjectServers = value;
RaisePropertyChanged();
}
}
}
}
}
与模型相关的代码:
namespace Model
{
public class Server : INotifyPropertyChanged
{
private string _serverName;
public string ServerName
{
get { return _serverName; }
set
{
_serverName = value;
RaisePropertyChanged();
}
}
private int _serverID;
public int ServerID
{
get { return _serverID; }
set
{
_serverID = value;
RaisePropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string caller = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(caller));
}
}
}
推荐答案
将"EditedServerName"之类的属性绑定到Combobox.Text.更改"EditedServerName"后,可以将值设置为SelectedServer的"ServerName".
Bind a property like "EditedServerName" to Combobox.Text. When the "EditedServerName" is changed you can set the value to the "ServerName" of your SelectedServer.
<ComboBox Grid.Column="1" x:Name= "serverNameUpdateBox" HorizontalAlignment= "Stretch" Height= "23" VerticalAlignment= "Center" IsEditable= "True"
ItemsSource= "{Binding Path=DataContext.SelectedProjectServers, ElementName=main}"
DisplayMemberPath= "ServerName"
SelectedItem="{Binding SelectedServer}"
Text= "{Binding EditedServerName, UpdateSourceTrigger=LostFocus}"
/>
这篇关于MVVM可编辑ComboBox绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!