How to remove NewItemPlaceholder at TreeView wpf

2019-03-02 04:20发布

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

enter image description here

<TreeView ItemsSource="{Binding MyDataToDisplay}">

Collection looks like:

ObservableCollection<MyElement> MyDataToDisplay

4条回答
Bombasti
2楼-- · 2019-03-02 04:54

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:

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
        <ContentControl Content="{Binding Converter={StaticResource IgnoreNewItemPlaceholderConverter}}"/>                  
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

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:

<converter:IgnoreNewItemPlaceholderConverter x:Key="IgnoreNewItemPlaceholderConverter"/>

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-03-02 04:55

Old question but just came across this problem. I have a treeview & datagrid bound to the same datasource; however making the datagrid IsReadOnly=true wasn't an option because I need to be able to edit the datagrid. I found that setting CanUserAddRows=false fixed the issue and still allowed me to edit the Datagrid.

查看更多
霸刀☆藐视天下
4楼-- · 2019-03-02 05:06

Like alot of people, I had the same issues but after some thinking, it occured to me that I could just create a bool property in the ViewModel and bind it to the DataGrid CanUserAddRows Property like below. Then I can make the property true or false as needed:

private bool _canUserAddRows;
    public bool CanUserAddRows
    {
        get { return _canUserAddRows; }
        set
        {
            _canUserAddRows = value;

            NotifyPropertyChanged("CanUserAddRows");
        }
    }

<--!--DataGrid property --!-->
CanUserAddRows="{Binding CanUserAddRows,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

This is easier and works very well. Hope it helps you.

查看更多
放荡不羁爱自由
5楼-- · 2019-03-02 05:17

Suddenly I solved my problem. The problem was that on the same window I had a DataGrid binded to the same collection. I think that I had to make the DataGrid readonly to eliminate that row in the TreeView. However the DataGrid was just for testing purpose so the problem is gone.

查看更多
登录 后发表回答