12

次のクラスがあり、テキスト変数をRoutedEventArgsとして渡したいと思います。

  public class CloseableTabItem : TabItem
  {
    String text;

    public CloseableTabItem()
    {
      //This style is defined in themes\generic.xaml
      DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
          new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
    }

    public CloseableTabItem(String incomingText)
    {
      //This style is defined in themes\generic.xaml
      DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
          new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
      text = incomingText;
    }

    public static readonly RoutedEvent CloseTabEvent =
        EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(CloseableTabItem));

    public event RoutedEventHandler CloseTab
    {
      add { AddHandler(CloseTabEvent, value); }
      remove { RemoveHandler(CloseTabEvent, value); }
    }

    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();

      Button closeButton = base.GetTemplateChild("PART_Close") as Button;
      if (closeButton != null)
        closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);
    }

    void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      this.RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
    }
  }

これは、WPFアプリのメインクラスであるWindow1のコードです。

  public partial class Window1 : Window
  {
    public static Window1 myWindow1;

    public Window1()
    {
      myWindow1 = this;
      InitializeComponent();
      this.AddHandler(CloseableTabItem.CloseTabEvent, new RoutedEventHandler(this.CloseTab));
    }

    private void CloseTab(object source, RoutedEventArgs args)
    {
      TabItem tabItem = args.Source as TabItem;
      if (tabItem != null)
      {
        TabControl tabControl = tabItem.Parent as TabControl;
        if (tabControl != null)
          tabControl.Items.Remove(tabItem);
      }
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
      CloseableTabItem tabItem = new CloseableTabItem("THIS IS A TEST");
      MainTab.Items.Add(tabItem);
    }
  }

CloseTabメソッドで「Stringtext」の値を出力できるようにしたい。RoutedEventArgs引数で「文字列テキスト」を渡すにはどうすればよいですか?

よろしくお願いします!

編集

プロジェクトにいくつか変更を加えました。コードは次のとおりです

ClosableTabItem.cs

namespace SampleTabControl
{
  public class CloseableTabItem : TabItem
  {

    String text;
    public delegate void TabsEventHandler(object sender, TabsEventArgs e);

    public CloseableTabItem()
    {
      //This style is defined in themes\generic.xaml
      DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
          new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
    }

    public CloseableTabItem(String incomingText)
    {
      //This style is defined in themes\generic.xaml
      DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
          new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
      this.text = incomingText;
    }

    public static readonly RoutedEvent CloseTabsEvent = EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble, typeof(TabsEventHandler), typeof(CloseableTabItem));    

    public event TabsEventHandler CloseTab
    {
      add { AddHandler(CloseTabsEvent, value); }
      remove { RemoveHandler(CloseTabsEvent, value); }
    }


    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();

      Button closeButton = base.GetTemplateChild("PART_Close") as Button;
      if (closeButton != null)
        closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);
    }

    void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      TabsEventArgs args = new TabsEventArgs(CloseTabsEvent, text);
      RaiseEvent(args);
    }
  }
}

TabsEventArgs.cs

public class TabsEventArgs : RoutedEventArgs
{
    private readonly string text;

    public string Text
    {
        get { return text; }
    }

    public TabsEventArgs(RoutedEvent routedEvent, string text) : base(routedEvent)
    {
        this.text = text;
    }
}

Window1.cs

  public partial class Window1 : Window
  {
    public static Window1 myWindow1;

    public Window1()
    {
      myWindow1 = this;
      InitializeComponent();
      this.AddHandler(CloseableTabItem.CloseTabsEvent, new RoutedEventHandler(this.CloseTab));
    }

    private void CloseTab(object source, RoutedEventArgs args)
    {      
      TabItem tabItem = args.Source as TabItem;
      if (tabItem != null)
      {
        TabControl tabControl = tabItem.Parent as TabControl;
        if (tabControl != null)
          tabControl.Items.Remove(tabItem);
      }
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
      CloseableTabItem tabItem = new CloseableTabItem("THIS IS A TEST");
      MainTab.Items.Add(tabItem);
    }
  }

