【发布时间】:2017-01-26 08:57:35
【问题描述】:
我有一个绑定到 ListView 控件的应用程序列表。
private List<_Application> _applicationList;
public List<_Application> applicationList
{
get { return _applicationList; }
set
{
_applicationList = value;
OnPropertyChanged();
}
}
ListView ItemTemplate 设置为按钮。
<ListView ItemsSource="{Binding applicationList}"
BorderThickness="5"
Style="{DynamicResource ListViewStyle}">
<ListView.ItemTemplate>
<DataTemplate>
<Button Command="{Binding RunCommand}"
Style="{StaticResource ApplicationButtonStyle}"
Content="{Binding name}"
Background="{Binding colorRGB}" >
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
当我单击按钮时,我希望执行应用程序。我的模型 _Application 包含一个运行该过程的 ActionCommand。
public class _Application
{
public ActionCommand RunCommand
{
get { return new ActionCommand(action => Run()); }
}
private void Run()
{
Process p = new Process();
p.StartInfo.FileName = path;
try
{
p.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public _Application()
{
}
}
我不确定,将 ActionCommand 保留在模型中是否正确?
如何在 MVVM 模式中正确实现这一点?
ActionCommand 应该放在哪里以及如何将它绑定到 Buttons 的 ListView 以便运行正确的 _Application?
【问题讨论】:
标签: c# wpf mvvm binding icommand