0

我有一个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();
    }
}

请帮帮我谢谢

4

1 回答 1

0

您知道,如果您使用 a 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>
于 2012-04-11T20:58:15.010 回答