Tag: 依赖项属性

必须在与DependencyObject相同的Thread上创建DependencySource

我有一个用wpf编写的应用程序,它可以下载一些网页,解析HTML代码并保存一些值。 class ListOfItems { public List ListToBind; public void DownloadItems() { Task.Factory.StartNew(() => { … … if (OnDownloadCompleted != null) OnDownloadCompleted(this, EventArgs.Empty); } } } class SomeObject { public string NameOfItem; public MyClass Properties; } class MyClass { public int Percentage; public SolidColorBrush Color; } 这是我正在使用的对象模型。 它是简化版本,我不希望你重新组织它,我有这样写道的原因。 在ListOfItems类中是执行所有工作的方法(内部使用一些其他方法使代码可读) – 下载源代码,使用数据解析和填充ListToBind ,fe [0] => NameOfItem = […]

知道是否在XAML中设置了DependencyProperty

我有一个控件,它暴露了一个名为PlacementTarget的DP(它绑定到子弹出FWIW)。 我想要做的是如果没有在XAML中设置PlacementTarget,那么(控件将)将它设置为控件所在的Window。当我说’not set’时,我并不仅仅意味着’is null’(这允许用户dev将PlacementTarget设置为null,并且控件不会自动将其设置为Window)。 我有一个名为placementTargetIsSet的字段,我在Change事件处理程序中将其设置为true public readonly static DependencyProperty PlacementTargetProperty = DependencyProperty.Register( “PlacementTarget”, typeof(UIElement), typeof(MyControl), new PropertyMetadata(new PropertyChangedCallback(PlacementTargetChanged))); static void PlacementTargetChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e) { MyControl ctrl = (sender as MyControl); ctrl.placementTargetIsSet = true; } public UIElement PlacementTarget { get { return (UIElement)GetValue(PlacementTargetProperty); } set { SetValue(PlacementTargetProperty, value); } } 但是我发现更改的事件发生在OnApplyTemplate和Loaded事件之后。 即当OnApplyTemplate或Loaded发生时,placementTargetIsSet == false,无论是否已设置PlacementTarget(在XAML中)。 […]

来自字符串的DependencyProperty

如何将属性名称(在字符串中)转换为DependencyProperty ? 我有一组属性名称,它在string中的值和DependencyObject 。 现在我想将这些属性值设置为DependencyObject 。 有关如何实现这一点的任何想法? 谢谢。

使用值绑定分配的依赖项属性不起作用

我有一个带有依赖项属性的usercontrol。 public sealed partial class PenMenu : UserControl, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public bool ExpandCollapse { get { return false; } set { //code } } public static readonly DependencyProperty ExpandCollapseProperty = DependencyProperty.Register(“ExpandCollapse”, typeof(bool), typeof(PenMenu), null); //some more […]

ListView中的UserControl DependencyProperty绑定

当我尝试在ListView中使用自定义UserControl时,它会失败并且只显示空块(但下面的TextBlock可以正常工作)。 虽然ListView外部的customControl工作得很好。 有什么问题? MainWindow.xaml MainWindow.cs public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); InitializeMyComponent(); } public System.Collections.ObjectModel.ObservableCollection CustomCollection { get; set; } private void InitializeMyComponent() { this.CustomCollection = new System.Collections.ObjectModel.ObservableCollection(); this.CustomCollection.Add(new CustomClass() { Number = 1, Text = “a” }); this.CustomCollection.Add(new CustomClass() { Number = 2, Text = “b” }); this.CustomCollection.Add(new CustomClass() […]

集合依赖属性

我有一个自定义控件,它具有ObservableCollection类型的DependencyProperty ,它绑定到observableCollection : <MyControl MyCollectionProperty = {Binding MyObservableCollection} … 问题是添加到MyObservableCollection不会更新MyCollectionProperty 。 我需要完全替换MyObservableCollection以使其工作,例如 MyObservableCollection = null; MyObservableCollection = new ObservableCollection(){…} 有没有更好的方法来处理这个? 编辑: public ObservableCollection Columns { get { return (ObservableCollection)GetValue(ColumnsProperty); } set { SetValue(ColumnsProperty, value); } } public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register(“Columns”, typeof(ObservableCollection), typeof(MyControl), new PropertyMetadata(new ObservableCollection(), OnChanged));

触发PropertyChanged时,WPF依赖项属性设置器不会触发,但源值不会更改

我在自定义文本框上有一个int依赖项属性,它包含一个支持值。 它绑定到一个int? DataContext上的属性。 如果我在DataContext中引发PropertyChanged事件,并且源属性的值未更改(保持为null),则不会触发依赖项属性的setter。 这是一个问题,因为我想在PropertyChanged上更新自定义文本框(清除文本),即使源属性保持不变。 但是,我没有找到任何符合我想要的绑定选项(有一个UpdateSourceTrigger属性,但我想在这里更新目标,而不是源代码)。 也许有更好的方法来通知Textbox它需要清除它的文本,我对任何建议持开放态度。 来源,按要求(简化) DataContext(来源): private int? _foo; public int? Foo { get { // The binding is working, because _foo is retrieved (hits a breakpoint here). // RaisePropertyChanged(“Foo”) is called from elsewhere, even if _foo’s value is not changed return _foo; } set { // Breakpoint is hit on user […]

为什么我的数据绑定会看到真实值而不是强制值?

我正在编写一个真正的NumericUpDown/Spinner控件作为练习来学习自定义控件创作。 我有大部分的行为,我正在寻找,包括适当的强制。 然而,我的一项测试揭示了一个缺陷。 我的控件有3个依赖项属性: Value , MaximumValue和MaximumValue 。 我使用强制来确保Value保持在最小值和最大值之间,包括在内。 例如: // In NumericUpDown.cs public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(“Value”, typeof(int), typeof(NumericUpDown), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, HandleValueChanged, HandleCoerceValue)); [Localizability(LocalizationCategory.Text)] public int Value { get { return (int)this.GetValue(ValueProperty); } set { this.SetCurrentValue(ValueProperty, value); } } private static object HandleCoerceValue(DependencyObject d, object baseValue) { NumericUpDown o […]

AvalonEdit中的双向绑定不起作用

我在我的项目中使用了AvalonEdit,它基于WPF和MVVM。 看完这篇文章后,我创建了以下课程: public class MvvmTextEditor : TextEditor, INotifyPropertyChanged { public static DependencyProperty DocumentTextProperty = DependencyProperty.Register(“DocumentText”, typeof(string), typeof(MvvmTextEditor), new PropertyMetadata((obj, args) => { MvvmTextEditor target = (MvvmTextEditor)obj; target.DocumentText = (string)args.NewValue; }) ); public string DocumentText { get { return base.Text; } set { base.Text = value; } } protected override void OnTextChanged(EventArgs e) { RaisePropertyChanged(“DocumentText”); base.OnTextChanged(e); […]

WPF绑定到变量/ DependencyProperty

我正在玩WPF Binding和变量。 显然,只能绑定DependencyProperties。 我提出了以下内容,它完全正常:代码隐藏文件: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public string Test { get { return (string)this.GetValue(TestProperty); } set { this.SetValue(TestProperty, value); } //set { this.SetValue(TestProperty, “BBB”); } } public static readonly DependencyProperty TestProperty = DependencyProperty.Register( “Test”, typeof(string), typeof(MainWindow), new PropertyMetadata(“CCC”)); private void button1_Click(object sender, RoutedEventArgs e) { […]