Combox多级联动
<h3>ViewModel</h3>
<pre><code class="language-csharp">public class WindowsViewmodel : INotifyPropertyChanged
{
public class Data
{
public int id { get; set; }
public string Name { get; set; }
public int Pid { get; set; }
}
public List&lt;Data&gt; ALL { get; set; } = new List&lt;Data&gt;();
public ObservableCollection&lt;Data&gt; SF { get; set; } = new ObservableCollection&lt;Data&gt;();
public ObservableCollection&lt;Data&gt; XJ { get; set; } = new ObservableCollection&lt;Data&gt;();
public ObservableCollection&lt;Data&gt; ZJ { get; set; } = new ObservableCollection&lt;Data&gt;();
private int s;
public int S
{
get { return s; }
set
{
s = value;
XJ.Clear();
var xx = ALL.Where(p =&gt; p.Pid == value).ToList();
foreach (var item in xx)
{
XJ.Add(item);
}
ZJ.Clear();
var zz = ALL.Where(p =&gt; p.Pid == XJ[0].id).ToList();
foreach (var item in zz)
{
ZJ.Add(item);
}
}
}
private int x;
public int X
{
get { return x; }
set {
x = value;
ZJ.Clear();
var zz = ALL.Where(p =&gt; p.Pid == value).ToList();
foreach (var item in zz)
{
ZJ.Add(item);
}
}
}
private int z;
public int Z
{
get { return z; }
set { z = value; }
}
public WindowsViewmodel()
{
ALL.Add(new Data() { id = 1, Name = &quot;Zs1&quot;, Pid = 0 });
ALL.Add(new Data() { id = 2, Name = &quot;Zs2&quot;, Pid = 0 });
ALL.Add(new Data() { id = 3, Name = &quot;Zs3&quot;, Pid = 0 });
ALL.Add(new Data() { id = 4, Name = &quot;Zs4&quot;, Pid = 1 });
ALL.Add(new Data() { id = 5, Name = &quot;Zs5&quot;, Pid = 1 });
ALL.Add(new Data() { id = 6, Name = &quot;Zs6&quot;, Pid = 1 });
ALL.Add(new Data() { id = 7, Name = &quot;Zs7&quot;, Pid = 2 });
ALL.Add(new Data() { id = 8, Name = &quot;Zs8&quot;, Pid = 2 });
ALL.Add(new Data() { id = 9, Name = &quot;Zs9&quot;, Pid = 2 });
ALL.Add(new Data() { id = 10, Name = &quot;Zs10&quot;, Pid = 3 });
ALL.Add(new Data() { id = 11, Name = &quot;Zs11&quot;, Pid = 3 });
ALL.Add(new Data() { id = 12, Name = &quot;Zs12&quot;, Pid = 3 });
ALL.Add(new Data() { id = 13, Name = &quot;Zs13&quot;, Pid = 4 });
ALL.Add(new Data() { id = 14, Name = &quot;Zs14&quot;, Pid = 4 });
ALL.Add(new Data() { id = 15, Name = &quot;Zs15&quot;, Pid = 4 });
ALL.Add(new Data() { id = 16, Name = &quot;Zs16&quot;, Pid = 5 });
ALL.Add(new Data() { id = 17, Name = &quot;Zs17&quot;, Pid = 5 });
ALL.Add(new Data() { id = 18, Name = &quot;Zs18&quot;, Pid = 5 });
ALL.Add(new Data() { id = 19, Name = &quot;Zs19&quot;, Pid = 6 });
ALL.Add(new Data() { id = 20, Name = &quot;Zs20&quot;, Pid = 6 });
ALL.Add(new Data() { id = 21, Name = &quot;Zs21&quot;, Pid = 6 });
ALL.Add(new Data() { id = 22, Name = &quot;Zs22&quot;, Pid = 7 });
ALL.Add(new Data() { id = 23, Name = &quot;Zs23&quot;, Pid = 7 });
ALL.Add(new Data() { id = 24, Name = &quot;Zs24&quot;, Pid = 7 });
ALL.Add(new Data() { id = 25, Name = &quot;Zs25&quot;, Pid = 8 });
ALL.Add(new Data() { id = 26, Name = &quot;Zs26&quot;, Pid = 8 });
ALL.Add(new Data() { id = 27, Name = &quot;Zs27&quot;, Pid = 8 });
ALL.Add(new Data() { id = 28, Name = &quot;Zs28&quot;, Pid = 9 });
ALL.Add(new Data() { id = 29, Name = &quot;Zs29&quot;, Pid = 9 });
ALL.Add(new Data() { id = 30, Name = &quot;Zs30&quot;, Pid = 9 });
var Al = ALL.Where(p =&gt; p.Pid == 0).ToList();
foreach (var item in Al)
{
SF.Add(item);
}
}
public event PropertyChangedEventHandler? PropertyChanged;
}</code></pre>
<h3>UI界面</h3>
<pre><code class="language-csharp">&lt;StackPanel Grid.Row=&quot;1&quot;&gt;
&lt;ComboBox Width=&quot;100&quot; ItemsSource=&quot;{Binding SF}&quot; SelectedValuePath=&quot;id&quot; DisplayMemberPath=&quot;Name&quot; SelectedValue=&quot;{Binding S}&quot;&gt;&lt;/ComboBox&gt;
&lt;ComboBox Width=&quot;100&quot; ItemsSource=&quot;{Binding XJ}&quot; SelectedValuePath=&quot;id&quot; DisplayMemberPath=&quot;Name&quot; SelectedValue=&quot;{Binding X}&quot;&gt;&lt;/ComboBox&gt;
&lt;ComboBox Width=&quot;100&quot; ItemsSource=&quot;{Binding ZJ}&quot; SelectedValuePath=&quot;id&quot; DisplayMemberPath=&quot;Name&quot; SelectedValue=&quot;{Binding Z}&quot;&gt;&lt;/ComboBox&gt;
&lt;/StackPanel&gt;</code></pre>