Tag: 依赖项属性

自定义控件中的依赖项属性意外地共享内存/值

我有以下设置: 自定义WPF控件(基类),派生自Canvas 该基类的实现 该实现的ObservableCollection依赖项属性 我有一个测试应用程序,显示我的自定义控件的三个唯一实例(例如 ,Test2,Test3等)。 当我运行并调试应用程序时, ObservableCollection的内容对于控件的所有三个实例都是相同的。 为什么是这样? 图表: [ContentProperty(“DataGroups”)] public abstract class Chart : Canvas { static Chart() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Chart), new FrameworkPropertyMetadata(typeof(Chart))); } public ObservableCollection DataGroups { get { return (ObservableCollection)GetValue(DataGroupsProperty); } set { SetValue(DataGroupsProperty, value); } } public static readonly DependencyProperty DataGroupsProperty = DependencyProperty.Register(“DataGroups”, typeof(ObservableCollection), typeof(Chart), new FrameworkPropertyMetadata(new ObservableCollection(), FrameworkPropertyMetadataOptions.AffectsArrange)); public abstract […]

WPF自定义控件:集合类型的DependencyProperty

我有一个包含ListBox的CustomControl : 我使用Code Behind中的属性绑定ItemsSource : public partial class CustomList : UserControl, INotifyPropertyChanged { public CustomList( ) { InitializeComponent( ); } public ObservableCollection ListSource { get { return (ObservableCollection)GetValue( ListSourceProperty ); } set { base.SetValue(CustomList.ListSourceProperty, value); NotifyPropertyChanged( “ListSource” ); } } public static DependencyProperty ListSourceProperty = DependencyProperty.Register( “ListSource”, typeof( ObservableCollection ), typeof( CustomList ), new PropertyMetadata( […]

在自定义用户控件的DependencyProperty上绑定不更新更新

我在自定义用户控件上进行数据绑定时遇到了困难。 我创建了一个示例项目来突出我的问题。 我是WPF的新手,也是MVVM的新手,所以请耐心等待…… 我创建了一个使用数据绑定两种方式的简单视图。 内置控件上的数据绑定工作正常。 我的自定义控件没有…我在我的控件的PropertyChangedCallback中放了一个断点。 它在启动时被击中一次,但从未再次被击中。 与此同时,我已经绑定到相同价值的标签很高兴倒计时。 我错过了什么? 我的示例项目如下: 主窗口: 这是我的模特: namespace WpfMVVMApp { public class CountdownModel : INotifyPropertyChanged { private int chargeTimeRemaining_Mins; public int ChargeTimeRemaining_Mins { get { return chargeTimeRemaining_Mins; } set { chargeTimeRemaining_Mins = value; OnPropertyChanged(“ChargeTimeRemaining_Mins”); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged […]

无法将lambda表达式转换为类型’System.Delegate’,因为它不是委托类型

我为class1定义了一个依赖属性,它引发了一个事件。 我不知道为什么它给我这个错误“无法将lambda表达式转换为’System.Delegate” public static readonly DependencyProperty class1Property = DependencyProperty.Register(“class1Property”, typeof(Class1), typeof(UserControl1), new PropertyMetadata(null)); public Class1 class1 { get { return Dispatcher.Invoke((() => GetValue(class1Property)))as Class1; } set { Dispatcher.Invoke(new Action(() => { SetValue(class1Property, value); })); } } 非常简单的Class1代码: public class Class1 { public delegate void myhandler(object sender, EventArgs e); public event myhandler test; public void connection() […]

使用DependencyProperty进行可见性绑定

我在下面的一些简单代码中使用了ToggleButton.IsChecked属性来设置TextBlock的可见性。 它工作正常。 由于这不适合我的程序结构,我试图将另一个TextBlock的可见性绑定到“this”的DependencyProperty。 它编译很好,但它没有产生任何影响。 我做错了什么,只是不确定是什么。 XAML C# using System.Windows; namespace ToggleButtonTest { public partial class MainWindow : Window { static MainWindow() { FrameworkPropertyMetadata meta = new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault); ShowMoreTextProperty = DependencyProperty.Register(“ShowMoreText”, typeof(bool), typeof(MainWindow), meta); } public MainWindow() { InitializeComponent(); } public static readonly DependencyProperty ShowMoreTextProperty; public bool ShowMoreText { get { return (bool)GetValue(ShowMoreTextProperty); } set […]

WPF依赖属性:为什么我需要指定所有者类型?

这是我注册DependencyProperty : public static readonly DependencyProperty UserProperty = DependencyProperty.Register(“User”, typeof (User), typeof (NewOnlineUserNotifier)); public User User { get { return (User)GetValue(UserProperty); } set { SetValue(UserProperty, value); } } DependencyProperty.Register方法的第三个参数要求您指定依赖项属性所在的Control的类型(在本例中,我的用户控件称为NewOnlineUserNotifier )。 我的问题是, 为什么你实际指定了所有者的类型,如果指定的类型与实际所有者不同,会发生什么?

WPF财产系统如何经济?

据说WPF的设计者已经使它具有经济性或更高的性能。 有人可以解释一下在WPF属性系统更经济的情况下会发生什么事情的例子吗?

强制依赖属性需要什么?

我看到了一个有2个依赖属性的例子: public static readonly DependencyProperty CurrentReadingProperty = DependencyProperty.Register(“CurrentReading”, typeof(double), typeof(Gauge), new FrameworkPropertyMetadata(Double.NaN, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnCurrentReadingChanged), new CoerceValueCallback(CoerceCurrentReading) ), new ValidateValueCallback(IsValidReading) ); 和 public static readonly DependencyProperty MinReadingProperty = DependencyProperty.Register( “MinReading”, typeof(double), typeof(Gauge), new FrameworkPropertyMetadata( double.NaN, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnMinReadingChanged), new CoerceValueCallback(CoerceMinReading) ), new ValidateValueCallback(IsValidReading)); 在OnCurrentReadingChanged中我执行以下操作d.CoerceValue(MinReadingProperty); 它调用CoerceValueCallback委托(“CoerceMinReading”),它具有以下代码: private static object CoerceMinReading(DependencyObject d, object value) { Gauge g […]

WPF自定义控件,DependencyProperty问题

我已经使用自定义控件设置了测试代码: /// /// Interaction logic for UCTest.xaml /// public partial class UCTest : UserControl { public static readonly DependencyProperty LastNameProperty = DependencyProperty.Register(“LastName”, typeof(string), typeof(UCTest), new PropertyMetadata(“No Name”, LastNameChangedCallback, LastNameCoerceCallback), LastNameValidateCallback); private static void LastNameChangedCallback( DependencyObject obj, DependencyPropertyChangedEventArgs e) { Console.WriteLine(e.OldValue + ” ” + e.NewValue); } private static object LastNameCoerceCallback(DependencyObject obj, object o) { string […]

DLL的依赖对象/属性

这是我的代码: public class MyObject : DependencyObject { public int speedSimu { get { return (int)GetValue(speedSimuProperty); } set { SetValue(speedSimuProperty, value); } } public static readonly DependencyProperty speedSimuProperty = DependencyProperty.Register(“speedSimu”, typeof(int), typeof(MyObject), new PropertyMetadata(0)); public int Value { get; set; } } public class Timer : DependencyObject { public string description { get { return (string)GetValue(descriptionProperty); […]