我正在使用WPF,并且在我的表单上绑定了一个TreeView模型。对象具有属性,我想将选定的项目(在树视图中)属性绑定到列表框但我无法弄清楚如何做到这一点。我的代码是:
酒吧班:
public class Bar
{
string barName;
List<bar> children;
List<Foo> attrs;
public string BarName
{
get { return barName; }
set { barName = value; }
}
public List<Folder> Children
{
get { return children; }
set { children = value; }
}
public List<Foo> Attributes
{
get { return attrs; }
set { attrs = value; }
}
public Bar(string name)
{
barName = name;
children = new List<Bar>();
attrs = new List<Foo>();
attrs.Add(new Foo { Name = "Attr1: " + name });
attrs.Add(new Foo { Name = "Attr2: " + name });
attrs.Add(new Foo { Name = "Attr3: " + name });
}
}
Foo类:
public class Foo
{
public string Name { get; set; }
}
填充模型:
Bar bar = new Folder("bar1");
bar.Children.Add(new Bar("bar1.1"));
bar.Children[0].Children.Add(new Bar("bar1.1.1"));
bar.Children.Add(new Bar("bar2"));
this.DataContext = bar;
还有XAML:
<Window.Resources>
<ResourceDictionary>
<HierarchicalDataTemplate DataType="{x:Type local:Bar}"
ItemsSource="{Binding Children}">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="16"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding BarName}"
Foreground="Black"
TextTrimming="CharacterEllipsis"
TextWrapping="Wrap"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Column="1"/>
</Grid>
</HierarchicalDataTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<TreeView Height="162" HorizontalAlignment="Left" Margin="203,0,0,0" Name="treeView1" VerticalAlignment="Top" Width="288" ItemsSource="{Binding Children}"/>
<ListBox Height="100" HorizontalAlignment="Left" Margin="203,168,0,0" Name="listBox2" VerticalAlignment="Top" Width="288" ItemsSource="{Binding Name}"/>
</Grid>
现在TreeView绑定工作正常,我显示了一个Bar.Name,但ListBox为空。请解释一下,我该怎么办?
答案 0 :(得分:0)
您将列表框绑定到Name,因此它正在尝试查找名为&#34; Name&#34;在你的Bar类。我相信你真正想要做的是显示当前在TreeView中选择的项目的属性。因此绑定到TreeView的SelectedItem并将DisplayMemberPath设置为&#34; Name&#34;:
<ListBox Height="100" HorizontalAlignment="Left" Margin="203,168,0,0" Name="listBox2" VerticalAlignment="Top" Width="288" ItemsSource="{Binding SelectedItem.Attributes, ElementName=treeView1}" DisplayMemberPath="Name"/>
这会有效,但更好的想法是在你的Bar类中为当前所选项创建一个成员(例如&#34; CurrentTreeItem&#34;)并绑定treelist的SelectedItem和ListBox& #39;该属性的源项目,至少你可以在setter中放置断点等,并确保你的前端控件正常启动。这样做的问题在于,您似乎并不支持IPropertyChange通知(如果您不知道那是什么,那就放弃您正在做的事情,并在继续前往Google之前)。
答案 1 :(得分:0)
您可以通过将xaml更改为
将列表框直接绑定到TreeView所选项目 <TreeView Height="162" HorizontalAlignment="Left" Margin="203,0,0,0" Name="treeView1" VerticalAlignment="Top" Width="288" ItemsSource="{Binding Source}" />
<ListBox DisplayMemberPath="Name" Height="100" HorizontalAlignment="Left" Margin="203,168,0,0" Name="listBox2" VerticalAlignment="Top" Width="288" ItemsSource="{Binding Path=SelectedItem.Attributes, ElementName=treeView1}"/>
遗憾的是,树状视图的所选项目是只读属性,因此您无法在视图中轻松创建属性并将列表框和树视图绑定到该属性。
此外,您应该将人口变为
public ObservableCollection<Bar> Source { get; set; }
public MainWindow()
{
InitializeComponent();
Bar bar = new Bar("bar1");
bar.Children.Add(new Bar("bar1.1"));
bar.Children[0].Children.Add(new Bar("bar1.1.1"));
bar.Children.Add(new Bar("bar2"));
Source = new ObservableCollection<Bar>() { bar };
this.DataContext = this;
}
请注意,您必须实现INotifyPropertyChanged接口以允许绑定自动更新