Я новичок в WPF, так что это, вероятно, очень просто.
Следующее приложение предназначено для запуска произвольных процессов из данных в сетке.
Часть, в которой я в данный момент не уверен, - как осуществить изменение экземпляра Row
, который передается в обработчик событий с автоматической проводной связью, в частности Browse(Row row)
в модели ниже:
XAML:
<Window x:Class="LauncherApp.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cm="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock x:Name="Title" />
<DataGrid AutoGenerateColumns="False" Name="dataGrid1" ItemsSource="{Binding Source}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Run" cm:Message.Attach="Run($dataContext)" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Folder">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<WrapPanel>
<TextBox Text="{Binding Folder}" x:Name="Folder" />
<Button Content="Browse" cm:Message.Attach="Browse($dataContext)" />
</WrapPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Command}" Header="Command"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Модель:
namespace LauncherApp.ViewModels
{
using Caliburn.Micro;
using System.Collections.ObjectModel;
using System.Windows;
using System.Diagnostics;
public class ShellViewModel : PropertyChangedBase
{
private string title;
public string Title
{
get { return title; }
set
{
if (title != value)
{
title = value;
RaisePropertyChangedEventImmediately("Title");
}
}
}
public ShellViewModel()
{
Title = "Hello Caliburn.Micro";
Source = new ObservableCollection<Row>(
new[]
{
new Row {},
new Row {},
}
);
}
public void Run(Row row)
{
Process process = new Process();
process.StartInfo.FileName = row.Folder + row.Executable;
process.StartInfo.Arguments = row.Arguments;
process.Start();
}
public void Browse(Row row)
{
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
string path = dialog.SelectedPath;
row.Folder = path; // <-- THIS CHANGE DOES NOT MAKE IT BACK TO THE UI
}
public ObservableCollection<Row> Source { get; set; }
}
public class Row
{
public string Folder { get; set; }
public string Executable { get; set; }
public string Arguments { get; set; }
}
}
ОБНОВЛЕНИЕ: Вот изменения, которые я сделал, чтобы заставить его работать (благодаря EisenbergEffect)
Класс строки наследует PropertyChangeBase
public class Row : PropertyChangedBase // <-- add
{
public string Folder { get; set; }
public string Executable { get; set; }
public string Arguments { get; set; }
}
Изменить уведомление после обновления
public void Browse(Row row)
{
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
string path = dialog.SelectedPath;
row.Folder = path;
row.RaisePropertyChangedEventImmediately("Folder"); // <-- add
}