Items.Count>时,DataGrid HasItems为false。 0为什么?

时间:2015-10-12 16:34:11

标签: c# wpf datagrid itemscontrol

我有DataGrid。我正在加载DataGrid ViewModelItemSource Binded ViewModel property加载TreeView。当我点击TreeViewItem中的某个项目并基于DataGrid我正在从ViewModel更改PropertyChanged的集合并提升DataGrid HasItems。我有条件检查AttachedProperty's CoerceValueCallBack中的false并返回Items.Count > 0,但同时我检查true if the items count is greater than 0; otherwise, false.The default is false.是大于0。

来自MSDN

  

ItemsControl.HasItems属性

     

HasItems

我想知道为什么Items.Count返回false并且 public static bool GetIsFocused(DependencyObject obj) { return (bool)obj.GetValue(IsFocusedProperty); } public static void SetIsFocused(DependencyObject obj, bool value) { obj.SetValue(IsFocusedProperty, value); } public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached( "IsFocused", typeof(bool), typeof(EDataGridCellFocus), new UIPropertyMetadata(false, null, OnCoerceValue)); private static object OnCoerceValue(DependencyObject d, object baseValue) { if (((DataGrid)d).HasItems) // (((DataGrid)d).Items.Count > 0) is true but (((DataGrid)d).HasItems) is false { //Some Code } } 大于零?

我的附属物

keyboard focus

此附加属性用于在我的TreeViewItem上选项卡时设置网格的TreeView

DataGrid和我的UserControl分为两个 <DataGrid Grid.Row="1" ItemsSource="{Binding ListOfDocuments}" SelectedItem="{Binding CurrentDocument,Mode=TwoWay}" SelectionMode="Single" GridLinesVisibility="Horizontal" AutoGenerateColumns="False" IsReadOnly="True" RowHeight="25" CanUserAddRows="False" utility:EDataGridCellFocus.IsFocused="{Binding IsGridFocused}">

我的DataGrid

 public List<DocumentModel> ListOfDocuments
    {
        get { return _ListOdDocuments; }
        set { _ListOdDocuments = value; NotifyPropertyChanged(); }
    }

我的收藏,

 if(ListOfDocuments==null)
    {
       ListOfDocuments = new List<DocumentModel>();
    }
 ListOfDocuments.Clear();
 foreach (var item in DocumentsFromDataBase)
 {
     ListOfDocuments.Add(item);//item is kind of DocumentModel
 }

将项目添加到我的收藏中

function GetStateByZip(zip) {

    $.post('../post_zip.php', {         
        Zip: zip,
        submit: 'yes'}, function(data) {

        if(data.match('success')) {

            $('#State').fadeIn('fast');
            var state = data.substring(7);

            $('#State').val(state);

        } else {

            $('#State').fadeOut('fast');
            $('#State').val("");
            $('#responseBox').fadeIn('fast');
            $('#response').html("Zip code is not valid.").fadeIn('slow');
            FadeMsg();
        }
    }, 'text');

THIS IS THE ERROR
POST http://localhost/iss/contact/post_zip.php 500 (Internal Server Error)
f.support.ajax.f.ajaxTransport.c.send @ jquery.min.js:4
f.extend.ajax @ jquery.min.js:4
f.each.f.(anonymous function) @ jquery.min.js:4
GetStateByZip @ (index):95
onchange @ (index):38

enter image description here

1 个答案:

答案 0 :(得分:0)

看到它们与众不同并不奇怪。 HasItems并非简单地实现为Items.Count > 0。实际上HasItemsItems更改后得到解决。 因此,如果您在更改Items的时间与解决HasItems之前的时间之间执行任何操作,您会发现它们不同。

通过查看ItemsControl的源代码,您会发现HasItems实际上是HasItemsProperty的包装,并且定义如下:

[Bindable(false), Browsable(false)]
public bool HasItems {
        get { return (bool) GetValue(HasItemsProperty); }
}

HasItemsProperty仅在Items的集合更改处理程序中更新,此处位于源代码中:

((INotifyCollectionChanged)_items).CollectionChanged += 
        new NotifyCollectionChangedEventHandler(OnItemCollectionChanged2);
//the handler
private void OnItemCollectionChanged2(object sender, NotifyCollectionChangedEventArgs e){
        SetValue(HasItemsPropertyKey, (_items != null) && !_items.IsEmpty);   
        ...
}

如你所见,它们并不同步。如果您让我们知道您的附属财产(在您的问题中提到)是什么,将会更有帮助。

相关问题