在ListBox中添加ToggleButton(有IsChecked属性)
Xaml文件:
<ListBox?Name="lbTasteSet" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding TasteSet}" Style="{StaticResource MultipleToggleButtonList}"
SelectionMode="Multiple">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding GetTasteCommand}"
CommandParameter="{Binding ElementName=lbTasteSet}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" ItemHeight="30" ItemWidth="60"></WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Xaml中用到的Style:因為ToggleButton相當于ListBoxItem的Content,這時要使togglebutton的IsSelected屬性與ListBox的SelectedItem的IsSelect屬性關聯則需要在Style中設置下劃線所示Setter
<Style TargetType="ListBox" x:Key="MultipleToggleButtonList">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Transparent" Margin="{TemplateBinding Padding}">
<telerik:RadToggleButton Content="{Binding Path=Taste.Name}"
IsChecked="{Binding Path=IsSelected,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"> ? </telerik:RadToggleButton>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
?
ViewModel:用到了(lanmuda)表達式
/// <summary>
/// 獲取選擇的口味
/// </summary>
public void ExecuteGetTaste(ListBox lb)
{
//TasteItemViewModel model = lb.SelectedItem as TasteItemViewModel;
List<Taste> selectedTastes= this.TasteSet.Where(i => i.IsSelected == true).Select(i => i.Taste).ToList();
selectedTastes.Distinct();
string strTaste = "";
foreach (var item in selectedTastes)
{
strTaste += "["+item.Name+"]";
}
this.DishTaste = strTaste;
}
轉載于:https://www.cnblogs.com/gnsds/p/3671955.html
總結
以上是生活随笔為你收集整理的在ListBox中添加ToggleButton(有IsChecked属性)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS UITableView
- 下一篇: 总结一下一般游戏中3D模型各种勾边方法遇