3 回答
TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個贊
IsSelected向您的孩子ViewModel 添加一個屬性(OrderViewModel針對您的情況):
public bool IsSelected { get; set; }
將容器上的選定屬性綁定到此(在這種情況下為ListBox):
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
IsSelected 更新以匹配容器上的相應(yīng)字段。
您可以通過執(zhí)行以下操作在視圖模型中獲取選定的子項(xiàng):
public IEnumerable<OrderViewModel> SelectedOrders
{
get { return Orders.Where(o => o.IsSelected); }
}
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個贊
可以嘗試創(chuàng)建附加屬性。
這樣做將避免IsSelected為您綁定的每個列表添加屬性。我已經(jīng)完成了ListBox,但是可以對其進(jìn)行修改以在列表視圖中使用。
<ListBox SelectionMode="Multiple"
local:ListBoxMultipleSelection.SelectedItems="{Binding SelectedItems}" >
更多信息:WPF –綁定ListBox SelectedItems –附加屬性VS Style。
- 3 回答
- 0 關(guān)注
- 879 瀏覽
添加回答
舉報
