ListView的可编辑化
WPF经常用于显示数据报表,但是使用ListView只能完成显示作用,最近看到一帖子,上面详细讲述了怎么设置。我也尝试的做了一下:
具体操作就是把listview建立celltemplate,然后进行编辑,完成数据处理。
代码如下:
<DockPanel Grid.Row="1">
<ListView x:Name="listView" IsSynchronizedWithCurrentItem="True">
<ListView.Resources>
更改bool属性,满足wpf需要;
<local:BooleanToVisibilyBox x:Key="BooleanToVisibilyBox" />
<local:BooleanToVisibilyBlock x:Key="BooleanToVisibilyBlock" />
<Style x:Key="ListViewItemBlockStyle" TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="80" />
定义显示框内容:
<Setter Property="Visibility" Value="{Binding Path=IsSelected,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListViewItem}},
Converter={StaticResource BooleanToVisibilyBlock},
ConverterParameter=False }">
</Setter>
</Style>
定义编辑框内容:
<Style x:Key="ListViewItemBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="80" />
<Setter Property="Visibility" Value="{Binding Path=IsSelected,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListViewItem}},
Converter={StaticResource BooleanToVisibilyBox},
ConverterParameter=True}">
</Setter>
</Style>
</ListView.Resources>
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridViewColumn Header="用户号码" Width="80" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Style="{ StaticResource ListViewItemBlockStyle}" Text="{Binding Path=UserPrimaryID}" />
<TextBox Style="{StaticResource ListViewItemBoxStyle}" Text="{Binding Path=UserPrimaryID}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="用户名" Width="80" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Style="{ StaticResource ListViewItemBlockStyle}" Text="{Binding Path=UserName}" />
<TextBox Style="{StaticResource ListViewItemBoxStyle}" Text="{Binding Path=UserName}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="用户密码" Width="80" DisplayMemberBinding="{Binding UserPwd}"/>
<GridViewColumn Width="80" Header="真实姓名">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Style="{StaticResource ListViewItemBlockStyle}" Text="{Binding Path=UserRealName}"></TextBlock>
<TextBox Style="{StaticResource ListViewItemBoxStyle}" Text="{Binding Path=UserRealName}"></TextBox>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</DockPanel>
中途会涉及到BooleanVisibility的设置:代码如下:
public class BooleanToVisibilyBlock:IValueConverter { /// <summary> /// 更改对象处理,将bool型数据转换为wpf可用的类型 /// </summary> /// <param name="value">需要转换的值</param> /// <param name="targetType">目标类型</param> /// <param name="parameter">参数</param> /// <param name="cultureinfo">设置格式</param> /// <returns></returns> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { bool bBoolean = (bool)value; bool target = bool.Parse(parameter.ToString()); if (bBoolean == true) { return Visibility.Hidden; } else { return Visibility.Visible; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { Visibility visibility = (Visibility)value; if ( visibility == Visibility.Visible) { return true; } return false; } }
有这两步配置,剩下的就是一些繁琐的数据登记了!
如果你在list中设置了button或者其它需要选择的单项,你在MVVM绑定的时候,绑定方式与单独的绑定不相同,需要设置如下:
Command="{Binding ElementName=LVShowTable,Path=DataContext.UpdateCommand}"
command="{Binding Path=UpdateCommand,Source={StaticResource XXXViewModel}}"