WPF刷新基于XML数据的窗口/控件

时间:2013-09-18 00:26:34

标签: c# xml wpf xaml

我是新手,我有一个非常基本的C#WPF应用程序。我的数据存在于XML文件中,其中的项目被加载到我使用一系列网格,堆栈面板和边框创建的日历视图中。我应该创建一个类,但我不是因为我要快速而肮脏。

所有东西都被正确加载和显示,但我的问题是当我删除一个项目(更新XML文件)时,我不知道如何刷新/重新加载窗口。我确定我使用的控制层次最差。

XAML:

<Window x:Class="myWindow.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="myWindow">
  <ScrollViewer>
    <Grid Name="CalendarView">
        <ComboBox Name="myComboBox" SelectionChanged="LoadXML" >
        </ComboBox>

        <Grid>
          <Button Name="DeleteItem" Click="DeleteItem_Click" Content="Delete"></Button>
          <Button Name="CancelBtn" Click="CancelBtn_Click" Content="Cancel"></Button>
        </Grid>
    </Grid>
  </ScrollViewer>
</Window>

代码隐藏:

private void LoadXML(object sender, EventArgs e)
    {
        XElement myXML= XElement.Load(myComboBox.SelectedValue+"XML.xml");

        List<string> monthList = new List<string>();

        var Months = from x in myXML.Elements("Month")
                     select x;

        //CREATE EACH MONTH AND ADD DAYS AND ITEMS
        foreach (var month in Months)
        {
            var monthName = month.Attribute("name").Value;
            string[] monthSplit = monthName.Split(new Char[] { '_' });
            var monthMonth = monthSplit[0];
            var monthYear = monthSplit[1];
            int numberMonth = 0;
            switch (monthMonth)
            {
                case "January":
                    numberMonth = 1;
                    break;
                case "February":
                    numberMonth = 2;
                    break;
                case "March":
                    numberMonth = 3;
                    break;
                case "April":
                    numberMonth = 4;
                    break;
                case "May":
                    numberMonth = 5;
                    break;
                case "June":
                    numberMonth = 6;
                    break;
                case "July":
                    numberMonth = 7;
                    break;
                case "August":
                    numberMonth = 8;
                    break;
                case "September":
                    numberMonth = 9;
                    break;
                case "October":
                    numberMonth = 10;
                    break;
                case "November":
                    numberMonth = 11;
                    break;
                case "December":
                    numberMonth = 12;
                    break;
            }

            //create the month element
            var monthID = monthMonth+monthYear;
            monthList.Add(monthName);
            Grid CalendarMonth = new Grid();
            CalendarMonth.Name = monthID;
            CalendarView.Children.Add(CalendarMonth);
            this.RegisterName(monthID, CalendarMonth);

            //find the month in the system calendar
            int monthDays = DateTime.DaysInMonth(int.Parse(monthYear), numberMonth);

            for (var day = 1; day <= monthDays; day++)
            {
                StackPanel dateblock = new StackPanel();

                DateTime systemDay = DateTime.Parse(monthMonth+" "+day+", "+monthYear);
                var dayName = systemDay.DayOfWeek.ToString();
                var dayMonth = systemDay.Month.ToString();
                var dayYear = systemDay.Year;

                //ASSIGN HORIZONTAL INDEX DEPENDING ON DAY OF WEEK

                //ADD DATEBLOCK TO CALENDAR
                Grid.SetColumn(dateblock, dayIndex);
                Grid.SetRow(dateblock, weekIndex);
                dateblock.Name = dayName +"_"+ monthMonth+"_"+day.ToString()+"_" + dayYear.ToString();
                CalendarMonth.Children.Add(dateblock);


                //CREATE THE ITEMs
                var xmldays = from x in month.Elements("Day")
                            select x;

                //need to match up the day with the system day in order to find the item in the xml
                foreach (var xmlday in xmldays)
                {
                    var xmldaydate = xmlday.Attribute("name").Value;
                    string[] xmldaySplit = xmldaydate.Split(new Char[] { '_' });
                    var xmldayName = xmldaySplit[0];

                    if (xmldayName + xmldaySplit[2] == dayName+day)
                    {
                        var items = from x in xmlday.Elements("item")
                                    select x;
                        var itemCount = 0;

                        foreach (var item in items)
                        {
                            itemCount++;

                            Border itemButton = new Border();
                            string itemButtonName = monthMonth + "_" + day + "_" + monthYear + "_" + "item" + itemCount.ToString();
                            itemButton.Name = itemButtonName;
                            itemButton.MouseLeftButtonDown += new MouseButtonEventHandler(ItemClick);

                            //ADD ITEM TO DAY
                            dateblock.Children.Add(itemButton);
                            this.RegisterName(itemButtonName, itemButton);

                            //ADD ITEM NAME
                            TextBlock itemName = new TextBlock();
                            itemButton.Child = itemName;
                        }
                    }
                }
            }
        }
    }

