如何使用组合编辑列表视图行

时间:2012-04-08 07:24:19

标签: wpf user-controls wpf-controls

我有ListView的行。当我点击一个特定的单元格时,我希望所选的单元格可以使用组合框进行编辑,而事情是我已经完成了,但是编辑框仍然在编辑后仍然存在。我希望组合框更改回文本块。

        <Style TargetType="{x:Type FrameworkElement}" 
   x:Key="GridEditStyle">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Visibility" 
     Value="{Binding Path=IsSelected, 
         RelativeSource={RelativeSource FindAncestor, 
             AncestorType={x:Type ListViewItem}},
         Converter={StaticResource boolToVis}, 
             ConverterParameter=True}" />
        </Style>

 <ComboBox SelectedItem="Present"
                                                           ItemsSource="{Binding ElementName=This, 
                                                            Path=AvailablePublishers}" 
                                                            Style="{StaticResource GridEditStyle}" />

背后的代码是

   private ObservableCollection<string> _AvailablePublishers =
 new ObservableCollection<string>();
    public Student_Attendance()
    {
        InitializeComponent();
        _AvailablePublishers.Add("Present");
        _AvailablePublishers.Add("Absent");
        _AvailablePublishers.Add("Late");



    }
    public ObservableCollection<string> AvailablePublishers
    { get { return _AvailablePublishers; } }

  public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
      object parameter, System.Globalization.CultureInfo culture)
    {
        bool param = bool.Parse(parameter as string);
        bool val = (bool)value;

        return val == param ? Visibility.Visible : Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType,
      object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

请帮助我 谢谢

1 个答案:

答案 0 :(得分:0)

你知道如果你使用DataGrid,你就不必为切换查看和编辑模板做任何事情,它会自动完成......这里有一个让你入门的例子:

<DataGrid  ItemsSource="{Binding ...}">
  <DataGrid.Columns>
      <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock DataContext="{Binding ...}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding ElementName=This, Path=AvailablePublishers}" SelectedItem="{Binding ...}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>