繁体   English   中英

DataGrid列自定义绑定

[英]DataGrid Column Custom Binding

我很新,在以下问题上确实很挣扎:

  1. 我有一个下面的列,可以正确加载数据:

      <xcdg:Column Title="TestData" FieldName="TestData" Width="1*" > 
  2. 但是我需要通过从弹出窗口中选择一个值来对其进行修改。

    我之前已经解决过类似的问题,但这是针对文本框的:

      <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Model.TestData}"> <TextBox.InputBindings> <KeyBinding Key="F12" Command="{Binding SearchCommand}" CommandParameter="TestData"></KeyBinding> </TextBox.InputBindings> </TextBox> 

不知道是否需要有关该弹出窗口的更多信息,基本上它会返回选定的值,该值应该到达该列。

您可以简单地将新值分配给适当的单元格。

DataRowView drv = myDataGrid.CurrentItem as DataRowView;
drv["FieldName"] = "New Value";

在下面显示的示例中,我使用一个具有ID,Name和Result属性的弹出窗口。 它将显示提供的ID和名称。 用户可以编辑名称(我将ID字段设置为只读),然后单击“确定”或“取消”按钮。 两者都将关闭弹出窗口,但是只有单击“确定”按钮,才会在关闭前将Result属性设置为true。 然后,我可以检查此Result属性,以了解是否应更新网格中的单元格。

private void btnShowPopup_Click(object sender, RoutedEventArgs e)
{
    // Get the grid's current row
    DataRowView drv = this.myGrid.CurrentItem as DataRowView;

    // Popup shows product's ID and Name (user can only edit Name)
    Popup popup = new Popup();
    popup.ProductID = drv["ProductID"].ToString();
    popup.ProductName = drv["ProductName"].ToString();
    popup.ShowDialog();

    // Apply changes if user clicked the OK button
    if (popup.Result == true)
        drv["ProductName"] = popup.ProductName;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM