How to use click event in MaterialDesign:PackIcon in WPF?

☆樱花仙子☆ 提交于 2020-07-22 21:37:00

问题


I used materialDesign:PackIkon in my WPF application.

this is my code at xaml for the PackIcon

<ListViewItem Background="White" Height="55" >
         <StackPanel Orientation="Horizontal">
             <materialDesign:PackIcon Kind="CardMembership" MouseDown="PackIconMember_MouseDown" Height="40" Width="25" Foreground="#FF0959A8" />
             <Button  x:Name="btnMember" Click="btnMember_Click" Content="Member" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" FontSize="10" />
           </StackPanel>
  </ListViewItem>

I have a few PackIcon in my xaml. each of them in different listview.

what I want is when user click on the Icon it will process the event in PackIconMember_MouseDown

There is no error in my code above, the problem is sometimes the code work. I means when user click on the icon it will process the event. but sometimes user need to click multiple time for it to process the event. I don't know why this happen.

Any idea on what I should do with this ? or any suggestion to replace the MouseDown event.

its only work when I click on icon with the blue color. it does not working when I click on the white space that I show with the arrow. how can I do to make it work when user click anywhere on the icon ? is it possible ?

if I do inside button, the packIcon does not appear

 <Button Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" Height="20" Width="25" Margin="10">
    <materialDesign:PackIcon MouseDown="PackIconMember_MouseDown" TouchDown="PackIconMember_MouseDown" Kind="CardMembership" />
 </Button>

回答1:


Set the Background property of the PackIcon to Transparent:

<materialDesign:PackIcon Kind="CardMembership" MouseDown="PackIconMember_MouseDown" Height="40" Width="25" Foreground="#FF0959A8"
                         Background="Transparent" />

This should capture the clicks also on the "empty" parts of the icon.




回答2:


Make The Icon as Part of the Button Like:

<Button  x:Name="btnMember" Click="btnMember_Click" Content="Member" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" FontSize="10" >
     <materialDesign:PackIcon Kind="CardMembership" Height="40" Width="25" Foreground="#FF0959A8" />
</Button

But you have to set the margin



来源:https://stackoverflow.com/questions/55806643/how-to-use-click-event-in-materialdesignpackicon-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!