darthmaule2
Topic Author
Posts: 94
Joined: 23 Oct 2014, 19:54

Specify the data context / class for UserControl.Resources?

15 Dec 2014, 12:46

I have a class SBToggleButton that derives from Button. I know it's working because I have the Visibility property bound and that works. But, when I try to compile this XAML, it is looking for TapDownStoryBoard_Completed in the UserControl and not my SBToggleButton class. Here is the error:

Parsing Storyboard.Completed (@188,6).
Unable to find function 'TapDownStoryboard_Completed' on event handler 'UserControl'

Is there a way to tell the parser to look at my custom class and not the code-behind for stuff in UserControl.Resources or do I just need to subclass UserControl and put that event in the code-behind?
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"  VerticalContentAlignment="Bottom">
    <UserControl.Resources>

      <SBToggleButton x:Name="sbToggleButtonModel"/>

      <Storyboard x:Key="TapDownStoryboard" Completed="TapDownStoryboard_Completed">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(TranslateTransform.Y)"       Storyboard.TargetName="ToggleButtonTransform">
          <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="36.5"/>
        </DoubleAnimationUsingKeyFrames>
      </Storyboard>

      <ControlTemplate x:Key="SBToggleButtonTemplate">
        <Grid Background="Transparent">
             <!-- ... -->
        </Grid>
        <ControlTemplate.Triggers>
          <EventTrigger RoutedEvent="TapDown">
            <EventTrigger.Actions>
              <BeginStoryboard x:Name="TapDown" Storyboard="{StaticResource TapDownStoryboard}"/>
            </EventTrigger.Actions>
          </EventTrigger>         
        </ControlTemplate.Triggers>
        </ControlTemplate>
    </UserControl.Resources>

    <Canvas Canvas.Bottom="0" Canvas.Right="0" Background="Transparent" Height="100" Width="145" Name="SoftButtonArea" />

      <SBToggleButton DataContext="{StaticResource sbToggleButtonModel}" x:Name="sbToggleButton" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Stretch"  Height="100" Focusable="False" FocusVisualStyle="{x:Null}"
        Template="{StaticResource SBToggleButtonTemplate}" Visibility="{Binding ToggleButtonVisibility}"/>

  </Canvas>
  </UserControl>
Note: I'm trying to port from WPF
 
darthmaule2
Topic Author
Posts: 94
Joined: 23 Oct 2014, 19:54

Re: Specify the data context / class for UserControl.Resourc

15 Dec 2014, 13:07

Well it looks like I did have it defined in the code-behind in the WPF version... so maybe my question doesn't really make sense ;)
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: Specify the data context / class for UserControl.Resourc

15 Dec 2014, 14:58

If you attach a handler to an event in the XAML, you need to specify a code-behind class (using the x:Class attribute), and then expose a function in that class with the same name and event signature:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"  VerticalContentAlignment="Bottom"
             x:Class="SBToggleButton">
    <UserControl.Resources>

      <SBToggleButton x:Name="sbToggleButtonModel"/>

      <Storyboard x:Key="TapDownStoryboard" Completed="TapDownStoryboard_Completed">
      ...
</UserControl>
class SBToggleButton: public UserControl
{
    ...
    void TapDownStoryboard_Completed(BaseComponent* sender, const TimelineEventArgs& e) { ... }

    NS_IMPLEMENT_INLINE_REFLECTION(SBToggleButton, UserControl)
    {
        NsMeta<TypeId>("SBToggleButton");
        NsFunc("TapDownStoryboard_Completed", &SBToggleButton::TapDownStoryboard_Completed);
    }
};
There is a sample of how to attach event handlers also in the documentation tutorial: Building an application.

Who is online

Users browsing this forum: No registered users and 0 guests