我有DataGrid
。我正在加载DataGrid
ViewModel
,ItemSource
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
答案 0 :(得分:0)
看到它们与众不同并不奇怪。 HasItems
并非简单地实现为Items.Count > 0
。实际上HasItems
在Items
更改后得到解决。
因此,如果您在更改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);
...
}
如你所见,它们并不同步。如果您让我们知道您的附属财产(在您的问题中提到)是什么,将会更有帮助。