Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
150
Setting the Mask on a XamNumericEditor with respect to the parent binding context
posted

Hi guys, I've read the examples on the forum that show how to define a style (specifically targeted to a XamNumericEditor), that sets the background color based on its "Value" that gets set from a property on an item in an XamDataGrid.

So, if I have a list of items that a grid is bound to, and each of those items has a non-browsable "Precision" property (that won't be displayed by the grid) then my question is this... 

How would I set the Mask property on an XamNumericEditor to alter the precision (per cell), NOT based on the Value in the editor, but on a Precision property belonging to the items bound into the grid? e.g a string that I would use to map to a specific precision.

I tried doing something like this, where the intent was to access the parents binding context in the hope that I could access the current items Precision property and then pass that to a PrecisionConverter that would return the correct mask...

<Style TargetType="{x:Type igEditors:XamNumericEditor}">

<Setter Property="Background" Value="{Binding Path=Value, RelativeSource={RelativeSource Self}, Converter={StaticResource itemColorBackgroundConverter} }" />

 

 

 

 

 

<Setter Property="Mask" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=igDP:XamDataGrid}, Path=/Precision, Converter={StaticResource itemPrecisionConverter}}" />

 

 

 

 

</Style>

 

Thanks...

  • 150
    Verified Answer
    posted

    I found that using WPF Snoop helped hugely.   I've been able to view the visual tree of my application and the settings of the properties and values on controls like XamNumericEditor, and found that all you need to do is use the DataContext property.

    Define a style targeted at the XamNumericEditor type and add triggers

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    <Style TargetType="{x:Type igEditors:XamNumericEditor}">

    <Style.Triggers>

     

     

     

     

    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.DataItem.SomePropertyOnTheBoundItem.SomeOtherPropertyOfThatETC}" Value="False">

     

     

     

     

    <Setter Property="Background" Value="AliceBlue" />

     

     

     

     

    </DataTrigger>

     

     

     

     

    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.DataItem.SomePropertyOnTheBoundItem.SomeOtherPropertyOfThatETC}" Value="True">

     

     

     

     

     

     

     

    <Setter Property="Background" Value="LemonChiffon" />

    </DataTrigger>

     

     

     

     

    </Style.Triggers>

     

     

     

     

    </Style>

    The important part of this is using the DataContext.DataItem in your binding, so you're not just tied to using triggers, you can apply this technique using converters too.