locked
how to change Binding UpdateSourceTrigger default? RRS feed

  • Question

  • How do I enable all the TextBox elements of a user control to bind the Text property when the property changes?

    I do not want to have to code UpdateSourceTrigger=PropertyChanged on each textbox.

    <TextBox Text="{Binding CustName,UpdateSourceTrigger=PropertyChanged}" />

    thanks,

    Tuesday, May 17, 2016 12:37 PM

Answers

  • You can't set properties on a binding via a style.

    The two ways I can think of doing this I can think of are:

    Use an attached property or a markup extension to provide the binding.

    That way you can build the binding in code.

    eg

    https://blogs.msdn.microsoft.com/alexdan/2008/10/30/binding-to-a-markupextension-that-returns-a-binding/

    Here's the code from there:

        [MarkupExtensionReturnType(typeof(BindingExpression))]
       public class PhysicalLengthMarkupExtension : MarkupExtension
        {
           public override object ProvideValue(IServiceProvider serviceProvider)
            {
               Binding physicalLengthBinding = new Binding();
               physicalLengthBinding.Source = _dpiProvider;
               physicalLengthBinding.Path = new PropertyPath("DPI");
               physicalLengthBinding.Converter = _dpiConverter;
               physicalLengthBinding.ConverterParameter = _length;
                return physicalLengthBinding.ProvideValue(serviceProvider);
            }
        }

    You would, add a public string variable/property for the name of the property you want to bind.

    That would then be used in the Path ( instead of DPI ).

    And of course you don't need a converter but you need to set UpdateSourceTrigger to propertychanged.

    If you wanted more sophistication then this approach is worth taking a look at:

    http://www.hardcodet.net/2008/04/wpf-custom-binding-class


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, May 17, 2016 4:47 PM
  • >>how to change Binding UpdateSourceTrigger default?

    Short answer: You cannot change the default value of the UpdateSourceTrigger property of a binding to the built-in TextBox's Text property.

    Also, you cannot really change the UpdateSourceProperty trigger of an existing binding that you have defined in your XAML markup without cloning it (creating a new Binding object). Please refer to the following thread at Stackoverflow for more information about this: http://stackoverflow.com/questions/7990137/how-to-set-and-get-updatesourcetrigger-of-a-textbox-in-codebehind

    So you will have to specify the UpdateSourceTrigger value explicitly in all of your XAML bindings.

    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Steve Richter Tuesday, May 17, 2016 5:27 PM
    Tuesday, May 17, 2016 5:09 PM

All replies

  • You can't set properties on a binding via a style.

    The two ways I can think of doing this I can think of are:

    Use an attached property or a markup extension to provide the binding.

    That way you can build the binding in code.

    eg

    https://blogs.msdn.microsoft.com/alexdan/2008/10/30/binding-to-a-markupextension-that-returns-a-binding/

    Here's the code from there:

        [MarkupExtensionReturnType(typeof(BindingExpression))]
       public class PhysicalLengthMarkupExtension : MarkupExtension
        {
           public override object ProvideValue(IServiceProvider serviceProvider)
            {
               Binding physicalLengthBinding = new Binding();
               physicalLengthBinding.Source = _dpiProvider;
               physicalLengthBinding.Path = new PropertyPath("DPI");
               physicalLengthBinding.Converter = _dpiConverter;
               physicalLengthBinding.ConverterParameter = _length;
                return physicalLengthBinding.ProvideValue(serviceProvider);
            }
        }

    You would, add a public string variable/property for the name of the property you want to bind.

    That would then be used in the Path ( instead of DPI ).

    And of course you don't need a converter but you need to set UpdateSourceTrigger to propertychanged.

    If you wanted more sophistication then this approach is worth taking a look at:

    http://www.hardcodet.net/2008/04/wpf-custom-binding-class


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, May 17, 2016 4:47 PM
  • >>how to change Binding UpdateSourceTrigger default?

    Short answer: You cannot change the default value of the UpdateSourceTrigger property of a binding to the built-in TextBox's Text property.

    Also, you cannot really change the UpdateSourceProperty trigger of an existing binding that you have defined in your XAML markup without cloning it (creating a new Binding object). Please refer to the following thread at Stackoverflow for more information about this: http://stackoverflow.com/questions/7990137/how-to-set-and-get-updatesourcetrigger-of-a-textbox-in-codebehind

    So you will have to specify the UpdateSourceTrigger value explicitly in all of your XAML bindings.

    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Steve Richter Tuesday, May 17, 2016 5:27 PM
    Tuesday, May 17, 2016 5:09 PM
  • So you will have to specify the UpdateSourceTrigger value explicitly in all of your XAML bindings.

    That is incorrect.

    You have to do something other than define bindings in xaml but I've given two ways of doing this that do not involve specifying the UpdateSourceTrigger value explicitly every time.

    Neither are particularly simple approaches and you might think setting the property on every binding is more attractive than creating bindings in code. But that's a different matter.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Wednesday, May 18, 2016 8:13 AM