我一直有一些麻烦,获得一个列表框正确绑定到集合。属性在列表框DataTemplate中没有正确绑定
我给框架代码,然后解释我想要它做什么。
XAML标记:
<ListBox DataContext="{Binding Foos, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding Main.SelectedFoo, Mode=TwoWay,
Source={StaticResource Locator},
UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding Main.SelectedFoo, Source={StaticResource Locator}}"/>
<ListBox ItemsSource="{Binding Main.SelectedFoo.Bars}" SelectedItem="{Binding Main.SelectedBar}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Right">
<!-- The binding requires "{Binding .}" because a path must be explicitly set for Two-Way binding,
even though {Binding .} is supposed to be identical to {Binding} -->
<TextBox Text="{Binding Path=. , UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
C#视图模型:
private ObservableCollection<Foo> _barList = new ObservableCollection<Foo>();
private const string BardListPN = "FooList";
public ObservableCollection<Foo> FooList
{
get { return _fooList; }
set
{
if (_fooList == value)
{
return;
}
var oldValue = _fooList;
_fooList = value;
RaisePropertyChanged(FooListPN);
}
}
private Foo _selectedFoo;
private const string SelectedFooPN = "SelectedFoo";
public Foo SelectedFoo
{
get { return _selectedFoo; }
set
{
if (_selectedFoo == value)
{
return;
}
var oldValue = _selectedFoo;
_selectedFoo = value;
// Update bindings, no broadcast
RaisePropertyChanged(SelectedFooPN);
}
}
public const string SelectedBarPN = "SelectedBar";
private string _selectedBar = "";
public string SelectedBar
{
get
{
return _selectedBar;
}
set
{
if (_selectedBar == value)
{
return;
}
var oldValue = _selectedBar;
_selectedBar = value;
// Update bindings, no broadcast
RaisePropertyChanged(SelectedBarPN);
}
}
C#型号:
public class Foo
{
public ICollection<string> Bars
{
get { return _bars; }
set
{
_bars= value;
NotifyPropertyChanged("Bars");
// snipped obvious INotifyPropertyChanged boilerplate code
}
}
}
我的问题没有设置Bar
集合中字符串文本框的任何更改。当选择的Foo
更改为不同的Foo
后,将显示原始Bars
。
有人能告诉我我做错了什么吗?这看起来应该更简单。谢谢!
更新:我已根据Tri Q的建议更改了代码,但对文本框所做的更改未反映在属性本身中。有任何想法吗?
太棒了!非常感谢额外的信息。是的,正如你可以清楚地看到的,我仍然在学习WPF和MVVM,并且我衷心感谢评论。我会试试你的建议。 – llaughlin 2010-10-05 12:36:25
需要注意的一件事:我有'TextBox'绑定的方式,因为模式是'TwoWay'(甚至默认),所以需要路径,我在我的XAML注释中注明了这一点。这似乎是将'TextBox'绑定到集合中的当前项目的唯一方法。 – llaughlin 2010-10-05 15:53:26