DataGrid(MS.toolkit_)UWP如何获取SelectedItem

时间:2018-10-26 12:30:22

标签: c# uwp datagrid

我尽力找到如何在UWP应用程序的dataGrid(Microsoft Toolkit)中删除行。我根本找不到有关删除的任何信息。 该表是从SQL数据库填充的。我想选择一行并按Delete btn后获取所需项目的值。 我只能得到一个无用的RowNumber(或没有?)。 乞求帮助。 谢谢。

Xaml:

    <controls:DataGrid
        Grid.Row="1"
        x:Name="NotebookListDataGrid"
        Margin="12"
        VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
        HorizontalScrollBarVisibility="Auto"
        VerticalScrollBarVisibility="Visible"
        AlternatingRowBackground="Transparent"
        AlternatingRowForeground="Gray"
        AreRowDetailsFrozen="False"
        AreRowGroupHeadersFrozen="True"
        AutoGenerateColumns="False"
        CanUserSortColumns="False"
        CanUserReorderColumns="True"
        CanUserResizeColumns="True"
        ColumnHeaderHeight="32"
        MaxColumnWidth="400"
        FrozenColumnCount="0"
        GridLinesVisibility="All"
        HeadersVisibility="Column"
        IsReadOnly="False"
        ItemsSource="{x:Bind Items1.DefaultItem}"
        RowDetailsVisibilityMode="Collapsed"
        SelectedItem="{x:Bind Items1.VarSelected, Mode=TwoWay}"
        SelectionChanged="NotebookList_SelectionChanged" IsRightTapEnabled="False">
        <controls:DataGrid.Columns>
            <controls:DataGridTextColumn Header="Фамилия" Binding="{Binding Name}"/>
            <controls:DataGridTextColumn Header="Имя" Binding="{Binding Name}"/>
            <controls:DataGridTextColumn Header="Модель телефона" Binding="{Binding Phone_model}"/>
            <controls:DataGridTextColumn Header="Номер телефона" Binding="{Binding Phone_number}"/>
            <controls:DataGridTextColumn Header="E-mail" Binding="{Binding Email_adress}"/>
        </controls:DataGrid.Columns>
    </controls:DataGrid>

C#数据源:

public class Notebook_List : BindableBase
{
    public Notebook_List() => IsLoading = false;

    public ObservableCollection<Item> DefaultItem { get; private set; } = new ObservableCollection<Item>();

    private Item _VarSelected;
    public Item VarSelected
    {
        get => _VarSelected;
        set
        {
            Set(ref _VarSelected, value);
        }
    } ...

C#动作:

public Notebook_List Items1 { get; } = new Notebook_List();

private void AppBarButton_Click_2(object sender, RoutedEventArgs e) //delete button
    {
        var deleteItem = Items1;
        textbox.Text = deleteItem.VarSelected.ToString();
    }

1 个答案:

答案 0 :(得分:0)

确保您的DataGrid的ItemsSource属性绑定到 ObservableCollection ,当添加或删除项或列表本身的属性发生更改时,该通知会向UI提供更改通知。