我已经将第一个ListBox'ListBox1'的ItemsSource属性指定为另一个ListBox的ItemsSource,即'ListBox2' . 如果我将ListBox2的ItemsSource设置为null,那么我无法将任何项目进一步添加到ListBox1的ItemsSource .
下面是xaml片段,
<ListBox VerticalAlignment="Top" HorizontalAlignment="Center" Width="150" Margin="0 25 0 0"
x:Name="ListBox1" ItemsSource="{Binding Coll,Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding _Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox VerticalAlignment="Top" HorizontalAlignment="Center" Width="150" Margin="0 25 0 0"
x:Name="ListBox2" ItemsSource="{Binding Path=ItemsSource,ElementName=ListBox1,Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding _Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在Code后面,我在下面的按钮点击上将ListBox2的ItemSource设置为null,
ListBox2.SetCurrentValue(ListBox.ItemsSourceProperty, null);
完成后,我尝试将项添加到ListBox1的“Coll”集合,但抛出NRE“Coll”为空 .
任何建议PLZ .
此致,Dinesh Kumar P.
1 回答
当
ListBox2.ItemsSource
设置为null
时,ListBox2.ItemsSource
上的双向绑定将ListBox1.ItemsSource
设置为null
. 随后,ListBox1.ItemsSource
上的双向绑定也将Coll
设置为null
.不要双向
ItemsSource
绑定 . 没有必要,仍然会通知收集更改 .目前还不是很清楚你要实现什么,但最好还是将
ListBox2.ItemsSource
直接绑定到Coll
属性 .