-
- darthmaule2
- Posts: 95
- Joined:
Specify the data context / class for UserControl.Resources?
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?
Note: I'm trying to port from WPF
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?
Code: Select all
<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>
-
- darthmaule2
- Posts: 95
- Joined:
Re: Specify the data context / class for UserControl.Resourc
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 

-
-
sfernandez
Site Admin
- Posts: 2065
- Joined:
Re: Specify the data context / class for UserControl.Resourc
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:
There is a sample of how to attach event handlers also in the documentation tutorial: Building an application.
Code: Select all
<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>
Code: Select all
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);
}
};
Who is online
Users browsing this forum: Google [Bot] and 0 guests