Sim*_*ons 3 c# wpf .net-4.0 mvvm
我有一个类型的ListBox绑定.MyCollectionIEnumerable<string>
<ListBox x:Name="ListBox" ItemsSource="{Binding MyCollection, Mode=OneWay}" SelectionMode="Multiple"/>
Run Code Online (Sandbox Code Playgroud)
我有另一个List<string> SubCollection包含的子集MyCollection
无论什么时候SubCollection改变,我都希望按照这个改进SelectedItems来突出显示SubCollection
有没有Binding,Behaviour或任何其他方式来实现这一目标?
编辑:
让我们说我有一定的ListBox约束力MyCollection { "Orange", "Mango", "Stawberry", "Pineapple" }
让我们假设我按下一个按钮从数据库加载数据,结果是"橙色","芒果",然后放入SubCollection.ListBox现在应该有"橘子","芒果"作为它的SelectedItems.
我有一个建议给你.
你可以绑定到的" IsSelected"属性ListBoxItem.
要做到这一点,你必须使用collection of objects(比方说MyListBoxItem)而不是字符串集合.
public class MyListBoxItem
{
public string Description { get; set; }
public bool IsSelected { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用此" IsSelected的"财产MyListBoxItem类绑定" IsSelected"的属性ListBoxItem.
例如:在你看模型中,
this.MyCollection = new ObservableCollection<MyListBoxItem>();
MyListBoxItem item1 = new MyListBoxItem()
item1.Description = "Mango";
item1.IsSelected = true;
MyCollection .add(item1);
MyListBoxItem item2 = new MyListBoxItem()
item2 .Description = "Orange";
item2 .IsSelected = false;
MyCollection .add(item2 );
Run Code Online (Sandbox Code Playgroud)
XAML(里面ListBox)
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)