0

我对 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>
4

1 回答 1

2

您应该将SelectedItemDataGrid 的属性绑定到 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为 a ,ViewModel那么您需要SelectedContact在 ViewModel 类中拥有一个属性,就像您可能对其他属性所做的那样。

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

然后在您的处理程序中,您只需要访问 SelectedContact 属性即可获取选定的联系人。

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

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

于 2012-05-23T11:09:55.270 回答