Mik*_*ike 5 dependency-properties windows-runtime
我已经仔细搜索过了,因为我知道有很多关于依赖属性的内容,但我还没有看到任何有效的解决方案.我正在尝试将ObservableCollection从我的ViewModel绑定到我的AutoCompleteBox.我的ViewModel正在返回数据,Getter正在被击中.但是,之后,控件的SetValue或OnItemsSourcePropertyChanged不会触发.有什么可能是错的想法吗?
我有一个像这样的控件:
[ContentProperty(Name = "ItemsSource")]
public partial class AutoCompleteBox : Control
{
//local stuff
private ListBox lb;
private List<Person> _items;
private ObservableCollection<Person> _view;
public AutoCompleteBox() : base()
{
DefaultStyleKey = typeof(AutoCompleteBox);
Loaded += (sender, e) => ApplyTemplate();
}
protected override void OnApplyTemplate()
{
this.lb = this.GetTemplateChild("Selector") as ListBox;
base.OnApplyTemplate();
}
#region ItemsSource
public IEnumerable ItemsSource
{
get { return GetValue(ItemsSourceProperty) as ObservableCollection<Person>; }
set { SetValue(ItemsSourceProperty, value); } //Never gets called
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(
"ItemsSource",
typeof(IEnumerable),
typeof(AutoCompleteBox),
new PropertyMetadata(null, OnItemsSourcePropertyChanged));
private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Never gets called :(
}
#endregion
public String SearchText
{
get { return GetValue(SearchTextProperty) as String; }
set
{
SetValue(SearchTextProperty, value);
}
}
public static readonly DependencyProperty SearchTextProperty =
DependencyProperty.Register(
"SearchText",
typeof(String),
typeof(AutoCompleteBox),
new PropertyMetadata(null, OnSearchTextPropertyChanged));
private static void OnSearchTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//gets fired when loaded, with data being bound
}
}
Run Code Online (Sandbox Code Playgroud)
以下是它如何使用控件:
<toolkit:AutoCompleteBox Grid.Row="5" Grid.Column="2" ItemsSource="{Binding Persons,Mode=TwoWay}" SearchText="WhatTheHell"/>
Run Code Online (Sandbox Code Playgroud)
作为测试并且为了简单起见,我为SearchText创建了一个String DependencyProperty.如果我绑定SearchText,它会正常工作,调用OnSearchTextPropertyChanged:
<toolkit:AutoCompleteBox Grid.Row="5" Grid.Column="2" ItemsSource="{Binding Persons,Mode=TwoWay}" SearchText="{Binding SearchText}"/>
Run Code Online (Sandbox Code Playgroud)
有没有人遇到WinRT的这些问题?或者看到我在做什么有什么问题?
(这里BaseClass是一个抽象类或您从中派生的接口,以及您尝试绑定到依赖项属性的实例)
注册依赖项属性时需要更改typeof(BaseClass)为typeof(object).Getter和setter可能会继续使用您的BaseClass,但如果您为此依赖项属性设置了不同的类型实例,请注意InvalidCastException.
public BaseClass Item
{
get { return (BaseClass)GetValue(ItemProperty); }
set { SetValue(ItemProperty, value); }
}
public static readonly DependencyProperty ItemProperty =
DependencyProperty.Register("Item", typeof(object), typeof(MyUserControl), new PropertyMetadata(null));
Run Code Online (Sandbox Code Playgroud)
在代码中的某处创建一个虚拟依赖项属性(例如,在您尝试创建的依赖项属性之后),它将具有所有派生类型的类型.像这样:
#region DummyProperty
public DerivedClass1 Dummy
{
get { return (DerivedClass1)GetValue(DummyProperty); }
set { SetValue(DummyProperty, value); }
}
public static readonly DependencyProperty DummyProperty =
DependencyProperty.Register("Dummy", typeof(DerivedClass1), typeof(MyUserControl), new PropertyMetadata(default(DerivedClass1)));
#endregion
Run Code Online (Sandbox Code Playgroud)
这里DerivedClass1派生自BaseClass.
我想原因是XAML对你的派生类型一无所知,只知道BaseClass.因此,为每种类型创建虚拟依赖项属性应该可以解决问题.
我会尝试检查绑定是否使用 DebugConverter 触发 - 检查WinRT XAML 工具包中的BindingDebugConverter - 将其设置为绑定的转换器,并查看它是否以及何时中断(是在 Convert 或 ConvertBack 中,设置了什么值ETC。)。
如果这没有帮助 - 检查您的 DataContext 设置是否正确。
*编辑1
如果您正在查看的是绑定集合的项目中的更改 - 您可以检查 ItemsSource 是否为INotifyCollectionChanged,如果为真 - 订阅CollectionChanged事件以查看更改。否则 - 您可以修改控件以从ItemsControl继承并覆盖GetContainerForItemOverride和IsItemItsOwnContainerOverride等方法。
*编辑2
Windows 8 Consumer Preview 的框架中似乎存在与使用 IEnumerable 作为依赖属性类型相关的错误。使用对象作为类型解决了这个问题。如果启用本机代码调试,您将看到此绑定异常:
错误:转换器无法转换类型“System.Collections.ObjectModel.ObservableCollection`1[[ACBDP.Person,ACBDP,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]],System,Version=4.0.0.0 类型的值, Culture=neutral, PublicKeyToken=b77a5c561934e089' 键入 'IEnumerable';BindingExpression: Path='Persons' DataItem='ACBDP.BlankPageViewModel, ACBDP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; 目标元素是 'ACBDP.AutoCompleteBox' (Name='null'); 目标属性是“ItemsSource”(类型“IEnumerable”)。
| 归档时间: |
|
| 查看次数: |
2635 次 |
| 最近记录: |