1

C#WPFプログラムには、データを正常に入力したグリッドがあります。1つの列には、編集ページにリンクするボタンがあります。コードは以下のとおりです。

var col = new DataGridTemplateColumn();
col.Header = "Edit";
var template = new DataTemplate();
var textBlockFactory = new FrameworkElementFactory(typeof(Button));
textBlockFactory.SetBinding(Button.ContentProperty, new System.Windows.Data.Binding("rumId"));
textBlockFactory.SetBinding(Button.NameProperty, new System.Windows.Data.Binding("rumId"));
textBlockFactory.AddHandler( Button.ClickEvent, new RoutedEventHandler((o, e) => System.Windows.MessageBox.Show("TEST")));
template.VisualTree = textBlockFactory;
col.CellTemplate = template;
template = new System.Windows.DataTemplate();
var comboBoxFactory = new FrameworkElementFactory(typeof(Button));
template.VisualTree = comboBoxFactory;
col.CellEditingTemplate = template;
dgData.Columns.Add(col);

コードは正常に実行され、ボタンを選択するたびにメッセージボックスが表示されます。

これを取得して別のメソッドを呼び出し、これから選択したボタンの行番号を取得するにはどうすればよいですか?

次のメソッドは次のようになります。どうすれば呼び出すことができますか?

void ButtonClick(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("hi Edit Click 1");
// get the data from the row
string s = myRumList.getRumById(rumid).getNotes();
// do something with s
}
4

2 に答える 2

1

Id、またはデータ オブジェクト全体をバインドするだけです。CommandParameter

void ButtonClick(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("hi Edit Click 1");

    Button b = sender as Button;

    // Get either the ID of the record, or the actual record to edit 
    // from b.CommandParameter and do something with it
}

これは、MVVM デザイン パターンを使用するようにアプリケーションを切り替えることにした場合にも機能します。たとえば、XAML は次のようになります。

<Button Command="{Binding EditCommand}"
        CommandParameter="{Binding }" />
于 2012-02-17T19:29:26.590 に答える
0

私が理解しているのrumIdは、それを編集するには、クリックされたテキスト ブロックの が必要だということです。

イベントの設定中にClick、次のことができます。

textBlockFactory.AddHandler( Button.ClickEvent, 
     new RoutedEventHandler((o, e) => 
      {
         var thisTextBlock = (TextBlock) o; // casting the sender as TextBlock 
         var rumID = int.Parse(thisTextBlock.Name); // since you bind the name of the text block to rumID
         Edit(rumID); // this method where you can do the editting 
      })); 
于 2012-02-17T19:02:04.927 に答える