加载按钮会失去功能

时间:2014-03-24 13:07:16

标签: c# wpf load

编辑:这里是projekt本身http://www.fast-files.com/getfile.aspx?file=71686

我正在开发的项目希望成为一名易于管理的协议创建者。它可以创建动态文本字段。一行用于描述内容,另一行用于内容本身。由于多种原因,第2行有一个按钮作为红绿灯。我现在已经完成了事情的“Alpha”,我可以保存所有内容并加载它但问题是按钮的功能丢失了,我认为是因为他没有保存参考到事件处理程序。

namespace AmpelThingy
{
    public class Load       
    {

    public void Loading() {
    StreamReader sR = new StreamReader(@"AA.xaml");
    string text = sR.ReadToEnd();
    sR.Close();

    StringReader stringReader = new StringReader(text);
    XmlReader xmlReader = XmlReader.Create(stringReader);

    StackPanel wp = (StackPanel)System.Windows.Markup.XamlReader.Load(xmlReader);

    ((MainWindow)System.Windows.Application.Current.MainWindow).SP2.Children.Clear(); // clear the existing children

    foreach (FrameworkElement child in wp.Children) // and for each child in the WrapPanel we just loaded (wp)
    {
        ((MainWindow)System.Windows.Application.Current.MainWindow).SP2.Children.Add(CloneFrameworkElement(child)); // clone the child and add it to our existing wrap panel
    }           
}

FrameworkElement CloneFrameworkElement(FrameworkElement originalElement)
{
    string elementString = XamlWriter.Save(originalElement);

    StringReader stringReader = new StringReader(elementString);
    XmlReader xmlReader = XmlReader.Create(stringReader);
    FrameworkElement clonedElement = (FrameworkElement)XamlReader.Load(xmlReader);

    return clonedElement;
}
    }
}

namespace AmpelThingy
{
    public class Save
    {

     public void Saving() {   
    StringBuilder outstr = new StringBuilder();

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.OmitXmlDeclaration = true;
    settings.NewLineOnAttributes = true;


    XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
    dsm.XamlWriterMode = XamlWriterMode.Expression;
    XamlWriter.Save(((MainWindow)System.Windows.Application.Current.MainWindow).SP2, dsm);
    string savedControls = outstr.ToString();

    File.WriteAllText(@"AA.xaml", savedControls);
     }
    }
}


