Tag: dependency properties

C#:从超过1个类扩展

假设我有一个ViewModel TabViewModel ,它将ObservableObject扩展为MVVM Foundation Framework中ViewModels的类。 然后我还有一个扩展TabViewModel ,现在我需要扩展DependencyObject来实现DependencyProperties。 我不能超过1class。 我该如何实现呢? 我可以有一个“中间”类,如…… TabViewModel : ObservableObject EditorTabViewModel : TabViewModel DependentEditorTabViewModel : DependencyObject 但那是一个额外的不必要的课程。 有没有更好的方法呢? UPDATE Ops实际上我不能做到以上。 DependentEditorTabViewModel仍然需要扩展EditorTabViewModel …除了DependencyObject

如何使用MVVM双击列表框项来触发命令?

当用户双击列表框项目时,我正在尝试启动ICommand。 此外,我正在尝试使用MVVM模式。 在这个XAML中,按键“p”完美地工作。 当我双击列表框时,命令永远不会启动。 我设置了一个断点来确认没有双击调用“PlayVideoCommand”。 我错过了什么或者我是否必须使用Setter(我不熟悉)? 双击和“p”都应该执行相同的命令。 使用鼠标时,我可以看到listboxitem已被选中。 我有一个预感,MouseBinding Command属性不是依赖属性,但我不知道如何确认这一点。

如何在Silverlight中按名称获取DependencyProperty?

情况:我有一个字符串,表示Silverlight中TextBox的DependencyProperty的名称。 例如:“TextProperty”。 我需要获得TextBox的实际TextProperty的引用,它是DependencyProperty。 问题:如果我得到的只是属性的名称,我如何获得对DependencyProperty(在C#中)的引用? 诸如DependencyPropertyDescriptor之类的东西在Silverlight中不可用。 我似乎不得不求助于反思来获得参考。 有什么建议?

WPF,无法绑定属性,因为它说它不是DependencyProperty,但我注册了它

我有一个带有Template属性的UserControl ,我将其设置为DependencyProperty : public partial class TemplateDetail : UserControl { public static readonly DependencyProperty _templateProperty = DependencyProperty.Register( “Template”, typeof(Template), typeof(TemplateDetail) ); public TemplateDetail() { InitializeComponent(); Template = new Template(); DataContext = Template; } public Template Template { get { return (Template)GetValue(_templateProperty); } set { SetValue(_templateProperty, value); } } } 我正在尝试在XAML中使用此UserControl用于Window ,SaveTemplateDialog,并且我正在尝试将其Template属性设置为SaveTemplateDialog类中的Template属性: SaveTemplateDialog的DataContext设置为其Template属性,该属性也是依赖属性。 但是,对于上面的XAML, Template=”{Binding}”在Visual Studio中以蓝色下划线标出,它表示: […]

从DataTemplate UWP绑定UserControl DP

我有一个显示小雕像的FlipView 。 小雕像包含其图像的路径 。 将此属性绑定到常规DataTemplate是可以的。 (以下代码工作正常) 但是当使用我的UserControl时,它不再起作用了: 从未设置过FigurinePath DP。 (如果我使用硬编码字符串,那很好。)这是输出中的错误: 错误:BindingExpression路径错误:’Com.Test.ViewModels.UserControl.FigurineStickerUserControlViewModel,eSmart.ViewModels,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null’上找不到’Path’属性。 BindingExpression:Path =’Path’DataItem =’Com.Test.ViewModels.UserControl.FigurineStickerUserControlViewModel,Test.ViewModels,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null’; target元素是’Com.Test.Views.FigurineStickerUserControl’(Name =’pageRoot’); target属性是’FigurinePath’(类型’对象’) 看起来DataTemplate尝试将Figurine分配为UserControl的DataContext,然后从我的UC的DataContext中检索属性。 但我的UC有自己的DataContext(它的ViewModel),我不想删除它。 不幸的是,对于WinRT / UWP,我没有使用Binding可以做的FindAncestor技巧。 我已经尝试过这个:( FlipFigurine是FlipView对象) 它不起作用。 即使将DP更改为对象并尝试以下操作也不起作用,DP的设置器永远不会被调用。 但是日志中没有错误。 FigurinePath=”{Binding SelectedItem, ElementName=FlipFigurine}” 有没有办法访问实际的Figurine对象,只需将其Path属性绑定到我的UC的FigurinePath属性?

在wpf中限制附加的依赖属性

我想仅将依赖属性附加到特定控件。 如果这只是一种类型,我可以这样做: public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached(“MyProperty”, typeof(object), typeof(ThisStaticWrapperClass)); public static object GetMyProperty(MyControl control) { if (control == null) { throw new ArgumentNullException(“control”); } return control.GetValue(MyPropertyProperty); } public static void SetMyProperty(MyControl control, object value) { if (control == null) { throw new ArgumentNullException(“control”); } control.SetValue(MyPropertyProperty, value); } (所以:限制Get / Set-Methods中的Control类型) 但是现在我想允许该属性附加在不同类型的Control 。 您尝试使用该新类型为这两个方法添加重载,但由于“未知的构建错误,找到了模糊匹配”而无法编译。 […]

如何订阅更改DependencyProperty?

可能重复: 聆听依赖属性的变化 对不起我的英语。 我需要创建一个可以订阅更改DependencyProperty的类,并根据此属性的新值来执行某些操作。 像这样: MyClass obj = new MyClass(); obj.Subscribe(TextBox.TextProperty, myTextBox); 我怎样才能做到这一点?

是否评估了DependencyProperties绑定的顺序?

是什么决定了同一控件上多个DepdencyProperties的评估顺序? 我正在使用Extended WPF Toolkit PropertyGrid并且绑定了SelectedObject和PropertyDefinitions: 问题是OnSelectedObjectChanged从依赖项属性触发,并且在该更改的处理程序中它引用了PropertyDefinitions,它被视为null。 如果我注释掉OnSelectedObjectChanged处理程序,那么我可以看到调试OnPropertyDefinitionsChanged时调用OnSelectedObjectChanged。 public static readonly DependencyProperty PropertyDefinitionsProperty = DependencyProperty.Register( “PropertyDefinitions”, typeof( PropertyDefinitionCollection ), typeof( PropertyGrid ), new UIPropertyMetadata( null, OnPropertyDefinitionsChanged ) ); public PropertyDefinitionCollection PropertyDefinitions { get { return ( PropertyDefinitionCollection )GetValue( PropertyDefinitionsProperty ); } set { SetValue( PropertyDefinitionsProperty, value ); } } private static void OnPropertyDefinitionsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) […]

