Custom WPF Binding

前端 未结 3 804
离开以前
离开以前 2021-01-21 02:55

I have a custom MarkupExtension that simulates binding. It works well in normal assignments but when used in Style Setters, for example:



        
相关标签:
3条回答
  • 2021-01-21 03:23

    From the documentation, it looks like the object must be freezable (so they can be shared between various interested parties)

    http://msdn.microsoft.com/en-us/library/system.windows.setter.value.aspx

    "Data binding and dynamic resources within the object is supported if the specified value is a Freezable object. See Binding Markup Extension and DynamicResource Markup Extension."

    0 讨论(0)
  • 2021-01-21 03:25

    why don't you

    return Value
    

    inside the ProvideValue??

    else

    You can bind to only DependencyProperty. make a dependency property for Value in your MyExtension Class!

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(Object), typeof(MyContentControl), new UIPropertyMetadata());
    
    0 讨论(0)
  • 2021-01-21 03:27

    Kind of guessing, but it's likely because the XAML compiler has special built-in support for the Binding class, allowing its usage in this scenario (and others). The Binding class is also a MarkupExtension, but unfortunately it seals its implementation of ProvideValue().

    That said, you might just get away with this:

    public class MyBinding : Binding
    {
        private object value;
    
        public object Value
        {
            get { return this.value; }
            set
            {
                this.value = value;
                this.Source = value;
            }
        }
    }
    

    Since ProvideValue will return the Binding instance anyway.

    0 讨论(0)
提交回复
热议问题