namespace AmpelThingy
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Background = Brushes.Magenta;
            stackPanel1.Width = Double.NaN;
            stackPanel1.Height = Double.NaN;
            stackPanel2.Width = Double.NaN; ;
            stackPanel2.Height = Double.NaN;
            AddWrapPanelMaster();

        }

        Button AddButtonSlave(Button but)
        {


            but.Click += new RoutedEventHandler(this.buttClickSlave);
            return (but);
        }

        Button AddButtonSlaveAdd(Button but)
        {


            but.Click += new RoutedEventHandler(this.buttClickSlaveAdd);
            return (but);
        }

        Button AddButtonMaster(Button but)
        {


            but.Click += new RoutedEventHandler(this.buttClickMaster);
            return (but);
        }

        void buttClickSlaveAdd(Object sender, EventArgs e)
        {
            TextBox txtB1 = new TextBox();
            txtB1.Background = System.Windows.Media.Brushes.Magenta;
            txtB1.Text = "Text";
            txtB1.Width = 75;
            txtB1.Height = 75;
            Button s = (Button)sender;
            WrapPanel wp = (WrapPanel)s.Parent;
            wp.Children.Remove(s);
            wp.Children.Add(txtB1);
            Button b = new Button();
            b = AddButtonMaster(b);
            b.Content = "Add";
            b.Width = 75;
            wp.Children.Add(b);

            // Add the buttons to the parent WrapPanel using the Children.Add method.


        }

        void buttClickMaster(Object sender, EventArgs e)
        {
           TextBox txtB1 = new TextBox();
            txtB1.Background = System.Windows.Media.Brushes.Magenta;
            txtB1.Text = "Text";
            txtB1.Width = 75;
            txtB1.Height = 75;
            Button s = (Button)sender;
            WrapPanel wp = (WrapPanel)s.Parent;
            wp.Children.Remove(s);
            wp.Children.Add(txtB1);
            Button b = new Button();
            b = AddButtonMaster(b);
            b.Content = "Add";
            b.Width = 75;
            wp.Children.Add(b);

            // Add the buttons to the parent WrapPanel using the Children.Add method.


        }

        public StackPanel SP2
        {
            get { return stackPanel2; }
        }

        void buttClickSlave(Object sender, EventArgs e)
        {

            Button s = (Button)sender;

            if (s.Background != Brushes.Blue && s.Background != Brushes.Red && s.Background != Brushes.Green)
                s.Background = Brushes.Red;
            else
                if (s.Background == Brushes.Green)
                    s.Background = Brushes.Red;
            else
            if (s.Background == Brushes.Blue)
                s.Background = Brushes.Green;
            else
            if (s.Background == Brushes.Red)
                s.Background = Brushes.Blue;

        }

        void AddTextBox(Button sender) {
            TextBox txtB1 = new TextBox();
            txtB1.Text = "Text";
            txtB1.Width = 75;
            txtB1.Height = 75;
            WrapPanel s = (WrapPanel)sender.Parent;
            s.Children.Add(txtB1);

            // Add the buttons to the parent WrapPanel using the Children.Add method. 

        }

        void AddWrapPanelSlave() {

            WrapPanel myWrapPanel = new WrapPanel();
            myWrapPanel.Background = System.Windows.Media.Brushes.Azure;
            myWrapPanel.Orientation = Orientation.Horizontal;
            myWrapPanel.Width = 4000;
            myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
            myWrapPanel.VerticalAlignment = VerticalAlignment.Top;


            // Define 3 button elements. The last three buttons are sized at width 
            // of 75, so the forth button wraps to the next line.
            Button btn1 = new Button();
            btn1 = AddButtonSlave(btn1);
            btn1.Content = "Button 1";
            btn1.Width = 75;
            Button btn2 = new Button();
            btn2 = AddButtonSlaveAdd(btn2);
            btn2.Content = "Button 2";
            btn2.Width = 75;

            // Add the buttons to the parent WrapPanel using the Children.Add method.
            myWrapPanel.Children.Add(btn1);
            myWrapPanel.Children.Add(btn2);
            this.stackPanel1.Children.Add(myWrapPanel);
        }

        void AddWrapPanelMaster()
        {

            WrapPanel myWrapPanel = new WrapPanel();

            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);

            myWrapPanel.Background = System.Windows.Media.Brushes.Magenta;
            myWrapPanel.Orientation = Orientation.Horizontal;
            myWrapPanel.Width = 4000;
            myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
            myWrapPanel.VerticalAlignment = VerticalAlignment.Top;


            // Define 3 button elements. The last three buttons are sized at width 
            // of 75, so the forth button wraps to the next line.
            TextBox txtB1 = new TextBox();
            txtB1.Background = System.Windows.Media.Brushes.Magenta;
            txtB1.Text = "Text";
            txtB1.Width = 75;
            txtB1.Height = 75;
            Button btn1 = new Button();
            btn1 = AddButtonMaster(btn1);
            btn1.Content = "Add";
            btn1.Width = 75;

            // Add the buttons to the parent WrapPanel using the Children.Add method.
            myWrapPanel.Children.Add(txtB1);
            myWrapPanel.Children.Add(btn1);
            this.stackPanel2.Children.Add(myWrapPanel);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            AddWrapPanelSlave();
            this.Show();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Load load = new Load();
            load.Loading();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Save save = new Save();
            save.Saving();
        }


    }
}

1 个答案:

答案 0 :(得分:0)

由于它是Button,我建议您使用Commands而不是直接处理Click事件吗? 否则,您必须在解析后绑定事件 - 如this answer中所述。