XamGrid CellControl Behaviour

Answered (Verified) This post has 1 verified answer | 5 Replies | 2 Followers Thread's RSS feed.

Andrey
Points 195
Replied On: Tue, Aug 15 2017 8:54 AM Reply

Hi Guys,

I would like to set CellControl Behaviour to handle enter edit mode. I should set Behaviour from styles because I use the same column in many places. But it can create only one instance of behavior and I get the next error:http://prntscr.com/g8qlc9

Could you help me with that? Could you provide a sample project where Behaviour set from properties for CellControl type?

<utils:DataGridNameCellBehavior x:Key="XamGridNameCellBehavior"
x:Shared="False"
IsEditing="{Binding IsInEditMode, Mode=TwoWay}" />

<utilities:BehaviorCollection x:Key="XamGridNameCellBehaviors"
x:Shared="False">
<StaticResource ResourceKey="XamGridNameCellBehavior" />
</utilities:BehaviorCollection>

<Style x:Key="XamGridNameCellStyle"
TargetType="{x:Type ig:CellControl}">
<Setter Property="utilities:InteractionHelper.Behaviors"
Value="{StaticResource XamGridNameCellBehaviors}" />
</Style>

<ig:TemplateColumn Key="Name" x:Key="XamGridDetailGridNameColumn"
x:Shared="False"
CellStyle="{StaticResource XamGridNameCellStyle}"
IsReadOnly="True"
HeaderText="{x:Static properties:Resources.Name}">

  • Post Points: 20

Verified Answer

Michael DiFilippo
Points 25,485
Infragistics Employee
Answered (Verified) Replied On: Mon, Aug 21 2017 12:17 PM Reply
Verified by Andrey

Hello Andrey,

Thank you for following up. The issue regarding the exception thrown in OnBehaviorsChanged event is to be expected because adding a behavior which triggers the event again and so forth. I've revised the event to prevent the exception by adding a flag to cause the logic to not add another behavior. Please make sure you do not add the same instance of the behavior to more the CellControl.

eg. The following additions were in bold and I commented out the foreach loop.


        static int addBehaviorInstance;

        private static void OnBehaviorsChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {           
            if (e.NewValue is BehaviorCreatorCollection == false)
                return;

            var newBehaviorCollection = e.NewValue as BehaviorCreatorCollection;

            var behaviorCollection = Interaction.GetBehaviors(depObj);
            behaviorCollection.Clear();

            if (addBehaviorInstance > 0)
            {
                return;
            }
            else
            {
                addBehaviorInstance++;
                behaviorCollection.Add(newBehaviorCollection[0]);
            }          
                                   
            //foreach (var behavior in newBehaviorCollection)
            //{
            //    behaviorCollection.Add(behavior);
            //}
        }

Let me know if you have any questions.

Sincerely,

Michael Di Filippo
Associate Software Developer

  • Post Points: 20

All Replies

Michael DiFilippo
Points 25,485
Infragistics Employee
Replied On: Tue, Aug 15 2017 11:12 AM Reply

Hello Andrey,

Thank you for contacting Infragistics. Which platform are you using (eg. WPF or Silverlight). x:Shared would indicate you are using WPF, which should work outright. However you can work around this by embedding the behavior directly within your style rather than a static resource. The error you are receiving is to be excepted because a single instance o f your behavior is reused.

eg.


<Style x:Key="XamGridNameCellStyle"
TargetType="{x:Type ig:CellControl}">

<Setter Property="utilities:InteractionHelper.Behaviors" >

<Setter.Value >
<utils:DataGridNameCellBehavior  />
</Setter.Value>

</Setter>

</Style>

If you still require assistance please provide a sample that includes your InteractionHelper so I can investigate this further.

Let me know if you have any questions regarding this matter.

Sincerely,

Michael Di Filippo
Associate Software Developer

  • Post Points: 20
Andrey
Points 195
Replied On: Sat, Aug 19 2017 2:24 PM Reply

Hi, Sorry for the late.

Yes, I use it for WPF project.

I attached the project with my problem. The main problem that I couldn't apply style for a specific column.

  • Post Points: 20
Michael DiFilippo
Points 25,485
Infragistics Employee
Answered (Verified) Replied On: Mon, Aug 21 2017 12:17 PM Reply
Verified by Andrey

Hello Andrey,

Thank you for following up. The issue regarding the exception thrown in OnBehaviorsChanged event is to be expected because adding a behavior which triggers the event again and so forth. I've revised the event to prevent the exception by adding a flag to cause the logic to not add another behavior. Please make sure you do not add the same instance of the behavior to more the CellControl.

eg. The following additions were in bold and I commented out the foreach loop.


        static int addBehaviorInstance;

        private static void OnBehaviorsChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {           
            if (e.NewValue is BehaviorCreatorCollection == false)
                return;

            var newBehaviorCollection = e.NewValue as BehaviorCreatorCollection;

            var behaviorCollection = Interaction.GetBehaviors(depObj);
            behaviorCollection.Clear();

            if (addBehaviorInstance > 0)
            {
                return;
            }
            else
            {
                addBehaviorInstance++;
                behaviorCollection.Add(newBehaviorCollection[0]);
            }          
                                   
            //foreach (var behavior in newBehaviorCollection)
            //{
            //    behaviorCollection.Add(behavior);
            //}
        }

Let me know if you have any questions.

Sincerely,

Michael Di Filippo
Associate Software Developer

  • Post Points: 20
Andrey
Points 195
Replied On: Thu, Aug 24 2017 2:46 PM Reply

Thank, but it doesn't work as expected, but it looks like not the Infragistics grid issue. I will mark this post as done.

  • Post Points: 20
Michael DiFilippo
Points 25,485
Infragistics Employee
Replied On: Thu, Aug 24 2017 3:02 PM Reply

Hello Andrey,

Thank you for following up. I found a similar post related to your code on stackoverflow. It's possible that the built-in WPF DataGrid from Microsoft works as expected when attaching a behavior to the individual cells, and the XamGrid does not.

I would investigate if the XamDataGrid works in your favor. Let me know if you have any questions or need further clarification.

Sincerely,

Michael Di Filippo
Associate Software Developer

  • Post Points: 5
Page 1 of 1 (6 items) | RSS