[英]ItemsSource to property of SelectedItem of TreeView
I'm creating my first WPF application (have done a few with WinForms earlier), and I'm starting to like it (even if it adds more complexity than necessary sometimes..). 我正在创建我的第一个WPF应用程序(之前已经使用WinForms完成了一些操作),并且我开始喜欢它(即使它有时增加了不必要的复杂性。)。 But I've hit a problem that I can't really figure out.
但是我遇到了一个我无法真正解决的问题。
The Problem: I have a TreeView and a companion GroupBox with properties of the selected (2nd level) item. 问题:我有一个TreeView和一个伴随的GroupBox,具有选定(第二级)项目的属性。 The properties are editable in TextBoxes (bind to SelectedItem.[Property]) and that works fine.
这些属性在TextBoxes中可编辑(绑定到SelectedItem。[Property]),并且可以正常工作。 But one of the properties is a list (defined as ObservableCollection), that I want to display in a ComboBox, so I bound ItemsSource to the SelectedItem.Modes of the TreeView:
但是属性之一是一个列表(定义为ObservableCollection),我想在ComboBox中显示它,因此我将ItemsSource绑定到TreeView的SelectedItem.Modes:
<ComboBox Name="FixtureModesListBox" ItemsSource="{Binding ElementName=FixturesTreeView, Path=SelectedItem.Modes}" ItemTemplate="{StaticResource FixtureModeTemplate}" />
Which, apparently, doesn't work, the error I get says: 这显然是行不通的,我得到的错误是这样说的:
BindingExpression path error: 'Modes' property not found on 'object' ''efcFixtureModel' ...
The property does exist, is set, is public etc. 该属性确实存在,已设置,已公开等。
My current solution I can get it to work, by using some codebehind (and referencing the exact same "path"): 我当前的解决方案可以通过使用一些代码隐藏(并引用完全相同的“路径”)使其工作:
private void FixturesTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (FixturesTreeView.SelectedItem is efcFixtureModel) // The 1st level/parent items is of a different type
{
FixtureModesListBox.ItemsSource = (FixturesTreeView.SelectedItem as efcFixtureModel).Modes;
}
}
I can't wrap my brain around why this isn't working and I haven't find any good info on the net. 我无法解决为什么此方法不起作用的问题,也无法在网上找到任何好的信息。
Edited to include more info: 编辑以包括更多信息:
<TreeView Name="FixturesTreeView" ItemTemplate="{StaticResource FixtureItemTemplate}" SelectedItemChanged="FixturesTreeView_SelectedItemChanged">
</TreeView>
...
// Assignment of ItemsSource
FixturesTreeView.ItemsSource = mainWin.Companies;
...
public partial class efcMainWindow : Window
{
// Definition of Companies
public ObservableCollection<efcCompany> Companies;
...
// Definition of efcCompany
public class efcCompany
{
public int Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Logo { get; set; }
public int FixtureCount { get { return Fixtures.Count; } }
public ObservableCollection<efcFixtureModel> Fixtures { get; set; }
public efcCompany(string name = "", int id = -1, string url = "", string logo = "")
{
this.Name = name;
this.Id = id;
this.Url = url;
this.Logo = logo;
this.Fixtures = new ObservableCollection<efcFixtureModel>();
}
public override string ToString()
{
return "#" + Id.ToString() + ": " + Name + " (" + Url + ", " + Logo + ")";
}
}
// Definition of efcFixtureController
public class efcFixtureModel
{
public int Id { get; set; }
public int Manufacturer { get; set; }
public efcFixtureType Type { get; set; }
public string Name { get; set; }
public string Image { get; set; }
public string Description { get; set; }
public byte TotalChannelCount { get; set; }
public ObservableCollection<efcFixtureMode> Modes;
public efcFixtureModel()
{
Modes = new ObservableCollection<efcFixtureMode>();
}
public override string ToString()
{
return "#" + Id.ToString() + ": " + Name + " (" + Type.ToString() + ", " + Image + ", " + Description + ")";
}
}
You need to convert your efcFixtureModel.Modes from Field to Property: 您需要将efcFixtureModel.Modes从Field转换为Property:
public ObservableCollection<efcFixtureMode> Modes { get; set; }
Please read What is Difference between Property and Variable in C# 请阅读C#中的属性和变量之间的区别
As I said before, you need bind the SelectedItem from FixturesTreeView to a property of type efcFixtureModel in ViewModel. 如前所述,您需要将FixturesTreeView中的SelectedItem绑定到ViewModel中类型为efcFixtureModel的属性。
<TreeView Name="FixturesTreeView" ItemTemplate="{StaticResource FixtureItemTemplate}" SelectedItemChanged="FixturesTreeView_SelectedItemChanged"
SelectedItem="{Binding TreeViewSelectedItem}">
</TreeView>
<ComboBox Name="FixtureModesListBox" ItemsSource="{Binding TreeViewSelectedItem.Modes}" ItemTemplate="{StaticResource FixtureModeTemplate}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.