How to remove NewItemPlaceholder at TreeView wpf

后端 未结 4 579
野性不改
野性不改 2021-01-16 05:04

I bind ObservableCollection to ListView and I get {NewItemPlaceholder} line at the end. How can I hide or remove that line?

4条回答
  •  礼貌的吻别
    2021-01-16 05:36

    Just set an ItemTemplate in the XAML for the ListView (in my example a TreeView, where the same failure happens), and bind it to a filter converter. This could look like this:

    
        
                              
        
    
    

    Now you have to add a Filter-Converter-Class like this:

    class IgnoreNewItemPlaceholderConverter : IValueConverter {
        public static readonly IgnoreNewItemPlaceholderConverter Instance = new IgnoreNewItemPlaceholderConverter();
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            if (value != null && value.ToString() == "{NewItemPlaceholder}")
                return DependencyProperty.UnsetValue;
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
    

    And don't forget to map this Filterclass a static resource in your XAML with something like this:

    
    

    This is caused by a DataGrid beeing bound to the same datasource, that is not "IsReadOnly=True" (writable). If this still fits your requirements, you can also try to make the DataGrid readonly or to bind to a different datasource.

提交回复
热议问题