Animate a Scrollviewer without using scrollbar
Posted: 18 Aug 2020, 10:29
Hi,
I have a lisbox with a collection of items, I use two buttons to control the listbox's scolling so I hide the listbox default scroller. using a custom template style instancied in a XAML file like this :
The two buttons controls the ListBox's Scrollviewer scrolling using this kind of code:
This works well, but now I'd like to animate the items' scrolling to get a nice visual effect.
So I tried this kind of code :
This dosn't works at all (does nothing).
So I also tried using a storyboard like this:
Dos nothing, like the previous code.
I'm still a very beginer in Noesis / WPF and I'm surely missing something. Do you have any suggestion to make this work ?
Thanks a lot,
Frank
I have a lisbox with a collection of items, I use two buttons to control the listbox's scolling so I hide the listbox default scroller. using a custom template style instancied in a XAML file like this :
Code: Select all
<Canvas x:Name="PanelMakeupBottomLeft" Grid.Row="1" Grid.Column="4" Opacity="1.0" Background="#80FFFFFF" VerticalAlignment="Top" HorizontalAlignment="Right" Width="136" Height="540">
<Button x:Name="BtnScrollUpBottomLeft" Command="{Binding ScrollUp4LooksBottomLeft}" Canvas.Top="5" Canvas.Left="65" Width="24" Height="24" Foreground="{StaticResource BandeauColor}" BorderBrush="{StaticResource BandeauColor}" Background="{StaticResource BandeauColor}" Panel.ZIndex="1">
<Image Source="ArrowUp.png" Stretch="Fill" VerticalAlignment="Center" HorizontalAlignment="Center">
</Image>
</Button>
<ListBox x:Name="BottomLeftMakeupSelector"
Style="{StaticResource Car4LooksListStyle}"
ItemContainerStyle="{StaticResource Car4LooksItemStyle}"
ItemsSource="{Binding LooksBottomLeft}"
DisplayMemberPath="Name"
Height="540"
Width="136">
</ListBox>
<Button x:Name="BtnScrollDownBottomLeft" Command="{Binding ScrollDown4LooksBottomLeft}" Canvas.Left="65" Canvas.Bottom="5" Width="24" Height="24" Foreground="{StaticResource BandeauColor}" BorderBrush="{StaticResource BandeauColor}" Background="{StaticResource BandeauColor}" Panel.ZIndex="1">
<Image Source="ArrowDown.png" Stretch="Fill" VerticalAlignment="Center" HorizontalAlignment="Center" Panel.ZIndex="1">
</Image>
</Button>
</Canvas>
Code: Select all
void ViewModel::ScrollListBoxAnimated(const char* szComponentName, float fScrolValue)
{
Noesis::ListBox* pListBox = FindControl<Noesis::ListBox>(szComponentName);
if (pListBox != nullptr)
{
Noesis::Visual* pVisual = VisualTreeHelper::GetChild(pListBox, 0);
ScrollViewer* pScroll = static_cast<ScrollViewer*>(pVisual);
// Scroll
float fOffset = pScroll->GetVerticalOffset();
pScroll->ScrollToVerticalOffset(fOffset + fScrolValue);
}
}
So I tried this kind of code :
Code: Select all
void ViewModel::ScrollListBoxAnimated(const char* szComponentName, float fScrolValue)
{
Noesis::ListBox* pListBox = FindControl<Noesis::ListBox>(szComponentName);
if (pListBox != nullptr)
{
Noesis::Visual* pVisual = VisualTreeHelper::GetChild(pListBox, 0);
ScrollViewer* pScroll = static_cast<ScrollViewer*>(pVisual);
float fOffset = pScroll->GetVerticalOffset();
// Scroll animated
Ptr<PowerEase> _ease = *new PowerEase();
_ease->SetEasingMode(EasingMode_EaseOut);
_ease->SetPower(8.0f);
Ptr<DoubleAnimation> scrollanim = *new DoubleAnimation();
scrollanim->SetEasingFunction(_ease);
Nullable<float> fFrom(pScroll->GetVerticalOffset());
Nullable<float> fTo(pScroll->GetVerticalOffset() + fScrolValue);
scrollanim->SetFrom(fFrom);
scrollanim->SetTo(fTo);
scrollanim->SetDuration(*new Duration(*new Noesis::TimeSpan(2.0)));
pScroll->SetAnimation(pScroll->VerticalOffsetProperty, scrollanim);
}
}
So I also tried using a storyboard like this:
Code: Select all
void ViewModel::ScrollListBoxAnimated(const char* szComponentName, float fScrolValue)
{
Noesis::ListBox* pListBox = FindControl<Noesis::ListBox>(szComponentName);
if (pListBox != nullptr)
{
Noesis::Visual* pVisual = VisualTreeHelper::GetChild(pListBox, 0);
ScrollViewer* pScroll = static_cast<ScrollViewer*>(pVisual);
float fOffset = pScroll->GetVerticalOffset();
// Scroll animated
Ptr<PowerEase> _ease = *new PowerEase();
_ease->SetEasingMode(EasingMode_EaseOut);
_ease->SetPower(8.0f);
Ptr<DoubleAnimation> scrollanim = *new DoubleAnimation();
scrollanim->SetEasingFunction(_ease);
Nullable<float> fFrom(pScroll->GetVerticalOffset());
Nullable<float> fTo(pScroll->GetVerticalOffset() + fScrolValue);
scrollanim->SetFrom(fFrom);
scrollanim->SetTo(fTo);
scrollanim->SetDuration(*new Duration(*new Noesis::TimeSpan(2.0)));
// With sotryboard
Ptr<Storyboard> storyboard = *new Storyboard();
storyboard->GetChildren()->Add(scrollanim);
Storyboard::SetTargetName(scrollanim, pScroll->GetName());
Storyboard::SetTargetProperty(scrollanim, MakePtr<PropertyPath>(pScroll->VerticalOffsetProperty));
storyboard->Begin(pScroll);
}
}
I'm still a very beginer in Noesis / WPF and I'm surely missing something. Do you have any suggestion to make this work ?
Thanks a lot,
Frank