English 中文(简体)
如何在网格中获取选中的项目?
原标题:how i can get the selected item in my grid
  • 时间:2012-05-23 11:02:05
  •  标签:
  • wpf
  • grid

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>
问题回答

您应该将 DataGrid 的 Sective 属性绑在您数据格式类型中的属性上。在删除处理器上,您只需访问您的类型属性即可获得选中的联系人。 在您的情况下 :

<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 , 那么您就需要在您的视图模式类中拥有一个属性 < code> SectiveContact , 正如您可能对其它属性所做的那样 。

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

然后在您的处理器中,您只需要访问选中的 Contact proeprty 即可获得选中的联系人 。

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

我希望这能消除你们的疑虑。





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签