我的应用程序与模型视图ViewModel一起工作。在我的模型中,我有一个基于客户机类的列表。
public class Client
{
public string Name { get; set; }
public string Ip { get; set; }
public string Mac { get; set; }
}在我的ClientRepository中,我用我的客户机类从XML文件中生成一个列表。
public ClientRepository()
{
var xml = "Clients.xml";
if (File.Exists(xml))
{
_clients = new List<Client>();
XDocument document = XDocument.Load(xml);
foreach (XElement client in document.Root.Nodes())
{
string Name = client.Attribute("Name").Value;
string Ip = client.Element("IP").Value;
string Mac = client.Element("MAC").Value;
_clients.Add(new Client() { Mac = Mac, Name = Name, Ip = Ip });
}
}
}在我的UI/UX中,我有3个用于MAC的Textboxes 1、1个IP和1个名称,还有一个按钮,它具有绑定到AddClientCommand的功能。
<Label Grid.Row="0" Grid.Column="0" Content="Host Name:"/>
<TextBox Grid.Row="0" Grid.Column="1" x:Name="tbHostName" Height="20" Text="{Binding Path=newClient.Name, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="1" Grid.Column="0" Content="IP Address:"/>
<TextBox Grid.Row="1" Grid.Column="1" x:Name="tbIP" Height="20" Text="{Binding Path=newClient.Ip, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="2" Grid.Column="0" Content="MAC Address"/>
<TextBox Grid.Row="2" Grid.Column="1" x:Name="tbMAC" Height="20" Text="{Binding Path=newClient.Mac, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Row="3" Grid.Column="0" Content="Remove" x:Name="bRemove" Margin="3 0 3 0" Click="bRemove_Click"/>
<Button Grid.Row="3" Grid.Column="1" Content="Add" x:Name="bAdd" Margin="3 0 3 0" Click="bAdd_Click" Command="{Binding AddClientCommand}"/>为了说明我的观点:,我想知道的是实现AddClientCommand?的最佳方法是什么?
我现在拥有的--我知道它不起作用--
public ClientViewModel()
{
_repository = new ClientRepository();
_clients = _repository.GetClients();
WireCommands();
}
private void WireCommands()
{
AddClientCommand = new RelayCommand(AddClient);
}
public Client newClient
{
get
{
return _newClient;
}
set
{
_newClient = value;
OnPropertyChanged("newClient");
AddClientCommand.isEnabled = true;
}
}
public void AddClient()
{
_repository.AddClient(newClient);
}RelayCommand类:
public class RelayCommand : ICommand
{
private readonly Action _handler;
private bool _isEnabled;
public RelayCommand(Action handler)
{
_handler = handler;
}
public bool isEnabled
{
get { return true; }
set
{
if (value != isEnabled)
{
_isEnabled = value;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
public bool CanExecute(object parameter)
{
return isEnabled;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_handler();
}
}发布于 2014-01-16 09:17:22
我建议您使用DelegateCommands,您可以在许多MVVM框架中找到这个类:
public ICommand AddClientCommand
{
get
{
return new DelegateCommand(AddClient, CanExecuteAddClient);
}
}我还看到_clients是List<Client>类型的。如果要将其绑定到UI以查看客户端列表,则除非使用ObservableCollection<Client>,否则将不会通知更改。
编辑:正如有人在评论中指出的那样,您应该创建_newClient。请注意为每个添加的客户端创建一个新实例,否则您将最终一次又一次地添加相同的Client实例!
发布于 2014-01-16 09:08:19
你刚刚试过把你的命令放到这样的财产里吗?
public ICommand AddClientCommand
{
get { return new RelayCommand(AddClient, CanAddClient); }
}
public bool CanAddClient()
{
return newClient != null;
}在CanAddClient中放置任何您想要的逻辑,以启用或禁用ICommand。
啊啊..。我明白了。RelayCommand的实现是错误的。您需要使用CanExecuteChanged事件处理程序的.您可以在RelayCommand.cs页面中找到GitHub上的正确实现。
https://stackoverflow.com/questions/21156934
复制相似问题