Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用组合框中的数据填充itemsource_C#_Wpf_Combobox - Fatal编程技术网

C# 用组合框中的数据填充itemsource

C# 用组合框中的数据填充itemsource,c#,wpf,combobox,C#,Wpf,Combobox,我有两个组合框,第一个是活动的,第二个只有在第一个被选中时才会被激活。它取决于第一个选项,选择什么,它将为第二个组合框构建不同的itemssource,并带有代码隐藏。比如说 情景1 我选择内容为1的第一个组合框和第二个组合框 将用数据A、B、C、D填充itemsource 场景2 我选择内容为2的第一个组合框和第二个组合框 将用数据T、Z、E、D填充itemsource 我的XAML代码片段 <UserControl x:Class="RestoreComputer.Views.Conf

我有两个组合框,第一个是活动的,第二个只有在第一个被选中时才会被激活。它取决于第一个选项,选择什么,它将为第二个组合框构建不同的itemssource,并带有代码隐藏。比如说

情景1

我选择内容为1的第一个组合框和第二个组合框 将用数据A、B、C、D填充itemsource

场景2

我选择内容为2的第一个组合框和第二个组合框 将用数据T、Z、E、D填充itemsource

我的XAML代码片段

<UserControl x:Class="RestoreComputer.Views.ConfigView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="60"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="20,0,0,0" Height="50">
            <ComboBox Name="_server" ItemsSource="{Binding Path=Servers}" SelectedItem="{Binding Path=Server}" IsSynchronizedWithCurrentItem="True" Width="100" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Text="18"/>
            <Image Source="../Images/narrow.png" Margin="10,0,10,0"/>
            <ComboBox Name="_computer" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Path=Computer}" Width="100" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Text="18">
                <ComboBox.Style>
                    <Style BasedOn="{StaticResource ComboBoxStyle}" TargetType="ComboBox">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedItem, ElementName=_server}" Value="{x:Null}">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ComboBox.Style>
            </ComboBox>

        </StackPanel>
    </Grid>
</UserControl>

以及viewmodel

public string Server
{
    get { return _server; }
    set
    {
        if (value != _server)
        {
            _server = value;
            RaisePropertyChanged(ref _server, value, () => Server);
            Computers = GetComputers();
        }
    }
}

public IEnumerable<string> GetComputers()
{
    string nas = ReadServerPath(Server);
    NetworkPath.MapNetworkDrive(nas, SettingsXml.User, SettingsXml.Password);
    var subFolders = new DirectoryInfo(nas);
    IEnumerable<string> folders = subFolders.GetDirectories().Select(e => e.Name).ToList();
    NetworkPath.DisconnectNetworkDrive();
    return folders;
}

