我对 WPF 完全陌生,我想在mygrid
其中获取选定的元素,我array of contacts objects
需要访问选定的联系人并Contactid
在某些方法中使用,例如
deleteContactByID()
当用户单击删除按钮并刷新网格以隐藏行被删除时
<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>