I generate datagrid content from the list. I need to be able to pass the value of the ‘Name’ cell of the selected row in the datagrid to a texblock of the child window. The child windows is launched by clicking the context menu item. The scenario is the the user right click the mouse over selected row “name’ cell, then pick the context menu item that launch the childwindow. ChildWindow is launched with the textblock text value from that cell from the datagrid. Any ideas are highly appreciated! Thank you in advance!
Datagrid with context menu:
<sdk:DataGrid x:Name="dgPack" ItemsSource="{Binding Source={StaticResource PackagesCollectionViewSource}}">
<InputToolkit:ContextMenuService.ContextMenu>
<InputToolkit:ContextMenu x:Name="cmPackages_DataGrid" DataContext="{Binding Source={StaticResource GlobalLabelsDataSource}}">
<InputToolkit:MenuItem x:Name="item1" Header="item1"/>
<InputToolkit:MenuItem x:Name="GetChildWindow" Header="GetChildWindow" Click="GetChildWindow_Click"/>
</InputToolkit:ContextMenu>
</InputToolkit:ContextMenuService.ContextMenu>
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="DataGrid_Name" Binding="{Binding Name}" Header="Name" />
<sdk:DataGridTextColumn x:Name="DataGrid_Version" Binding="{Binding Version}" Header="Version" />
<sdk:DataGridTextColumn x:Name="DataGrid_Size" Binding="{Binding Size}" Header="Size" />
</sdk:DataGrid.Columns>
Method in code behind to launch childwindow:
private void GetChildWindow_Click(object sender, System.Windows.RoutedEventArgs e)
{
var selectedItem = PackVM.SelectedPack;
if (selectedItem != null)
{
var bpw = new GetChildWindow1_ChildWindow(selectedItem.Id);
bpw.Show();
}
}
List:
public List<Pack> GetCollection()
{
return new List<Pack>()
{
new Pack() { Name="item1", Size=10, Version="1"},
new Pack() { Name="item2", Size=12, Version="5", },
};
}
And finally child window:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" Margin="0,0,79,0" HorizontalAlignment="Right" Grid.Row="1" />
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Grid.Row="1" />
<TextBlock x:Name="Message" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
</Grid>
I don't know if I've understand correctly your question but I think you can solve it this way:
-Declare a public method in the child window code behind like "SetText(string text)"
-In this method, just set Message.Text = text
public void SetText(string text)
{
this.Message.Text = text;
}
-Now, call this method before calling show method from parent window:
bpw.SetText(selectedItem.PropertyWithTheText);
This should work.
Anyway, you should take a look to MVVM to solve this kind of stuff ;-)