当您点击某个项目时,会弹出ItemInfo框,其中包含删除按钮

    private void ItemClick(object sender, MouseButtonEventArgs e)
    {
        Border itemButton = sender as Border;
        StackPanel dateBlock = itemButton.Parent as StackPanel;
        XElement myXML = XElement.Load(myComboBox.SelectedValue + "XML.xml");
        string[] itemIDarray = itemButton.Name.Split(new Char[] { '_' });
        var itemID = int.Parse(itemIDarray[3].Replace("item", ""));

        var days = from x in myXML.Elements("Month").Elements("Day")
                    select x;

        foreach (var day in days)
        {
            var dayID = day.Attribute("name").Value;
            if (dayID == dateBlock.Name)
            {
                var items = from x in day.Elements("item")
                            select x;
                var itemCount = 0;
                foreach (var item in items)
                {
                    itemCount++;
                    if (itemCount == itemID)
                    {
                        Date.Text = dayID;
                        ID.Text = itemID.ToString();
                    }
                }
            }
        }
    }

这就是我的问题发生的地方:

    private void DeleteItem_Click(object sender, RoutedEventArgs e)
    {
        var itemIndex = int.Parse(ID.Text);
        var itemDate = Date.Text;
        string[] itemDateSplit = itemDate.Split(new Char[]{'_'});
        XElement myXML= XElement.Load(myComboBox.SelectedValue + "XML.xml");

        var days = from x in myXML.Elements("Month").Elements("Day")
                   select x;
        foreach (var day in days)
        {
            if (day.Attribute("name").Value == itemDate)
            {
                var items = from x in day.Elements("item")
                            select x;
                var itemCount = 0;
                foreach (var item in items)
                {
                    itemCount++;
                    if (itemCount == itemIndex)
                    {
                        var itemDay = item.Parent;
                        var itemMonth = itemDay.Parent;
                        var itemName = string.Format("{0}_{1}_{2}_{3}",itemDateSplit[1],itemDateSplit[2],itemDateSplit[3],"item"+itemCount);
                        item.Remove();
                        Border itemButton = (Border)this.FindName(itemName);
                        StackPanel dateBlock = (StackPanel)itemButton.Parent;
                        Grid CalendarMonth = (Grid)dateBlock.Parent;

                        if (itemDay.HasElements == false)
                        {
                            itemDay.Remove();
                        }
                        if (itemMonth.HasElements == false)
                        {
                            itemMonth.Remove();
                        }
                        myXML.Save(myComboBox.SelectedValue + "XML.xml");

                        dateBlock.UpdateLayout();
                        CalendarMonth.UpdateLayout();
                    }
                }
            }
        }
      }
  }
}

一旦删除了一个项目(有效,文件更新),我无法让窗口重新加载并显示新数据。整个窗口只是坐在那里,失去了所有的互动性。没有错误。不知道我需要调用什么样的刷新,或者我是否需要使用绑定。再一次 - 抱歉凌乱。

0 个答案:

没有答案