本文介绍了我有两个组合框和xml文件,我想从xml中填充组合框1中的服务器名称,并基于服务器名称填充第二个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0" encoding="utf-8"?>

<servers>
    <server>s2
        <database>db1</database>
        <database>db2</database>    

    </server>

    <server>s3
        <database>db3</database>
        <database>db4</database>    

    </server>

</servers>





view:



view :



public List<string> GetDatabases(string serv)
       {          
           var item = from items in xdoc.Descendants("database")
                      where (string)items.Element("server") == serv
                      select items.Elements("database").ToList();                        
           foreach (var items in item)
           {
               lstDBName.Add(items.ToString());
           }               
           return lstDBName;

       }


推荐答案

可以理解,您需要一种机制,在第一个组合中进行选择时,将使用与第一个组合相关的数据来更新第二个组合(根据您的问题名称:

As I can understand, you need some mechanism which on selection in first combo will update the second combo with first combo related data (based on your question name:

)。

这里是一个基于两个可观察的集合及其更新机制的解决方案。
1. Xaml代码:

Here is a solution which is based on two observable collections an its update mechanisms. 1. Xaml code:

<Window x:Class="SimpleDataGrid.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:simpleDataGrid="clr-namespace:SimpleDataGrid"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <simpleDataGrid:GridViewModel/>
</Window.DataContext>
<Grid >
    <DataGrid x:Name="SelectDataGrid" ItemsSource="{Binding Persons}" HorizontalAlignment="Left" VerticalAlignment="Top" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn x:Name="dgCheckBox" Header="Select" Width="45" Binding="{Binding IsChecked}"/>
            <DataGridTextColumn Header="FIRST NAME" Width="125" Binding="{Binding FNAME}"/>
            <DataGridTextColumn Header="LAST NAME" Width="125" Binding="{Binding LNAME}"/>
            <DataGridComboBoxColumn Header="Servers"
                                    DisplayMemberPath="ServerName" SelectedValueBinding="{Binding SelectedServer, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Path=DataContext.Servers}"/>
                        <Setter Property="IsReadOnly" Value="True"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Path=DataContext.Servers}"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
            <DataGridComboBoxColumn Header="Dbs" 
                                    DisplayMemberPath="DbName" 
                                    SelectedValueBinding="{Binding SelectedDb, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Path=DataContext.Dbs}"/>
                        <Setter Property="IsReadOnly" Value="True"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Path=DataContext.Dbs}"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid></Window>

2。提供解决方案的主模型代码。 UpdateDbCollection 方法可以调用您的GetDatabases方法,查询与服务器相关的数据库,将该数据放入 newDbs列表中,并使用中的新数据更新第二个组合可观察到的集合。添加数据并重新设置数据的方式。

2. Main Model Code which gives the solution.The UpdateDbCollection method can call your GetDatabases method, query server related data bases, put that data to the 'newDbs' list and update the second combo observable collections with new data in way when it add a new data and remode an old data.

    public class Person : BaseObservableObject
{
    private string _lName;
    private string _fName;
    private bool _checked;

    private object _selectedServer;
    private DbDetails _selectedDb;

    public Person()
    {
        Dbs = new ObservableCollection<DbDetails>();
        Servers = new ObservableCollection<ServerDetails>(new List<ServerDetails>
        {
            new ServerDetails
            {
                ServerName = "A",
                DbDetailses = new List<DbDetails>
                {
                    new DbDetails{DbName = "AA"},
                    new DbDetails{DbName = "AB"},
                    new DbDetails{DbName = "AC"},
                }
            },
             new ServerDetails
            {
                ServerName = "B",
                DbDetailses = new List<DbDetails>
                {
                    new DbDetails{DbName = "BA"},
                    new DbDetails{DbName = "BB"},
                    new DbDetails{DbName = "BC"},
                }
            },
             new ServerDetails
            {
                ServerName = "C",
                DbDetailses = new List<DbDetails>
                {
                    new DbDetails{DbName = "CA"},
                    new DbDetails{DbName = "CB"},
                }
            }
        });
    }
    public DbDetails SelectedDb
    {
        get { return _selectedDb; }
        set
        {
            _selectedDb = value;
            OnPropertyChanged();
        }
    }

    public object SelectedServer
    {
        get { return _selectedServer; }
        set
        {
            _selectedServer = value;
            OnPropertyChanged();
            UpdateDbCollection(SelectedServer as ServerDetails);
        }
    }

    private void UpdateDbCollection(ServerDetails serverDetails)
    {
        //here you can get your db details by selected server
        var newDbs = serverDetails.DbDetailses;
        newDbs.ForEach(details => Dbs.Add(details));
        var valuesToClear =
            Dbs.Where(
                existingDbDetails => newDbs.FirstOrDefault(dbDetails => existingDbDetails == dbDetails) == null).ToList();
        valuesToClear.ForEach(details => Dbs.Remove(details));
    }

    public ObservableCollection<ServerDetails> Servers { get; set; }

    public ObservableCollection<DbDetails> Dbs { get; set; }

    public bool IsChecked
    {
        get { return _checked; }
        set
        {
            _checked = value;
            OnPropertyChanged();
        }
    }

    public string LNAME
    {
        get { return _lName; }
        set
        {
            _lName = value;
            OnPropertyChanged();
        }
    }

    public string FNAME
    {
        get { return _fName; }
        set
        {
            _fName = value;
            OnPropertyChanged();
        }
    }
}                                                

3。型号:

    public class ServerDetails:BaseObservableObject
{
    private string _serverName;

    public string ServerName
    {
        get { return _serverName; }
        set
        {
            _serverName = value;
            OnPropertyChanged();
        }
    }

    public List<DbDetails> DbDetailses { get; set; }
}

public class DbDetails:BaseObservableObject
{
    private string _dbName;

    public string DbName
    {
        get { return _dbName; }
        set
        {
            _dbName = value;
            OnPropertyChanged();
        }
    }
}




  1. 查看模型代码:

  1. View model code:

公共类GridViewModel:BaseObservableObject
{

public class GridViewModel:BaseObservableObject{

public GridViewModel()
{
    var l = new List<Person>
    {
        new Person {FNAME = "John", LNAME = "W"},
        new Person {FNAME = "George", LNAME = "R"},
        new Person {FNAME = "Jimmy", LNAME = "B"},
        new Person {FNAME = "Marry", LNAME = "B"},
        new Person {FNAME = "Ayalot", LNAME = "A"},
    };
    Persons = new ObservableCollection<Person>(l);
}

public ObservableCollection<Person> Persons { get; set; }

}

BaseObservableObject是INotifyPropertyChanged的简单实现。

BaseObservableObject is a simple implementation of INotifyPropertyChanged.

我希望它会对您有所帮助。
谢谢!

I hope it will help you.Thanks an regards,

这篇关于我有两个组合框和xml文件,我想从xml中填充组合框1中的服务器名称,并基于服务器名称填充第二个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 23:48