Search code examples
c#xmlwpfxmldataprovider

Passing Button_Click Tag to another page WPF C#


Hi have a button with click event & tag value binding from an XmlDataProvider;

<Button x:Name="Open" Tag="{Binding XPath=Id}" Content="Open" Click="OpenProject_Click" />

and in my xaml.cs click event code (creates new TabItem, with other page content);

  private void Open_Click(object sender, RoutedEventArgs e)
    {
                var ID = ((Button)sender).Tag;

                TabItem tabitem = new TabItem();
                tabitem.Header = ID;
                tabitem.Tag = ID;
                Frame tabFrame = new Frame();
                Pages.Views.View newTab = new Pages.Views.View(ID);
                tabFrame.Content = newTab;
                tabitem.Content = tabFrame;
                AppTabs.Items.Add(tabitem);
                tabitem.Focus();

    }

Below is my Other Page code;

    public partial class View : Page
{
    Object valueFromPage1;

    public View()
    {
        InitializeComponent();
    }

    public View(Object val)
        : this()
    {
        valueFromPage1 = val;
        this.Loaded += new RoutedEventHandler(View_Loaded);

    }
    void View_Loaded(object sender, RoutedEventArgs e)
    {
        text.Text = "Value passed from page1 is: " + valueFromPage1;
    }
}

Problem is that the value that appears on the other page as System.Xml.XmlElement when it need to be the ID Value (which is a number). Is there away to convert the System.Xml.XmlElement back to the button.Tag value?


Solution

  • That the value appears as "System.Xml.XmlElement" indicates that the Tag is an XmlElement. It has an attribute InnerText that contains its value (which should be the Id you are looking for).

    var ID = (((Button)sender).Tag as XmlElement).InnerText;