Cześć, bym to zrobił tak:
DataGrid w oknie głównym:
<Window x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid x:Name="dataGrid"
SelectionUnit="Cell"
CanUserAddRows="False"
CanUserDeleteRows="False"
SelectionMode="Single"
IsReadOnly="True" MouseDoubleClick="dataGrid_MouseDoubleClick"
/>
</Grid>
</Window>
oraz Codebehind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
dataGrid.ItemsSource = new List<Person>()
{
new Person(){Id = "1", Name = "Piotr", Nick = "Strzała"},
new Person(){Id = "2", Name = "Jan", Nick = "Kowal"},
new Person(){Id = "3", Name = "Kuba", Nick = "Ponury"},
new Person(){Id = "4", Name = "Michał", Nick = "Wolny"}
};
}
private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource is TextBlock)
{
var textBlok = e.OriginalSource as TextBlock;
Window window = new EditWindow(textBlok.Text);
window.ShowDialog();
if (window.Tag != null && window.Tag is string)
{
textBlok.Text = window.Tag.ToString();
}
}
}
}
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
public string Nick { get; set; }
}
W oknie edycji mam textbox oraz buttona, a codebehind:
public partial class EditWindow : Window
{
public EditWindow(string text)
{
InitializeComponent();
box.Text = text;
}
private void save_Click(object sender, RoutedEventArgs e)
{
Tag = box.Text;
Close();
}
}