lepoco / wpfui

WPF UI provides the Fluent experience in your known and loved WPF framework. Intuitive design, themes, navigation and new immersive controls. All natively and effortlessly.
https://wpfui.lepo.co
MIT License
7.55k stars 733 forks source link

Binding support to NotifyIcon #287

Open Tinekk opened 2 years ago

Tinekk commented 2 years ago

I am creating an application that has dynamic tray icon (both icon and tooltip). So far I was able to do this using hardcodet. It would be cool to have such ability for wpfui. Also MenuItem commands are not working.

To Reproduce

        <ui:TitleBarHeader="{x:Static resx:Resources.AppTitle}">
            <ui:TitleBar.Tray>
                <ui:NotifyIcon 
                    FocusOnLeftClick="True"
                    MenuOnRightClick="True"
                    TooltipText="{Binding Test}"
                    Icon="{Binding TrayIcon}"
                >
                    <ui:NotifyIcon.Menu>
                        <ContextMenu>
                            <MenuItem Header="{Binding Test}" >
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding ShowApp}" PassEventArgsToCommand="True" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </MenuItem>
                            <Separator />
                           <MenuItem Command="{Binding ShowApp}" Header="{x:Static resx:Resources.OpenButton}" />
                        </ContextMenu>
                    </ui:NotifyIcon.Menu>
                </ui:NotifyIcon>
            </ui:TitleBar.Tray>
        </ui:TitleBar>

Expected behavior

Desktop:

Additional context Working Hardcodet taskabar icon

        <tb:TaskbarIcon
                    IconSource="{Binding TrayIcon}"
                    ToolTipText="{Binding TrayTooltip}"
                    LeftClickCommand="{Binding ShowApp}"
                    MenuActivation="RightClick" >
            <tb:TaskbarIcon.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="{Binding ShowApp}" Header="{x:Static resx:Resources.OpenButton}" />
                </ContextMenu>
            </tb:TaskbarIcon.ContextMenu>
        </tb:TaskbarIcon>
jasmeet21 commented 2 years ago

Not just the NotifyIcon, most of the controls with click handler in general. E.g ButtonRightClick & ButtonLeftClick in messagebox & dialog would be great to have an option to use those as Command using binding

jackBonadies commented 8 months ago

NotifyIcon does support binding. So does the context menu. Its just that the context menu isn't part of the same visual tree and therefore doesn't have the same DataContext. This goes for any context menu, not specific to wpfui. One can fix this by setting the DataContext of the context menu in the code behind (i.e. this.notifyIcon.Menu.DataContext = viewModel).