我如何在我的网格中获取所选项目

时间:2012-05-23 11:02:05

标签: wpf grid

我是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>

1 个答案:

答案 0 :(得分:2)

您应该将DataGrid的SelectedItem属性绑定到DataContext类型中的属性。在删除处理程序上,您只需访问类型的属性即可获取所选联系人。在你的情况下:

<DataGrid SelectedItem={Binding SelectedContact} 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">

如果您已将DataContext设置为ViewModel,那么您需要在ViewModel类中拥有属性SelectedContact,就像对其他属性一样。

public Contact SelectedContact
{
    get; set; // Calling the NotifyPropertyChanged 
}

然后在您的处理程序中,您只需访问SelectedContact proeprty即可获得所选联系人。

void handler()
{
    var selectedContact = SelectedContact;
    var contactId = selectedContact.ContactId; 
}

我希望这可以解除你的疑虑。