Question: How to bind AnimatedIcon to NavigationViewItem.Icon
See original GitHub issueI’m trying to use AnimatedIcon
in NavigationViewItem
, it works properly when using it in MenuItems
...
<winui:NavigationViewItem Content="Search">
<winui:NavigationViewItem.Icon>
<winui:AnimatedIcon>
<winui:AnimatedIcon.Source>
<animatedVisuals:AnimatedFindVisualSource />
</winui:AnimatedIcon.Source>
<winui:AnimatedIcon.FallbackIconSource>
<winui:SymbolIconSource Symbol="Find" />
</winui:AnimatedIcon.FallbackIconSource>
</winui:AnimatedIcon>
</winui:NavigationViewItem.Icon>
</winui:NavigationViewItem>
...
But when I’m using Binding in MenuItemTemplate
, it goes wrong
<models:NavigationMenuItemModel
Content="Search">
<models:NavigationMenuItemModel.Icon>
<winui:AnimatedIcon>
<winui:AnimatedIcon.Source>
<animatedVisuals:AnimatedFindVisualSource />
</winui:AnimatedIcon.Source>
<winui:AnimatedIcon.FallbackIconSource>
<winui:FontIconSource FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="" />
</winui:AnimatedIcon.FallbackIconSource>
</winui:AnimatedIcon>
</models:NavigationMenuItemModel.Icon>
</models:NavigationMenuItemModel>
...
<winui:NavigationView.MenuItemTemplate>
<DataTemplate x:DataType="models:NavigationMenuItemModel">
<winui:NavigationViewItem
Height="40"
Content="{x:Bind Content}"
Icon="{x:Bind Icon}"
MenuItemsSource="{x:Bind Children}" />
</DataTemplate>
</winui:NavigationView.MenuItemTemplate>
...
Model:
using System;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
public class NavigationMenuItemModel : DependencyObject
{
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(
"Content", typeof(string), typeof(NavigationMenuItemModel), new PropertyMetadata(default(string)));
public string Content
{
get => (string)GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
"Icon", typeof(IconElement), typeof(NavigationMenuItemModel), new PropertyMetadata(default(IconElement)));
public IconElement Icon
{
get => (IconElement)GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
}
I get an error:
System.ArgumentException: Parameter error.
Parameter error.
at Microsoft.UI.Xaml.Controls.NavigationViewItem.put_Icon(IconElement value)
at HyPlayer.UWP.Pages.BasePage.XamlBindingSetters.Set_Microsoft_UI_Xaml_Controls_NavigationViewItem_Icon(NavigationViewItem obj, IconElement value, String targetNullValue)
at HyPlayer.UWP.Pages.BasePage.BasePage_obj6_Bindings.Update_Icon(IconElement obj, Int32 phase)
at HyPlayer.UWP.Pages.BasePage.BasePage_obj6_Bindings.Update_(NavigationMenuItemModel obj, Int32 phase)
If the Icon is a FontIcon, it goes well. I’m wondering why it will throw an error and how to fix it?
**Additional Information: ** Dependencies: Microsoft.UI.Xaml : 2.7.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
AnimatedIcon Class (Microsoft.UI.Xaml.Controls)
Represents an icon that displays and controls a visual that can animate in response to user interaction and visual state changes.
Read more >Cannot bind Icon property in NavigationView ...
What I need is to BIND the Icon property of the implicitly generated NavigationViewItems . The question is how? I've tried with this...
Read more >BottomNavigationView: Animating Icons
In this article we'll look at some tricks for getting them to play nicely. There are two distinct types of animated icons that...
Read more >AnimatedIcon class - material library - Dart API
Shows an animated icon at a given animation progress. The available icons are specified in AnimatedIcons. This example shows how to create an...
Read more >Untitled
... Question: How to bind AnimatedIcon to NavigationViewItem.Icon https://docs.telerik.com/devtools/wpf/controls/radnavigationview/getting-started ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ojhad @StephenLPeters - unrelated to XMP. This is the
IconElement
multiparent issue, where a singleIconElement
can’t be set as the value of multipleIcon
properties sinceIconElement
is aFrameworkElement
and it would exist in multiple places in the visual tree. The symptom is setting a breakpoint onWindows.UI.Xaml!OnFailureEncountered
, and seeing in the callstackCDependencyObject::SetEffectiveValue()
which is failing when callingVerifyCanAssociate()
.The developer code using
IconElement
could be refactored to use theIconElement
parameters in the data model instead of anIconElement
itself, and then use function binding to create new instances ofIconElement
using those parameters, e.g. instead of:The xaml would be:
With
IconParameters
being a class/struct replacingIcon
on the data model, representing the parameters to construct theIconElement
with in a newCreateIcon()
method.Thanks, it works!