• 在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;
    }

  • 相关阅读:
    ffplay 2.5.3 媒体播放器
    MinGW/MSYS 交叉编译环境搭建
    python chm 中文帮助 (2.7 和 3.4)
    wx.html2.WebView在 target="_blank" or rel="external" 没有反映的解决方法
    韩星5,6号 一锅双星技巧
    暖房子工程
    CStringUtf8ToUnicode
    燃气灶中心炉芯帽子生锈了,如何拆不下来?
    翻窗户消失的百岁老人/百岁老人跷家去 中文字幕
    CPinyin unicode汉字查找拼音(支持多音字)
  • 原文地址:https://www.cnblogs.com/gnsds/p/3671955.html
Copyright © 2020-2023  润新知