XAML 如何更新SfDataGrid控件中的特定单元格?

8iwquhpp  于 2023-09-28  发布在  其他
关注(0)|答案(2)|浏览(120)

我在我的.NET MAUI应用程序中使用了来自Syncfusion的数据网格控件,现在我想更新用户单击的单个单元格。在XAML代码中,我有:

<syncfusion:SfDataGrid x:Name="PasswdVw" ItemsSource="{Binding PassWrd}"
    SelectedRow="{Binding SelPassRws, Mode=TwoWay}" SelectionMode ="Multiple"
    NavigationMode="Row"
    HeaderRowHeight="35" CellLongPress="PasswdVw_CellLongPress">
       
    <syncfusion:SfDataGrid.Columns>
        <syncfusion:DataGridTextColumn MappingName="PassId" Visible="False"/>
        <syncfusion:DataGridTemplateColumn MappingName="PassTitle" HeaderText="Title" 
            CellPadding="3" HeaderTextAlignment="Center"  ColumnWidthMode="FitByCell">
            <syncfusion:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <local:HyperlinkLabel Text="{Binding PassTitle}"
                        BackgroundColor="White"
                        Url="{Binding PassUrl}"/>
                </DataTemplate>
            </syncfusion:DataGridTemplateColumn.CellTemplate>
        </syncfusion:DataGridTemplateColumn>

        <syncfusion:DataGridTextColumn MappingName="PassUsrname" HeaderText="User Name"
            HeaderTextAlignment="Center" ColumnWidthMode="FitByCell"/>
        <syncfusion:DataGridTextColumn MappingName="PassPassword" HeaderText="Password"
            HeaderTextAlignment="Center" Width="150"/>
        <syncfusion:DataGridTextColumn MappingName="PassUrl" Visible="False"/>
        <syncfusion:DataGridTextColumn MappingName="PassGrpName" HeaderText="Group"
            HeaderTextAlignment="Center" ColumnWidthMode="FitByCell"/>
        <syncfusion:DataGridTextColumn MappingName="PassNotes" HeaderText="Notes"
            Width="100" HeaderTextAlignment="Center"/>
    </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

我的CellLongPress事件背后的代码是:

private void PasswdVw_CellLongPress(object sender, DataGridCellLongPressEventArgs e)
{
    if(e.RowColumnIndex.ColumnIndex == 3)
    {
        var passData = e.RowData as PasswrdInfo;
        string tmpPass, newPass;
        using(SqlConnection c = new SqlConnection(App.ConnStr))
        {
            string query = "select password from PassWrds where Id = " + passData.PassId;
                
            using(SqlCommand cmd = c.CreateCommand())
            {
                c.Open();
                tmpPass = cmd.ExecuteScalar().ToString();
                c.Close();
            }
        }
    
        newPass = Crypto.Decrypt(App.secretKey, tmpPass);
            
    }
}

所以现在我想将单击的单元格设置为newPass
我尝试了PasswdVw[e.RowColumnIndex.RowIndex].Cell[3]和其他类似的东西,以及玩e.RowColumnIndex.RowIndexe.RowColumnIndex.ColumnIndex,但我只是不断得到错误。
如何将单击的单元格设置为名为newPass的变量?

bn31dyow

bn31dyow1#

根据您提供的详细信息,您遇到的问题似乎与将newPass值分配给Model类中的PassPassword属性有关。DataGridCell值依赖于要更新的模型类的属性。如果你的模型类是Observable或者实现了INotifyPropertyChanged,那么简单地将更新后的值赋给模型类中的属性就足够了。这将自动触发DataGrid中更改的反映。
代码片段:

private void PasswdVw_CellLongPress(object sender, DataGridCellLongPressEventArgs e)
{
    if(e.RowColumnIndex.ColumnIndex == 3)
    {  
        newPass = Crypto.Decrypt(App.secretKey, tmpPass);    
        passData.PassPassword = newPass;
    }
}
z0qdvdin

z0qdvdin2#

如果你的PasswrdInfo类是 observable,我们假设它看起来像这样:

public partial class PasswrdInfo : ObservableObject
{
    [ObservableProperty]
    private int passId;

    [ObservableProperty]
    private string passTitle;

    [ObservableProperty]
    private string passUsrname;

    [ObservableProperty]
    private string passPassword;

    // etc.
}

您可以简单地将newPass分配给passData.PassPassword

private void PasswdVw_CellLongPress(object sender, DataGridCellLongPressEventArgs e)
{
    if(e.RowColumnIndex.ColumnIndex == 3)
    {
        var passData = e.RowData as PasswrdInfo;
        string tmpPass, newPass;

        // skipping some code for brevity
    
        newPass = Crypto.Decrypt(App.secretKey, tmpPass);

        // if PasswrdInfo is observable, this should suffice
        passData.PassPassword = newPass;
    }
}

这将自动更新 SfDataGrid 的选定单元格中的数据。

相关问题