Exemplo n.º 1
0
        /// <summary>
        /// 根据TextBox内容动态更新静态成员CustomPool的属性
        /// </summary>
        /// <param name="sender">TextChange事件的sender</param>
        /// <param name="window"></param>
        public static void ChangeProperty(object sender, MainWindow window)
        {
            UIElementCollection collection = window.StackPanel_Main.Children;
            TextBlock           textblock  = (TextBlock)Helper.GetTextBlock(sender, window.StackPanel_Main, collection);

            PoolInfo    info    = window.GetPoolInfo(window.List_PoolDisplay.SelectedItem);
            PoolContent content = window.GetPoolContent(window.List_PoolContentDisplay.SelectedItem, info);

            string keyname = Helper.GetKeyByChineseName(textblock.Text).FirstOrDefault();
            //??运算符:左侧为null时,将右侧的值赋给等号左边
            var property = info.GetType().GetProperty(keyname) ?? content.GetType().GetProperty(keyname);

            //?这句的作用似乎和上面那句重复了
            //用于获取值是来自List_PoolContentDisplay还是List_PoolDisplay
            object obj = (info.GetType().GetProperties().Contains(property)) ? (object)info : content;

            if (sender.GetType().Name == "ComboBox")
            {
                ComboBox comboBox = (ComboBox)sender;
                //将变化的值赋值给obj对应的属性
                property.SetValue(obj, Convert.ChangeType(comboBox.SelectedItem.ToString(), property.PropertyType));
            }
            else if (sender.GetType().Name == "TextBox")
            {
                TextBox textBox = sender as TextBox;
                property.SetValue(obj, Convert.ChangeType(textBox.Text, property.PropertyType));

                //如果变化的是PoolName或者是Name,还需要同步更新左侧ListBox的内容
                string key = GetKeyByChineseName(textblock.Text).FirstOrDefault();
                if (key == "PoolName" || key == "Name")
                {
                    if (obj.Equals(info))
                    {
                        ChangeItemText(window.List_PoolDisplay, textBox.Text);
                    }
                    else
                    {
                        ChangeItemText(window.List_PoolContentDisplay, textBox.Text);
                    }
                }
            }
        }
 /// <summary>
 /// 向全局cp中添加新元素
 /// </summary>
 /// <param name="listbox"></param>
 private void AddNewElement(ListBox listbox)
 {
     if (!listbox.Name.Contains("Content")) //是上面的ListBox
     {
         if (listbox.SelectedItem == null)  //无被选中的,添加模板
         {
             cp.Infos.Add((PoolInfo)Helper.GetTempleteItem(new PoolInfo()));
         }
         else
         {
             //有被选中的,方便起见,复制当前选中的项目
             PoolInfo info = GetPoolInfo(List_PoolDisplay.SelectedItem).Clone();
             info.PoolName = "新元素..."; info.PoolContents = new List <PoolContent>();
             cp.Infos.Insert(List_PoolDisplay.SelectedIndex + 1, info);
         }
     }
     else//List_PoolContentDisplay
     {
         if (cp.Infos.Count == 0)//想在下面的listbox添加,却发现上面的listbox是空的
         {
             cp.Infos.Add((PoolInfo)Helper.GetTempleteItem(new PoolInfo()));//先添加模板
             List_PoolDisplay.Items.Add("新元素...");
             List_PoolDisplay.SelectedIndex = 0;
         }
         if (listbox.SelectedItem == null)//is null ,need to get a templete item
         {
             GetPoolInfo(List_PoolDisplay.SelectedItem).PoolContents.Add((PoolContent)Helper.GetTempleteItem(new PoolContent()));
         }
         else//not null,clone the selected item
         {
             var         info    = GetPoolInfo(List_PoolDisplay.SelectedItem);
             PoolContent content = GetPoolContent(List_PoolContentDisplay.SelectedItem, info).Clone();
             content.Name = "新元素...";
             info.PoolContents.Insert(List_PoolContentDisplay.SelectedIndex + 1, content);
         }
     }
 }