none
UserControl/datagrid binding to dependancy property RRS feed

  • Question

  • Hi

     

    I have created a simple user control with a datagrid on that is bound to an observablecollection.

    I am attempting to validate the users entry into the grid/business object and then bind a save buttons' enabled property on the window hosting my control to the haserror property of my usercontrols datagrid.

    So far I have created a dependancy property on my usercontrol named DataHasErrors. This is the boolean value my save buttons' enabled property is to be bound to. My problem is in trying to set my usercontrols' dependancy property from my datagrid.datarow. I am trying this with the below but have come unstuck. Can anyone point me in the right direction?

      <UserControl.Template>
        <ControlTemplate TargetType="local:ucSupplierItems">
          <ControlTemplate.Triggers>
            <Trigger SourceName="grdSupplierItems" Property="Validation.HasError" Value="True">
              <Setter TargetName="ucSupplierItems" Property="ControlHasErrors" Value="True"></Setter>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </UserControl.Template> 
    

    Cheers
    Friday, July 16, 2010 10:23 AM

Answers

All replies

  • Have wandered through the web and got the following style hooked up to my datagrid:

      <Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
       <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
         <Setter Property="BorderThickness" Value="1"/>
         <Setter Property="BorderBrush" Value="Red"/>
         <Setter Property="ToolTip"
        Value="{Binding RelativeSource={RelativeSource Self},
          Path=(Validation.Errors)[0].ErrorContent}"/>
         <!--Setter Property="ControlHasErrors" Value="True"></Setter-->
        </Trigger>
       </Style.Triggers>
      </Style>

    Just need to know how I can reference my usercontrol in the commented out setter to set my dependancy proeprty to true when the HasError property is true?

     

    Any help appreciated.

     

    Thanks.


    Cheers
    Friday, July 16, 2010 1:40 PM
  • Okay, I've tried to set a datatrigger as below and still unable to set my dependancy proeprty from my datagridrow.validation.haserror property being true.

        <Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
          <Style.Triggers>
           
            <Trigger Property="Validation.HasError" Value="true">
              <Setter Property="BorderThickness" Value="1"/>
              <Setter Property="BorderBrush" Value="Red"/>
              <Setter Property="ToolTip"
           Value="{Binding RelativeSource={RelativeSource Self},
               Path=(Validation.Errors)[0].ErrorContent}"/>
              <!--Setter Value="True"></Setter-->
            </Trigger>
            <DataTrigger Binding="{Binding Path=Validation.HasError}" Value="True">
              <Setter TargetName="ucSupplierItems" Property="ControlHasErrors" Value="true"></Setter>
            </DataTrigger>
          </Style.Triggers>
        </Style>
    
    Get error informing me that no element with name ucSupplierItems was found but that is the name of my user control???????????
    Cheers
    Saturday, July 17, 2010 8:56 PM
  • Here is an example I wrote for someone else that might help.

    http://cid-90e62986b27f4a1a.office.live.com/self.aspx/.Public/TriggerExample.zip

    Sunday, July 18, 2010 12:11 AM
  • Cheers 8x. This is very nearly what I'm after I think but the other way round. I want to set my user controls' dependancy property if the currently selected datagridrow fails validation. Looking at the example you provide you are binding to the dependancy property to show/hide something on your UC, is this correct?

    Thanks

    Paul.


    Cheers
    Sunday, July 18, 2010 8:02 PM
  • Have amended my style to the following and now get no errors but my dependancy property is not being set upon the style being applied. The rows border is red and validation errors appear in the tooltip as style dictates but my dependancy prop set never fires. Am I missing something?

    <Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
          <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
              <Setter Property="BorderThickness" Value="1"/>
              <Setter Property="BorderBrush" Value="Red"/>
              <Setter Property="ToolTip"
           Value="{Binding RelativeSource={RelativeSource Self},
               Path=(Validation.Errors)[0].ErrorContent}"/>
              <Setter Property="local:ucSupplierItems.ControlHasErrors" Value="true"></Setter>
            </Trigger>
          </Style.Triggers>
        </Style>
    

    Cheers.


    Cheers
    Sunday, July 18, 2010 8:28 PM
  • I've tried everything I can think of now and still no joy. If anyone has any other ideas? I've attempted the following:

    1. Setting dependancy prop using a setter in my style

    <Setter Property="local:ucSupplierItems.ControlHasErrors" Value="true"></Setter>

    2. Setting dependancy prop using a datatrigger

    <DataTrigger Binding="{Binding Path=Validation.HasError}" Value="True">
              <Setter TargetName="ucSupplierItems" Property="ControlHasErrors" Value="true"></Setter>


    3.Setting the dependancy prop using an EventSetter

    <

     

     

    EventSetter Event="Validation.Error" Handler="EventTrigger_Error"></EventSetter>

    I'm really lost for possible solutions now. Really urgent as project is at a standstill. I can't release my changes with the save button enabled with errors in data!!!!!

     

    Any help appreciated.

    Paul.


    Cheers
    Monday, July 19, 2010 9:37 AM
  • Hi,

    I guess you must not set the property value "ControlHasError" if there is an error... I think It should be the reverse one.

    This means that your ucSupplierItems should listen if there are error occur.  Means that your userControl will listen to DatagridRow if has the error.

     

    Hope that helps.

    Regards,

    gioVhan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, July 19, 2010 10:21 AM
  • Hi,

    I guess you must not set the property value "ControlHasError" if there is an error... I think It should be the reverse one.

    This means that your ucSupplierItems should listen if there are error occur.  Means that your userControl will listen to DatagridRow if has the error.

     

    Hope that helps.

    Regards,

    gioVhan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Hi Gio

    Not quite sure I understand this mate, I was hoping to set my usercontrols ControlHasError peroperty to true upon a datagridrow firing the Validation.Errors event OR the datagridrow.Validation.HasErrors property being set to true. Unfortunately I've drawn a blank at each step. It looks like not only can I not use the Setter in style.triggers but also I'm failing to handle the Validation.Error event as my code behind, EventTrigger_Error, never gets hit????

    Paul


    Cheers
    Monday, July 19, 2010 10:55 AM
  • Surely I'm not the only developer who has come across this stumbling block? Anyone any ideas?
    Cheers
    Monday, July 19, 2010 5:09 PM
  • Hi,

    After reading I came to a solution why your items are not working.

    1.  Setter will be applied only to the current target type.

    2.  Setter with TargetName can be any element within the scope of where the setter collection (the collection that this setter is part of) is applied. This is typically a named element that is within the template that contains this setter.

    3. for the third one.. check these link.. 

    http://msdn.microsoft.com/en-us/library/system.windows.controls.validation.error.aspx

    http://msdn.microsoft.com/en-us/library/system.windows.data.binding.notifyonvalidationerror(v=VS.85).aspx

    http://msdn.microsoft.com/en-us/library/system.windows.controls.validation.error(VS.85).aspx

     This means will only trigger of NotifyOnValidationError is set to true.

     

    XAML doesnt have the ability to read the property of its child elements but can read to its parent (bubbling). using FindAncestor.

    My suggestion is i think you need to redesign how you gonna inform your UserControl whenever there is a datagridrow error.  Maybe a property on a class.. then your button will listen to that property.

     

    Hope this helps.

     

    Regards,

    gioVhan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, July 21, 2010 6:29 AM