0
我是WPF的完整初學者,所以這可能很簡單。如何獲得按鈕點擊到列模板中的文本框的結果(Caliburn Micro)
以下應用程序將從網格中的數據啓動任意進程。
我目前不清楚的是如何在下面的模型影響的變化傳遞到自動有線事件處理程序情況下,特別是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; }
}
}
更新:這是我所做的更改,使其工作(感謝EisenbergEffe CT)
Row類更新後
public class Row : PropertyChangedBase // <-- add
{
public string Folder { get; set; }
public string Executable { get; set; }
public string Arguments { get; set; }
}
更改通知繼承PropertyChangeBase
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
}