我創建了我的第一個WPF應用程序(早些時候使用WinForms完成了一些),並且我開始喜歡它(即使它增加了比有時需要更多的複雜性..)。 但我遇到了一個我無法弄清楚的問題。ItemsSource TreeView SelectedItem的屬性
問題: 我有一個TreeView和一個伴隨GroupBox與所選(第二級)項目的屬性。這些屬性可以在TextBoxes中編輯(綁定到SelectedItem。[Property])並且工作正常。 但屬性之一是(定義爲的ObservableCollection)的列表,我想在一個組合框來顯示,所以我的ItemsSource綁定到TreeView的SelectedItem.Modes:
<ComboBox Name="FixtureModesListBox" ItemsSource="{Binding ElementName=FixturesTreeView, Path=SelectedItem.Modes}" ItemTemplate="{StaticResource FixtureModeTemplate}" />
其中,顯然,沒有按」將不起作用,我得到的錯誤說:
BindingExpression path error: 'Modes' property not found on 'object' ''efcFixtureModel' ...
酒店確實存在,是集,是公衆等
我目前的解決方案 我能得到它的工作,b Ÿ使用一些代碼隱藏(和引用完全相同的「路徑」):
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;
}
}
我不能換我周圍爲什麼這是行不通的大腦,我還沒有在網絡上找到任何好消息。
編輯,包括更多的信息:
<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 + ")";
}
}
謝謝!我的腦部全面發展!我早些時候轉換了其他屬性,但後來忘了它。 – ChrisH