路由事件和依赖项属性.NET包装器混淆

我是WPF的新手,并且对于路由事件和依赖属性的语法包含了一些困惑,我在许多来源上看到路由事件和依赖属性被包装成这样 // Routed Event public event RoutedEventHandler Click { add { base.AddHandler(ButtonBase.ClickEvent, value); } remove { base.RemoveHandler(ButtonBase.ClickEvent, value); } } // Dependency Property public Thickness Margin { set { SetValue(MarginProperty, value); } get { return (Thickness)GetValue(MarginProperty); } } 我从未在C#中看到添加/删除/设置/获取关键字的类型。 这些是C#语言的一部分作为关键字,我从来没有经历过或使用它们,因为我没有在C#中工作,因为我是一名C ++程序员? 如果不是关键字,那么如果它们不是C#的一部分以及它们如何工作,它们如何被编译器处理

使用viewmodel绑定usercontrol的依赖属性

基本上,我有一个主窗口,其上有一个用户控件,其中包含一个绑定到主窗口视图模型的属性。 这个想法是当属性在用户表单中发生变化时,通过绑定主窗口视图模型中的属性也会发生变化。 问题是,当用户控件没有ViewModel时,这是有效的,当我向用户控件添加一个简单的ViewModel时,绑定不再起作用。 因为我需要为我的控件设置ViewModel,所以我需要弄清楚为什么添加ViewModel会破坏绑定以及如何修复它。 欢迎任何建议。 public partial class Control1 : UserControl, INotifyPropertyChanged { public Control1() { InitializeComponent(); Loaded += Control1_Loaded; } void Control1_Loaded(object sender, RoutedEventArgs e) { DataContext = new Control1ViewModel(); } public static DependencyProperty SavedStringProperty = DependencyProperty.Register( “SavedString”, typeof(string), typeof(Control1)); public string SavedString { get { return (string)GetValue(SavedStringProperty); } set { SetValue(SavedStringProperty, value); } […]