选择行时修改ListView的行行为

时间:2012-09-06 14:14:49

标签: c# .net wpf listview

我有一个基本的ListView,其XAML可以在下面找到。基本上,在我填充它之后,我希望能够删除它们,因此“X”按钮:

<ListView Name="selectedPeople" ItemsSource="{Binding Path=map, RelativeSource={RelativeSource AncestorType=Window},
                Mode=OneWay}" Width="480" Height="200" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,225,10,0"
                SelectionChanged="selectedPeople_SelectionChanged">
         <ListView.View>
            <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Broadcast call targets">
               <GridViewColumn DisplayMemberBinding="{Binding Path=Key}"   Header="ID"      Width="120" />
               <GridViewColumn DisplayMemberBinding="{Binding Path=Value}" Header="Description" Width="320" />
               <GridViewColumn Header="" Width="30">
                  <GridViewColumn.CellTemplate>
                     <DataTemplate>
                        <Button Content=" X " Visibility="Hidden" />
                     </DataTemplate>
                  </GridViewColumn.CellTemplate>
               </GridViewColumn>
            </GridView>            
         </ListView.View>      
      </ListView>

我默认情况下保留了X按钮不可见,因为所需的行为是它仅在选择行时显示。如何做到这一点,即改变所选更改按钮的可见性?此外,单击后,如何关联按钮所关联的行/项?

3 个答案:

答案 0 :(得分:3)

  1. 将按钮绑定到item.IsSelected

    <Button Content=" X " Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}} Converter={StaticResource yourConverter}}" Command="{Binding DeleteCommand}"/>
    
  2. 将转换器从bool写入Visibility(IValueConverter

    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class BoolToVisibilityConverter : IValueConverter
    {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
        var boolValue = (bool) value;
        return boolValue ? Visibility.Visible : Visibility.Collapsed;
      }
    
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
        throw new NotImplementedException();
      }
    }
    
  3. 在ViewModel中创建DeleteCommand(通过实现ICommand接口)绑定到ListView中的项目并将按钮绑定到它

    public class ItemViewModel {
      ICommand DeleteCommand {get;set;}
    }
    

答案 1 :(得分:3)

您可以使用IsEnabled属性更改按钮列模板,如下所示。

<GridViewColumn Width="30" Header="">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content=" X " IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

或者你可以使用转换器或触发器来实现可见性。

 <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter1" />
    </Window.Resources>
  <GridViewColumn Width="30" Header="">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>

                                    <Button Content=" X " Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverter1}}" />
                                </StackPanel>

                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

答案 2 :(得分:1)

试试这个:

    <ListView Name="selectedPeople" ItemsSource="{Binding Path=map, RelativeSource={RelativeSource AncestorType=Window},
                    Mode=OneWay}" Width="480" Height="200" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,225,10,0"
                   >
 <ListView.Resources>
                <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter1" />
            </ListView.Resources>
             <ListView.View>
                <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Broadcast call targets">
                   <GridViewColumn DisplayMemberBinding="{Binding Path=Key}"   Header="ID"      Width="120" />
                   <GridViewColumn DisplayMemberBinding="{Binding Path=Value}" Header="Description" Width="320" />
                   <GridViewColumn Header="" Width="30">
                      <GridViewColumn.CellTemplate>
                         <DataTemplate>
                            <Button Content="X" 
                                        Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Converter={StaticResource BooleanToVisibilityConverter1}}"
                                        Command="{Binding DeleteCommand}"
                                        CommandParameter="{Binding}"
                                        />
                         </DataTemplate>
                      </GridViewColumn.CellTemplate>
                   </GridViewColumn>
                </GridView>            
             </ListView.View>      
          </ListView>

不要忘记创建Create Deletecommand