Materialdesigninxamltoolkit: How to change packicon on run time?

Created on 25 Sep 2018  路  3Comments  路  Source: MaterialDesignInXAML/MaterialDesignInXamlToolkit

hi, guys, i am a newbie C# programmer.
now we know Packicon can be added from XAML:

<Button Style="{StaticResource MaterialDesignFlatButton}" Width="30" Height="25" Margin="0,0,0,0" ToolTip="{DynamicResource LISTENCONTENT}" Tag="LISTENCONTENT" Click="Button_Click"> <materialDesign:PackIcon Kind="CommentOutline" Margin="-10,0,-5,0"/> </Button>

but how to change using c# code?
Button_click(){
// change the icon here
button.content = FindResource("") ; // what is the name for packicon ???
}

question

Most helpful comment

If you want to do this in code behind you could add a name to your PackIcon control:

<materialDesign:PackIcon Name="MyPackIcon" Kind="CommentOutline" Margin="-10,0,-5,0"/>

And change the Kind property of the control to something else:

Button_click(){
// change the icon here
MyPackIcon.Kind = PackIconKind.Settings;
}

All 3 comments

If you want to do this in code behind you could add a name to your PackIcon control:

<materialDesign:PackIcon Name="MyPackIcon" Kind="CommentOutline" Margin="-10,0,-5,0"/>

And change the Kind property of the control to something else:

Button_click(){
// change the icon here
MyPackIcon.Kind = PackIconKind.Settings;
}

That's very kind of you. thank you.

陌f you are using button inside collection viewers (List, Datagrid, etc) you may need to get icon from sender. So you can use findName

<Button Click="BtnClick" >
       <materialDesign:PackIcon Name="icon" Kind="SearchHandsFree" />
</Button>
  public void BtnClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            (button.FindName("icon") as PackIcon).Kind = PackIconKind.Tick;
        }
Was this page helpful?
0 / 5 - 0 ratings