2014-03-24 54 views
0
本身 http://www.fast-files.com/getfile.aspx?file=71686

加載按鈕,失去功能

我正在上的項目要成爲一個易於管理的協議創造者

編輯:這裏的PROJEKT。它可以創建動態文本字段。一行是描述內容的另一行是內容本身。第2行有一個按鈕,可用作交通燈,原因很多。我現在在這一點,事情的「阿爾法」完成我可以保存一切,並加載它,但問題是,按鈕的功能會丟失,即我假設,因爲他不保存參考到事件處理程序。

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(); 
     } 


    } 
} 

回答

0

既然是Button,我可能會建議你使用Commands,而不是直接處理Click事件? 否則,您必須在解析後綁定事件 - 如this answer中所述。

+0

問題是如果我保存它,我仍然保存按鈕,我不確定你是否可以很容易地改變包裝面板中的項目的位置,因爲我希望按鈕始終在最後。 –