我正在尝试做一些应该脑死的简单的事情,但是,我无法让它工作。我正在显示列表框中的项目列表。我已将复选框添加到列表框,以便用户可以选择多个项目。但是,即使列表中绑定到ListBox的对象具有“IsSelected”属性,它也没有被绑定。我可以使用一些帮助,因为这使我疯狂。在WPF中绑定ListBox的IsSelectedProperty不起作用。需要帮助
<Style x:Key="CheckBoxListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Multiple"></Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Margin" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}">
<ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<ListBox
Style="{StaticResource CheckBoxListStyle}"
IsEnabled="{Binding Path=SpecificClients.Value, Mode=OneWay}"
ItemsSource="{Binding Path=SelectedClients}"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
ScrollViewer.VerticalScrollBarVisibility="Auto"
MaxHeight="95">
</ListBox>
在视图模型我有以下几点:
public IEnumerable<SelectedClientVM> SelectedClients
....
public class SelectedClientVM
{
public bool IsSelected { get; set; }
public Client Client { get; set; }
public override string ToString()
{
return Client.SearchText;
}
}
thanx兄弟来处理。那钉了它。这让我疯狂。我尝试绑定到数据模板中的ListBoxItem,或者使用RelativeAncestor,但它们都不起作用。我从来没有想过为我使用的虚拟机类型使用数据模板,但现在我明白了......似乎很明显。 – Keith 2010-07-14 19:04:33
好,很高兴为你解决了它。对我而言,一旦我将我的大脑包裹在模型中,这绝对是一个灯泡爆发的时刻。 – 2010-07-14 20:13:30