C# 将变量数据绑定到ListBox.SelectedItem属性
我有一个列表框(lstBxsources),它被正确地填充并正常工作C# 将变量数据绑定到ListBox.SelectedItem属性,c#,data-binding,silverlight-4.0,listbox,C#,Data Binding,Silverlight 4.0,Listbox,我有一个列表框(lstBxsources),它被正确地填充并正常工作 <ListBox Name="lstBxSources" ItemsSource="{Binding}" > <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Name}" ToolTipService.
<ListBox Name="lstBxSources" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" ToolTipService.ToolTip="{Binding Path=Description}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我已将CurrentSource数据绑定到LSTBX源的SelectedItem,如下所示:
MyUserControl.SetBinding(MyControl.CurrentSourceProperty, new Binding() { Source = lstBxSources.SelectedItem});
该选项最初起作用,但在更改SelctedItem时不会更新
知道为什么它不会为我更新吗?知道了。。。按如下方式修复绑定:
MyUserControl.SetBinding(MyControl.CurrentSourceProperty,
new Binding() {
Source = lstBxSources,
Path= new PropertyPath("SelectedItem")
});
如果我没弄错,请将此代码放在MyUserControl中:
MyAnotherControl.SetBinding(AnotherControl.currentSourceInfoProperty,
new Binding()
{
Source = this,
Path = new PropertyPath("CurrentSource"),
Mode = BindingMode.TwoWay
});
这么简单。。我似乎总是这样。非常感谢。假设我在MyUserControl中有另一个用户控件,该控件有另一个要绑定到CurrentSource的变量。我该怎么做?我有以下内容:anotherControl.SetBinding(anotherControl.currentSourceInfoProperty,new Binding(){Source=CurrentMapSource,Path=new PropertyPath(“SourceInfo”)});但是第一次之后也不会更新。你很好。。。再次:o)
MyUserControl.SetBinding(MyControl.CurrentSourceProperty,
new Binding() {
Source = lstBxSources,
Path= new PropertyPath("SelectedItem")
});
MyAnotherControl.SetBinding(AnotherControl.currentSourceInfoProperty,
new Binding()
{
Source = this,
Path = new PropertyPath("CurrentSource"),
Mode = BindingMode.TwoWay
});