C# WPF基于同一列表的两个组合框
我对使用WPF相当陌生,我希望实现以下简单场景: 我有两个组合框,cmbSite和cmbLogFiles,我有一个C# WPF基于同一列表的两个组合框,c#,wpf,data-binding,combobox,C#,Wpf,Data Binding,Combobox,我对使用WPF相当陌生,我希望实现以下简单场景: 我有两个组合框,cmbSite和cmbLogFiles,我有一个列表,其定义如下: class LogFileDirectory { public List<System.IO.FileInfo> Files { get; private set; } public string Name { get; private set; } public string Path { get; private set;
列表
,其定义如下:
class LogFileDirectory
{
public List<System.IO.FileInfo> Files { get; private set; }
public string Name { get; private set; }
public string Path { get; private set; }
private LogFileDirectory() { }
public LogFileDirectory(string name, string path)
{
this.Name = name;
this.Path = path;
this.Files = new List<System.IO.FileInfo>();
if (System.IO.Directory.Exists(this.Path))
{
foreach (string file in System.IO.Directory.GetFiles(this.Path, "*.log", System.IO.SearchOption.TopDirectoryOnly))
this.Files.Add(new System.IO.FileInfo(file));
}
}
}
我希望cmbLogFiles绑定到当前所选cmbSite的同一
列表
上的文件属性,并过滤到cmbSite当前所选值的条目LogFileDirectory对象,但我真的不太确定如何在不在cmbSite的ClickEvent处理程序中写入代码的情况下执行此操作(根据我的WPF研究,这似乎是错误的方法)并将cmbLogFiles重新绑定到select cmbSite LogFileDirectory。根据@Chris在上面的评论中指出的线程,解决方法很简单
<ComboBox Name="cmbLogFiles" Width="140" ItemsSource="{Binding SelectedItem.Files, ElementName=cmbSite}" />
然后是解析器窗口的后续XAML:
<Window x:Class="Zapora.UI.Parser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Log File Parser" Height="350" Width="525">
<StackPanel Orientation="Horizontal" Height="26" VerticalAlignment="Top">
<Label Content="Web Site:"/>
<ComboBox Name="cmbSite" Width="180" ItemsSource="{Binding WebServerLogFileDirectories}" DisplayMemberPath="Name" SelectedValuePath="Path"/>
<Label Content="Files Available:"/>
<ComboBox Name="cmbLogFiles" Width="140" ItemsSource="{Binding SelectedItem.Files, ElementName=cmbSite}" />
</StackPanel>
</Window>
这应该让你开始:@Chris这比我想象的简单多了。谢谢你的提示。
parserView = new Parser();
parserView.DataContext = new LogFileInfo("deathstar");
<Window x:Class="Zapora.UI.Parser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Log File Parser" Height="350" Width="525">
<StackPanel Orientation="Horizontal" Height="26" VerticalAlignment="Top">
<Label Content="Web Site:"/>
<ComboBox Name="cmbSite" Width="180" ItemsSource="{Binding WebServerLogFileDirectories}" DisplayMemberPath="Name" SelectedValuePath="Path"/>
<Label Content="Files Available:"/>
<ComboBox Name="cmbLogFiles" Width="140" ItemsSource="{Binding SelectedItem.Files, ElementName=cmbSite}" />
</StackPanel>
</Window>