変更を加えた後(複数のタブを開くとアプリがクラッシュします)、Window1クラスのCloseTabメソッドの「文字列テキスト」にどのようにアクセスしますか?

4

3 に答える 3

14

私は似たようなものが必要で、@Aryaと@HBの両方の助けを借りてこれを思いついた:

私のカスタムRoutedEventArgsサブクラス

public class ChangePageEventArgs : RoutedEventArgs {
    private readonly int _pageNumber;

    public int PageNumber {
        get { return _pageNumber; }
    }

    public ChangePageEventArgs(RoutedEvent routedEvent, int pageNumber) : base(routedEvent) {
        this._pageNumber = pageNumber;
    }
}

私の子供のクラス

private void defineButton_Click(object sender, RoutedEventArgs e) {
    ChangePageItemList(2);
}

public static readonly RoutedEvent GoToItemPropertiesViewEvent = EventManager.RegisterRoutedEvent(
"GoToItemPropertiesView", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(NoObjectView));

// Provide CLR accessors for the event
public event RoutedEventHandler GoToItemPropertiesView {
    add { AddHandler(GoToItemPropertiesViewEvent, value); }
    remove { RemoveHandler(GoToItemPropertiesViewEvent, value); }
}

public void ChangePageItemList(int _pageNumber) {
    ChangePageEventArgs args = new ChangePageEventArgs(GoToItemPropertiesViewEvent, _pageNumber);
    this.RaiseEvent(args);
}

私の親クラス

private ItemListView createItemListView() {
    _itemListView = new ItemListView();
    _itemListView.GoToItemPropertiesView += new RoutedEventHandler(ChangePage);

    return _itemListView;
}

private void ChangePage(object sender, RoutedEventArgs e) {
    ChangePageEventArgs args = (ChangePageEventArgs)e;
    // Value of page number is 2
    int _pageNumber = args.PageNumber;
}

@Aryaのお役に立てば幸いです

于 2013-05-13T17:21:51.317 に答える
13

の新しいサブクラスを作成しRoutedEventArgs、渡される変数を格納できるプロパティを追加し、void (object, YourNewEventArgs)イベントのハンドラータイプとして割り当てるタイプのそれぞれのハンドラーデリゲートを作成します(現在、ノーマルRoutedEventHandlerを使用しているため、ノーマルのみを提供します)RoutedEventArgs)。

その後、イベントが発生する場合は、新しいイベント引数を作成し、その変数をその変数のプロパティに渡す必要があります。ハンドラーで値を取得する方法は、自明である必要があります。

于 2011-11-13T03:51:48.163 に答える
0

1-新しいイベントデータクラスを作成します。

public class SelectedRowEventArgs : RoutedEventArgs
{
    public int Value { get; set; }

    public SelectedRowEventArgs(RoutedEvent routedEvent, int value) : base(routedEvent)
    {
        this.Value = value;
    }
}

2-ユーザーコントロールでリンクコードを作成します。

public partial class AusenciasBaseAdmin : UserControl
{

    public static readonly RoutedEvent RowChangedEvent = EventManager.RegisterRoutedEvent("RowChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AusenciasBaseAdmin));

    public event RoutedEventHandler RowChanged
    {
        add { AddHandler(RowChangedEvent, value); } remove { RemoveHandler(RowChangedEvent, value); }
    }



    public AusenciasBaseAdmin()
    {
        InitializeComponent();
    }


    private void RegAusencias_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.RaiseEvent(new SelectedRowEventArgs(RowChangedEvent, 1));
    }
}

3-メインフォームでユーザーコントロールをリンクします。

<control:AusenciasBaseAdmin Grid.Row="0" Grid.Column="0"
                        x:Name="GridAusencias" 
                        RowChanged="GridAusencias_RowChanged_1"
                        />

4-メインフォームの背後にあるコード:

    private void GridAusencias_RowChanged_1(object sender, RoutedEventArgs e)
    {
        SelectedRowEventArgs t = (SelectedRowEventArgs)e;

        MessageBox.Show(t.Value.ToString());
    }

WPFチームおめでとうございます。あなたはそれを非常に複雑にすることを達成しました

于 2019-07-10T10:42:49.643 に答える