public IEnumerable<string> Computers
{
    get { return _computers; }

    set
    {
        _computers = value;
        RaisePropertyChanged(ref _computers, value, () => Computers);
    }
}
公共字符串服务器
{
获取{return\u server;}
设置
{
如果(值!=\u服务器)
{
_服务器=值;
RaisePropertyChanged(ref _server,value,()=>server);
Computers=GetComputers();
}
}
}
公共IEnumerable GetComputers()
{
字符串nas=ReadServerPath(服务器);
NetworkPath.MapNetworkDrive(nas,设置sxml.User,设置sxml.Password);
var subFolders=新目录信息(nas);
IEnumerable folders=subFolders.GetDirectories().Select(e=>e.Name.ToList();
NetworkPath.DisconnectNetworkDrive();
返回文件夹;
}
公共数字计算机
{
获取{return\u computers;}
设置
{
_计算机=价值;
RaisePropertyChanged(参考计算机,值,()=>计算机);
}
}
我怎样才能得到上面提到的愿望

更新代码: XAML


视图模型

public string Server
{
    get { return _server; }
    set
    {
        if (value != _server)
        {
            _server = value;
            RaisePropertyChanged(ref _server, value, () => Server);
            if (Computers == null)
            {
                Computers = new ObservableCollection<string>();
            }
            else
            {
                Computers.Clear();
            }

            Computers = GetComputers();
        }
    }
}


public ObservableCollection<string> GetComputers()
{
    string nas = ReadServerPath(Server);
    NetworkPath.MapNetworkDrive(nas, SettingsXml.User, SettingsXml.Password);
    var subFolders = new DirectoryInfo(nas);
    var folders = new ObservableCollection<string>();
    subFolders.GetDirectories().Select(e => e.Name).LookUp(folders.Add);
    NetworkPath.DisconnectNetworkDrive();
    return folders;
}
公共字符串服务器
{
获取{return\u server;}
设置
{
如果(值!=\u服务器)
{
_服务器=值;
RaisePropertyChanged(ref _server,value,()=>server);
如果(计算机==null)
{
计算机=新的ObservableCollection();
}
其他的
{
计算机;
}
Computers=GetComputers();
}
}
}
公共可观测集合GetComputers()
{
字符串nas=ReadServerPath(服务器);
NetworkPath.MapNetworkDrive(nas,设置sxml.User,设置sxml.Password);
var subFolders=新目录信息(nas);
var folders=新的ObservableCollection();
subFolders.GetDirectories().Select(e=>e.Name).LookUp(folders.Add);
NetworkPath.DisconnectNetworkDrive();
返回文件夹;
}
但仍然不起作用

更新3

我把代码改成了

public string Server
    {
        get { return _server; }
        set
        {
            if (value != _server)
            {
                _server = value;
                RaisePropertyChanged(ref _server, value, () => Server);
                if (Computers == null)
                {
                    Computers = new ObservableCollection<string>();
                }
                else
                {
                    Computers.Clear();
                }

                foreach (var ele in GetComputers())
                {
                    Computers.Add(ele);
                }

            }
        }
    }

    public IEnumerable<string> GetComputers()
    {
        string nas = ReadServerPath(Server);
        NetworkPath.MapNetworkDrive(nas, SettingsXml.User, SettingsXml.Password);
        var subFolders = new DirectoryInfo(nas);
        NetworkPath.DisconnectNetworkDrive();
        foreach (var folder in subFolders.GetDirectories())
        {
            yield return folder.Name;
        }
    }
公共字符串服务器
{
获取{return\u server;}
设置
{
如果(值!=\u服务器)
{
_服务器=值;
RaisePropertyChanged(ref _server,value,()=>server);
如果(计算机==null)
{
计算机=新的ObservableCollection();
}
其他的
{
计算机;
}
foreach(GetComputers()中的var ele)
{
计算机。添加(ele);
}
}
}
}
公共IEnumerable GetComputers()
{
字符串nas=ReadServerPath(服务器);
NetworkPath.MapNetworkDrive(nas,设置sxml.User,设置sxml.Password);
var subFolders=新目录信息(nas);
NetworkPath.DisconnectNetworkDrive();
foreach(子文件夹.GetDirectories()中的var文件夹)
{
返回文件夹。名称;
}
}

最简单的方法是,像以前一样为Combobox 1进行绑定

当选择combo1中的某个项目时,您将用正确的项目填充Computers集合。所以每次combo1选择Editem时,combo2的数据源都会发生变化。若Combo并没有选择任何项,则combo2的数据源为空

这有用吗

public string Server
{
  get { return _server; }
  set
  {
      if (value != _server)
      {
          _server = value;
          RaisePropertyChanged(ref _server, value, () => Server);
          if(Computers == null) {
            Computers = new ObservableCollection<string>();
          } else {
            Computers.Clear();
          }
          Computers.AddRange(GetComputers());
      }
  }
}
公共字符串服务器
{
获取{return\u server;}
设置
{
如果(值!=\u服务器)
{
_服务器=值;
RaisePropertyChanged(ref _server,value,()=>server);
如果(计算机==null){
计算机=新的ObservableCollection();
}否则{
计算机;
}
AddRange(GetComputers());
}
}
}
对于所有绑定列表,您应该在ViewModel中使用ObservableCollection

如果使用ObservableCollection,则在集合更改时会通知视图。这就是你在这里需要的

工作示例

XAML

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <ComboBox ItemsSource="{Binding List1}" SelectedItem="{Binding SelectedList1}"></ComboBox>
    <ComboBox ItemsSource="{Binding List2}"></ComboBox>
</StackPanel>

代码隐藏

public partial class MainWindow : Window
{
  public MainWindow()
  {
    DataContext = this;
    List1 = new ObservableCollection<string> {"option1", "option2", "option3", "option4"};
    List2 = new ObservableCollection<string>();
    InitializeComponent();

  }

  private string m_selectedList1;
  public string SelectedList1
  {
    get { return m_selectedList1; }
    set
    {
      m_selectedList1 = value;
      if (m_selectedList1 == "option1")
      {
        List2.Clear();
        List2.Add("12345");
        List2.Add("123456");
      }
      if (m_selectedList1 == "option2")
      {
        List2.Clear();
        List2.Add("123");
        List2.Add("1234");
      }
    }
  }

  public ObservableCollection<string> List1 { get; set; }
  public ObservableCollection<string> List2 { get; set; }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
DataContext=this;
列表1=新的可观察集合{“选项1”、“选项2”、“选项3”、“选项4”};
List2=新的ObservableCollection();
初始化组件();
}
私有字符串m_selectedList1;
公共字符串SelectedList1
{
获取{return m_selectedList1;}
设置
{
m_selectedList1=值;
如果(m_selectedList1==“选项1”)
{
清单2.Clear();
清单2.添加(“12345”);
清单2.添加(“123456”);
}
如果(m_se
public partial class MainWindow : Window
{
  public MainWindow()
  {
    DataContext = this;
    List1 = new ObservableCollection<string> {"option1", "option2", "option3", "option4"};
    List2 = new ObservableCollection<string>();
    InitializeComponent();

  }

  private string m_selectedList1;
  public string SelectedList1
  {
    get { return m_selectedList1; }
    set
    {
      m_selectedList1 = value;
      if (m_selectedList1 == "option1")
      {
        List2.Clear();
        List2.Add("12345");
        List2.Add("123456");
      }
      if (m_selectedList1 == "option2")
      {
        List2.Clear();
        List2.Add("123");
        List2.Add("1234");
      }
    }
  }

  public ObservableCollection<string> List1 { get; set; }
  public ObservableCollection<string> List2 { get; set; }
}