Allow only OneWayToSource binding mode

前端 未结 1 460
闹比i
闹比i 2021-01-19 11:41

I have EntitiesUserControl responsible for EntitiesCount dependency property:

public static readonly DependencyProperty EntitiesC         


        
相关标签:
1条回答
  • 2021-01-19 12:10

    It's a „bit” hacky, but you can create a Binding-derived class and use that instead of Binding:

    [MarkupExtensionReturnType(typeof(OneWayToSourceBinding))]
    public class OneWayToSourceBinding : Binding
    {
        public OneWayToSourceBinding()
        {
            Mode = BindingMode.OneWayToSource;
        }
    
        public OneWayToSourceBinding(string path) : base(path)
        {
            Mode = BindingMode.OneWayToSource;
        }
    
        public new BindingMode Mode
        {
            get { return BindingMode.OneWayToSource; }
            set
            {
                if (value == BindingMode.OneWayToSource)
                {
                    base.Mode = value;
                }
            }
        }
    }
    

    In XAML:

    <controls:EntitiesUserControl EntitiesCount="{local:OneWayToSourceBinding CountOfEntities}" />
    

    The namespace mapping local might be something else for you.

    This OneWayToSourceBinding sets the Mode to OneWayToSource and prevents setting it to anything else.

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