【问题标题】:Making item selected of a ListView of ToggleButtons从 ToggleButtons 的 ListView 中选择项目
【发布时间】:2015-11-02 19:17:29
【问题描述】:

我有一个 ListView,每个项目都有一个切换按钮。我希望能够在从列表中选择项目时切换按钮,并在取消选择项目时取消切换。它必须遵循mvvm,所以后面没有代码。

这是我的设置:

            <ListView x:Name="stampList"
                    ItemsSource="{Binding AllStampImages}">

                <ListView.ItemTemplate>              
                    <DataTemplate>

                        <StackPanel Orientation="Vertical" Margin="0,2,0,0">
                            <ToggleButton Width="72" 
                                        Height="72"
                                        Command="{Binding StampSelectedCommand}"/>
                        </StackPanel>

                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

我的问题是,当我点击切换按钮时,项目没有被选中。同样,当我在切换按钮之外(仍在 listView 项目的边界内)点击时,该项目被选中但按钮未切换。

如何将两者联系在一起?

【问题讨论】:

    标签: c# .net wpf xaml listview


    【解决方案1】:

    您可能必须将Selector.IsSelected 属性绑定到您的自定义属性。在这种情况下,您切换按钮的属性。

    尝试类似:

            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="Selector.IsSelected" Value="{Binding [Value of your toggle button], Mode=TwoWay}" />
                </Style>
            </ListView.ItemContainerStyle>
    

    【讨论】:

      【解决方案2】:

      最简洁的方法是拥有由 ViewModel 或 Model 层提供的项目集合。

      1. 每个项目都应该有一个 IsSelected 布尔属性:
      class LineItem
      {
          private bool isSelected;
          public bool IsSelected
          {
              get { return isSelected; }
              set { isSelected = value; }
          }
          private String label;
          public String Label
          {
              get { return label; }
              set { label = value; }
          }
      }
      
      1. 应该有一些绑定到模型 IsSelected 属性:

      2.1 在 ListView.ItemTemplate 上

      <ListView x:Name="list1" SelectionMode="Multiple" ItemContainerStyle="{DynamicResource ListViewItemStyle1}" Margin="0,0,334,0">
          <ListView.ItemTemplate>
              <DataTemplate>
                  <ToggleButton Margin="5" Content="{Binding Label}" IsChecked="{Binding IsSelected}"/>
              </DataTemplate>
          </ListView.ItemTemplate>
      

      2.2 在 ListViewItem 模板上

      右键单击列表视图创建模板,

      在菜单中:“编辑附加模板/编辑生成的项目容器(空)。

      填写以下代码:"

      <Style x:Key="ListViewItemStyle1" TargetType="{x:Type ListViewItem}">
              <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type ListViewItem}">
                          <Grid x:Name="Bd">
                              <ContentPresenter  />
                          </Grid>
                          <ControlTemplate.Triggers>
                              <MultiTrigger>
                                  <MultiTrigger.Conditions>
                                      <Condition Property="IsSelected" Value="True"/>
                                  </MultiTrigger.Conditions>
                                  <Setter Property="Background" TargetName="Bd" Value="#FF204080"/>
                              </MultiTrigger>
                          </ControlTemplate.Triggers>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      

      触发器基于 Model IsSelected 属性。
      Grid 命名为 Bd 是为了在触发器中更改其背景。

      完整的工作演示链接:http://1drv.ms/1iyvPgt

      注意

      我试图仔细回答这个问题。 但以我的拙见,我不会使用 ListView 也许你有充分的理由我忽略了。

      我更喜欢使用 ItemsControl,它是一个不允许进行选择的列表框。

      如果需要,我会放置切换按钮,并使用自定义模板放置蓝色背景。
      它会删除所有触发的东西 , ...

      最佳编码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多