如何删除NewItemPlaceholder在TreeView控件WPF [英] How to remove NewItemPlaceholder at TreeView wpf
问题描述
我绑定的ObservableCollection到ListView和我得到在最后{NewItemPlaceholder}行。如何隐藏或删除行?
< TreeView控件的ItemsSource ={结合MyDataToDisplay}>
收藏是这样的:
的ObservableCollection< MyElement> MyDataToDisplay
只需设置一个ItemTemplate在XAML为ListView(在我的例子一个TreeView,其中相同的故障发生),并将其绑定到一个过滤器。这可能是这样的:
< TreeView.ItemTemplate>
< HierarchicalDataTemplate的ItemsSource ={结合儿童}>
< ContentControl中含量={结合转换器= {的StaticResource IgnoreNewItemPlaceholderConverter}}/>
< / HierarchicalDataTemplate>
< /TreeView.ItemTemplate>
现在你需要添加一个过滤器,转换器级是这样的:
类IgnoreNewItemPlaceholderConverter:{的IValueConverter
公共静态只读IgnoreNewItemPlaceholderConverter实例=新IgnoreNewItemPlaceholderConverter();
公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化){
如果(值= NULL和放大器;!&安培; value.ToString()=={NewItemPlaceholder})
返回DependencyProperty.UnsetValue;
返回值;
}
公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化){
抛出新的NotImplementedException();
}
}
和不要忘记像这样的东西映射此Filterclass静态资源在您的XAML:
<转换器:IgnoreNewItemPlaceholderConverter X:关键=IgnoreNewItemPlaceholderConverter/>
这是由一个DataGrid beeing绑定到同一个数据源,这不是的IsReadOnly =真(写)。如果这还符合你的要求,你也可以尝试让DataGrid中只读或绑定到不同的数据源。
I bind ObservableCollection to ListView and I get {NewItemPlaceholder} line at the end. How can I hide or remove that line?
<TreeView ItemsSource="{Binding MyDataToDisplay}">
Collection looks like:
ObservableCollection<MyElement> MyDataToDisplay
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.
这篇关于如何删除NewItemPlaceholder在TreeView控件WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!