簡體   English   中英

如何讀取 XML 文檔並將其值附加到組合框和文本框

[英]How to read XML document and append the values of it to combo boxes and textboxes

編輯Model.cs:

private ObservableCollection<string> color;
public ObservableCollection<string> Color
{
    get { return color; }
    set
    {
        color = value;
        NotifyPropertyChanged("Color");
    }
}
private ObservableCollection<string> shapes;
public ObservableCollection<string> Shapes
{
    get { return shapes; }
    set
    {
        shapes = value;
        NotifyPropertyChanged("Shapes");
    }
}
private ObservableCollection<string> size;
public ObservableCollection<string> Size
{
    get { return size; }
    set
    {
        size = value;
        NotifyPropertyChanged("Size");
    }
}

編輯ViewModel.cs:

private string selectedcolor;
public string SelectedColor
{
    get { return selectedcolor; }
    set
    {
        if (value != selectedcolor)
        {
            selectedcolor = value; NotifyPropertyChanged("SelectedColor");
        }
    }
}
private string selectedshapes;
public string SelectedShapes
{
    get { return selectedshapes; }
    set
    {
        if (value != selectedshapes)
        {
            selectedshapes = value; NotifyPropertyChanged("SelectedShapes");
        }
    }
}
private string selectedsize;
public string SelectedSize
{
    get { return selectedsize; }
    set
    {
        if (value != selectedsize)
        {
            selectedsize = value; NotifyPropertyChanged("SelectedSize");
        }
    }
}

XML 文檔: (xml 文檔的名稱是 EditingsValue.xml)

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE MYEDITINGS[]>
<MYEDITINGS>
  <Group name="GeneralEditings">
     <EditingsName name="COLOR" value="red"/>
     <EditingsName name="SHAPES" value="circle"/>
     <EditingsName name="SIZE" value="medium"/>
     <EditingsName name="FILE PATH" value="C:\ProgramFiles"/>
  </Group>
</MYEDITINGS>

編輯視圖.xaml:

<ComboBox SelectedValue="{Binding SelectedColor,Mode=TwoWay}" Height="25" Width="150"    HorizontalContentAlignment="Right"  VerticalAlignment="Center"  ItemsSource="{Binding Color}"/ >
<ComboBox SelectedValue="{Binding SelectedShapes,Mode=TwoWay}" Height="25" Width="150"   HorizontalContentAlignment="Right"  VerticalAlignment="Center"  ItemsSource="{Binding Shapes}"/ >
<ComboBox SelectedValue="{Binding SelectedSize,Mode=TwoWay}" Height="25" Width="150"    HorizontalContentAlignment="Right"  VerticalAlignment="Center"  ItemsSource="{Binding Size}" />
<TextBox     Height="26"  Grid.Column="3"  IsReadOnly="True" TextAlignment="Right" VerticalContentAlignment="Center"  HorizontalAlignment="Left" VerticalAlignment="Center"  Width="150"   Text="{Binding ElementName=Mygroups, Path=DataContext.FolderPath,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"  />
<Button   KeyboardNavigation.TabIndex="2"  Grid.Column="4"  Content="Browse"    Height="29" VerticalAlignment="Bottom"   MinWidth="45"   Command="{Binding  ElementName=Mygeoups,Path=DataContext.FolderCommand}" CommandParameter="{Binding}" />

在我上面的代碼中,我使用SelectedIndex為我的組合框設置默認值,然后允許用戶選擇他們自己的值。 然后我如上所述在XML 文檔中寫入用戶的選定值。 到目前為止,一切正常。 但現在我的要求是,如果我再次打開我的應用程序,我不應該在組合框和文本框中獲取默認值,而是應該讀取 xml 文檔並在我的組合框和文本框中顯示該值。

如何使用 MVVM (wpf) 實現這一點。 誰能幫我嗎。

提前致謝。

我想,這個對你有幫助

對於EditingsViewModel.cs構造函數讀取 Xml 文件,將值分配給模型

  public EditingsViewModel()
    {
           ComboBoxModel = new EditingModel();

        //Xml Path
        string xmlpath = @"D:\MyDocument.xml";

        var doc = new XmlDocument();
        doc.Load(xmlpath);
        XmlNode colorNode = doc.SelectSingleNode(@"/MYEDITINGS/Group/EditingsName[@name = 'COLOR']/@value");
        XmlNode shapesNode = doc.SelectSingleNode(@"/MYEDITINGS/Group/EditingsName[@name = 'SHAPES']/@value");
        XmlNode sizeNode = doc.SelectSingleNode(@"/MYEDITINGS/Group/EditingsName[@name = 'SIZE']/@value");
        XmlNode filePathNode = doc.SelectSingleNode(@"/MYEDITINGS/Group/EditingsName[@name = 'FILE PATH']/@value");

        //Binding the Color to the Color Property
        var observableColors = new System.Collections.ObjectModel.ObservableCollection<string>() { "red","yellow","green"};
        ComboBoxModel.Color = observableColors;

        //Binding the Shapes to the Shape Property
        var observableShapes = new System.Collections.ObjectModel.ObservableCollection<string>() { "circle", "Triangle", "Rectangle" };
        ComboBoxModel.Shapes = observableShapes;

        //Binding the Size to the Size Property
        var observableSize = new System.Collections.ObjectModel.ObservableCollection<string>() { "medium", "high", "low" };
        ComboBoxModel.Size = observableSize;

        //Assign the Color Default vlaue from the Xml Document 
        SelectedColor = colorNode.Value;

        //Assign the Shape Default vlaue from the Xml Document 
        SelectedShapes = shapesNode.Value;

        //Assign the Size  Default vlaue from the Xml Document 
        SelectedSize = sizeNode.Value;

        //Assign the FilePath Default vlaue from the Xml Document 
        FolderPath = filePathNode.Value;
    }

EditingsView.xaml - 刪除選定的索引屬性

   <ComboBox SelectedValue="{Binding SelectedColor,Mode=TwoWay}" Height="25" Width="150"    HorizontalContentAlignment="Right"  VerticalAlignment="Center"  ItemsSource="{Binding ComboBoxModel.Color}"/>
    <ComboBox SelectedValue="{Binding SelectedShapes,Mode=TwoWay}" Height="25" Width="150"   HorizontalContentAlignment="Right"  VerticalAlignment="Center"  ItemsSource="{Binding ComboBoxModel.Shapes}"/>
    <ComboBox SelectedValue="{Binding SelectedSize,Mode=TwoWay}" Height="25" Width="150"    HorizontalContentAlignment="Right"  VerticalAlignment="Center"  ItemsSource="{Binding ComboBoxModel.Size}" />
    <TextBox Name="Mygroups"    Height="26"  Grid.Column="3"  IsReadOnly="True" TextAlignment="Right" VerticalContentAlignment="Center"  HorizontalAlignment="Left" VerticalAlignment="Center"  Width="150"   Text="{Binding ElementName=Mygroups, Path=DataContext.FolderPath,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"  />
    <Button   KeyboardNavigation.TabIndex="2"  Grid.Column="4"  Content="Browse"    Height="29" VerticalAlignment="Bottom"   MinWidth="45"   Command="{Binding  ElementName=Mygeoups,Path=DataContext.FolderCommand}" CommandParameter="{Binding}" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM