Please take a look at the attached example of hierarchical datasoure bound to xamDiagram.
I want to change the background of nodes based on employee type enum value, say if its admin employee background to take a colour and if academic staff then node should take different colour.
I tried using datatriggers but could not achieve it. Please help.
Hello Abs,
Thank you for your post.
Unfortunately, I was unable to see the sample project that you had attached, as it appears that the file may have corrupted, and so I am unable to extract or view the contents of your attached .zip file.
I do have a couple of recommendations on this matter, though. While I can't really see what your DataTrigger looks like, I would recommend that you write a Style for DiagramNode in this case. Binding your DataTrigger to the data context of that node will not help on this matter, though, as the data context of the nodes in the XamDiagram share the same data context as the diagram itself. Instead, each of these nodes have a Content property which should represent the underlying item of that particular node. So, if you were to bind to the following in your DataTrigger, you should be able to check the enum value and set the background property of your nodes:
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content.EnumPropertyName}" Value="someValue" >
I hope this helps. Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
My style is inline with your suggestion but it does not work.
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Employee.EmployeType}" Value="Admin">
<Style TargetType="{x:Type ig:DiagramNode}" x:Key="EmpNode" BasedOn="{StaticResource {x:Type ig:DiagramNode}}"> <Setter Property="Fill" Value="CadetBlue">Setter> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Employee.EmployeType}" Value="Admin"> <Setter Property="Fill" Value="Crimson">Setter> DataTrigger> Style.Triggers> Style>
I have re attached the zip file, please can you retry?
Thank you for reattaching your sample. I was able to download and view it this time.
The binding in your DataTrigger is still incorrect. Currently, your binding is as follows:
Binding="{Binding RelativeSource={RelativeSource Self}, Path=EmployeeType}"
Breaking this down, this will look at the DiagramNode object for a property named EmployeeType. That property does not exist, and so your DataTrigger will not work. In the case of your sample, there actually doesn't exist any property named EmployeeType. The property on your underlying data item is EmpType. EmployeeType appears to be the type of the enum that is used for this property.
Changing your binding to the following:
Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content.EmpType}
This works, as the Content property exists on the DiagramNode and will return your Employee object. The EmpType property then exists on the Employee object, which you can check.
I have attached a modified version of the sample project that you sent to demonstrate. I hope this helps you.
Please let me know if you have any other questions or concerns on this matter.
This works great, just a question for best practice; is it better to write multiple data triggers for each value of enum or better to write one converter and handle permutations of enum in code?
I am glad that the modified sample project works well for you on this matter.
Regarding the converter versus triggers argument, it depends on whether or not you have any sort of complex business logic for certain enum values. If the color is going to be based on the enum value alone, I would say that you should go for the DataTriggers, as they will be marginally better performance than a converter would be. If, for example, you are going to base off of multiple enum values with certain conditions for each, I would recommend the converter.