locked
Setting binding property in Style RRS feed

  • Question

  • Hi,

    I am a new to XAML. I am trying to set global binding property UpdateSourceTrigger to my TextBoxes. I believe it can be done via styles, but i have been unsuccessful so far (See my code below). I appreciate if someone can provide me a solution to setting UpdateSourceTrigger=PropertyChanged in Style.

    Thank You

    <Style.Triggers>
       <DataTrigger Value="true">
         <DataTrigger.Binding>
            <Binding UpdateSourceTrigger="PropertyChanged" />
          </DataTrigger.Binding>
       </DataTrigger>
    </Style.Triggers>

    Wednesday, April 28, 2010 4:59 PM

Answers

  • I am not entirely sure that what you are asking is possible.  UpdateSourceTrigger is a property of the Binding class not of the TextBox class.  It is also not an attached property so you cannot set it on the parent TextBox and expect Binding to retrieve the value from the TextBox.

    Your code seems to be slightly incorrect as well.  A DataTrigger basically does something when the bound data (obtained from the binding) is equal to the value.  So in the case of the code that you provided above your DataTrigger will only activate when the UpdateSourceTrigger property of the TextBox is equal to true (this is because you didn't specify a path in the Binding so the Binding is just applied to the TextBox itself).  So the DataTrigger is never fired.  In general a DataTrigger wants to react to something that is reachable by a path from the element such as a property, a property of a property, an attached property (attached property paths use special syntax). 

    If you want to set specific bindings as having an UpdateSourceTrigger value of PropertyChanged then you can certainly define them in your style.  But to my knowledge you can not define a global value of UpdateSourceTrigger that will by default apply to all TextBoxes that use a Style.  If you want to set an individual Binding within the Style you can follow the simple example given below:

     

    <Style TargetType="{x:Type TextBox}">
          <Setter Property="FontFamily" Value="{x:Static SystemFonts.IconFontFamily}"></Setter>
    </Style>
    
    

     

    In general this won't be a very good practice because most of you Bindings will likely be between the TextBox and other Elements.  In any case that I can imagine it isn't a great idea to keep a bunch of Element names in a Style.

    • Proposed as answer by Console.WriteLine Wednesday, April 28, 2010 7:25 PM
    • Marked as answer by Jie Bao Tuesday, May 4, 2010 2:02 AM
    Wednesday, April 28, 2010 7:08 PM

All replies

  • I am not entirely sure that what you are asking is possible.  UpdateSourceTrigger is a property of the Binding class not of the TextBox class.  It is also not an attached property so you cannot set it on the parent TextBox and expect Binding to retrieve the value from the TextBox.

    Your code seems to be slightly incorrect as well.  A DataTrigger basically does something when the bound data (obtained from the binding) is equal to the value.  So in the case of the code that you provided above your DataTrigger will only activate when the UpdateSourceTrigger property of the TextBox is equal to true (this is because you didn't specify a path in the Binding so the Binding is just applied to the TextBox itself).  So the DataTrigger is never fired.  In general a DataTrigger wants to react to something that is reachable by a path from the element such as a property, a property of a property, an attached property (attached properties use special syntax). 

    If you want to set specific bindings as having an UpdateSourceTrigger value of PropertyChanged then you can certainly define them in your style.  But to my knowledge you can not define a global value of UpdateSourceTrigger that will by default apply to all TextBoxes that use a Style.  If you want to set an individual Binding within the Style you can follow the simple example given below:

     

    <Style TargetType="{x:Type TextBox}">
       <Setter Property="FontFamily" Value="{x:Static SystemFonts.IconFontFamily}"></Setter>
    </Style>

     

    In general this won't be a very good practice because most of you Bindings will likely be between the TextBox and other Elements.  In any case that I can imagine it isn't a great idea to keep a bunch of Element names in a Style.  You could try to come up with some custom classes that would allow you to use a global value of UpdateSourceTrigger, but it would be simpler and cleaner to just set UpdateSourceTrigger on each of your bindings.  People who read your code (or you if you come back to it after a couple of months) will have a much easier timing reading the code then they (or you) would have if there was a global value.  The local values make the exact functionality of each Binding immediately obvious.

    Wednesday, April 28, 2010 7:06 PM
  • I am not entirely sure that what you are asking is possible.  UpdateSourceTrigger is a property of the Binding class not of the TextBox class.  It is also not an attached property so you cannot set it on the parent TextBox and expect Binding to retrieve the value from the TextBox.

    Your code seems to be slightly incorrect as well.  A DataTrigger basically does something when the bound data (obtained from the binding) is equal to the value.  So in the case of the code that you provided above your DataTrigger will only activate when the UpdateSourceTrigger property of the TextBox is equal to true (this is because you didn't specify a path in the Binding so the Binding is just applied to the TextBox itself).  So the DataTrigger is never fired.  In general a DataTrigger wants to react to something that is reachable by a path from the element such as a property, a property of a property, an attached property (attached property paths use special syntax). 

    If you want to set specific bindings as having an UpdateSourceTrigger value of PropertyChanged then you can certainly define them in your style.  But to my knowledge you can not define a global value of UpdateSourceTrigger that will by default apply to all TextBoxes that use a Style.  If you want to set an individual Binding within the Style you can follow the simple example given below:

     

    <Style TargetType="{x:Type TextBox}">
          <Setter Property="FontFamily" Value="{x:Static SystemFonts.IconFontFamily}"></Setter>
    </Style>
    
    

     

    In general this won't be a very good practice because most of you Bindings will likely be between the TextBox and other Elements.  In any case that I can imagine it isn't a great idea to keep a bunch of Element names in a Style.

    • Proposed as answer by Console.WriteLine Wednesday, April 28, 2010 7:25 PM
    • Marked as answer by Jie Bao Tuesday, May 4, 2010 2:02 AM
    Wednesday, April 28, 2010 7:08 PM