【问题标题】:How to read XML document and append the values of it to combo boxes and textboxes如何读取 XML 文档并将其值附加到组合框和文本框
【发布时间】:2019-01-28 04:16:31
【问题描述】:

EditingModel.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");
    }
}

EditingsViewModel.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>

EditingsView.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) 实现这一点。 谁能帮帮我。

提前致谢。

【问题讨论】:

    标签: c# xml wpf mvvm


    【解决方案1】:

    我想,这个对你有帮助

    对于 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}" />
    

    【讨论】:

    • 我应该在我的项目中的哪里写这些代码,这些代码位于“For this is the De-Serialize Model”这一特定行下? @Saravanakumar Natarajan
    • U 的意思是在 ViewModel.cs??
    • 为该模型创建类或在视图模型类中您可以使用它。
    • @DSPT,我修改了这个答案,现在检查一下
    • @Saravanakumar Natarajan "ComboBoxModel = new Model()" 这个特定的行是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多