I m completely new to WPF
i want to get selected element in mygrid
its have array of contacts objects
i need access to the selected contact and get the Contactid
to use in some methods like
deleteContactByID()
when user click the delete Button and refresh the grid to hide the row is deleted
<Window x:Class="WPFClient.ListOFContacts"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListOFContacts" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="450" Width="700" xmlns:my="clr-namespace:WPFClient.PhoneBookServiceReference" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="contactViewSource" d:DesignSource="{d:DesignInstance my:Contact, CreateList=True}" />
</Window.Resources>
<Grid DataContext="{StaticResource contactViewSource}" Name="myGrid" AllowDrop="True" ShowGridLines="False">
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="387" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="12,12,0,0" Name="contactDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="654" SelectionChanged="contactDataGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="nameColumn" Header="Name" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="emailColumn" Header="Email" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Email}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="phoneNumberColumn" Header="Phone Number" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=PhoneNumber}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="addressColumn" Header="Address" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Address}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="mobilColumn" Header="Mobil" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Mobil}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="Delete" Header="Delete" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="btnDelete">Delete</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>