0

I am trying to make use of interactivity and interactions libraries in wpf app. I need it to work on a grid, and it works well, but now I need this stuff in multiple grids and I cant find a way of reusing it. Here is the xaml

<Grid>
<i:Interaction.Behaviors>
    <ei:DataStateBehavior Binding="{Binding KeepAlive}"
                          FalseState="InactiveState"
                          TrueState="ActiveState"
                          Value="false" />
  </i:Interaction.Behaviors>
  <VisualStateManager.VisualStateGroups>
    <VisualStateGroup>
        <VisualState x:Name="ActiveState" />
        <VisualState x:Name="InactiveState">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames 
                 Storyboard.TargetName="ActiveContainer"
                 Storyboard.TargetProperty="(Control.IsEnabled)">
                    <DiscreteObjectKeyFrame KeyTime="0:0:0">
                        <DiscreteObjectKeyFrame.Value>
                            <system:Boolean>False</system:Boolean>
                        </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames 
                 Storyboard.TargetName="InactiveContainer" 
                 Storyboard.TargetProperty="(UIElement.Visibility)">
                    <DiscreteObjectKeyFrame KeyTime="0">
                        <DiscreteObjectKeyFrame.Value>
                            <Visibility>Visible</Visibility>
                        </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
  </VisualStateManager.VisualStateGroups>

<Border>
  <Grid>
   Content comes here, Texboxes, labels when active or inactive etc.
  </Grid>       
</Border>
</Grid>

It works great, but how can I refactor the above code so I can reuse the exact same behaviour on multiple grids?

Thank you

adminSoftDK
  • 2,012
  • 1
  • 21
  • 41

2 Answers2

0

A few approach immediately come up: style or resource. But Blend behavior cannot work when declared in resources. You can use attached property. See: How to add a Blend Behavior in a Style Setter

Community
  • 1
  • 1
dytori
  • 487
  • 4
  • 12
0

I created generic behaviour, from which you could simply inherit and add to your style

see my answer below

https://stackoverflow.com/a/31292989/4711853

Community
  • 1
  • 1
Roma Borodov
  • 596
  • 4
  • 10