【发布时间】:2017-01-31 18:25:50
【问题描述】:
我在我的应用程序中使用 hardcodet 的 WPF NotifyIcon,它是使用 MVVM Light 实现的。我正在尝试为图标创建自定义 TrayPopup。在这个弹出窗口中,我有一个 ItemsControl,它的 ItemsSource 是我的 ViewModel 中的对象“NumberList”列表。对于我的 DataTemplate,我使用了一个网格,它显示了我的源对象的一个名为“Id”的属性和一个按钮。我试图在我的 ViewModel 中绑定到 RelayCommand 的按钮。过去,我通过使用 RelativeSource 绑定来获取 Window Ancestor 的 DataContext,对 ItemsControls 做过类似的事情。这次我尝试了类似的方法,但是当我运行应用程序时,打开 TrayPopup,然后单击按钮没有任何反应。我的命令没有在我的 ViewModel 中触发。
作为一项测试,我尝试将另一个按钮添加到我的 TrayPopup 并将其命令绑定到我的 ViewModel 中的同一个按钮。该绑定工作正常,所以据我所知,这不是命令。
就像我说的,我在过去几乎完全使用过这种技术,只是以前从未在 NotifyIcon 中使用过。我尝试在按钮绑定到我的窗口名称中指定 ElementName,但没有成功。我也尝试将 RelativeSource 更改为父 TaskbarIcon,但也没有成功。
这里是 NotifyIcon 有同样问题的示例实现:
<tb:TaskbarIcon PopupActivation="LeftOrRightClick">
<tb:TaskbarIcon.TrayPopup>
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="0">
<ItemsControl ItemsSource="{Binding NumberList, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding Id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="1" Content="Test" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.TestCommand}"/> <!--This binding doesn't work!-->
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
<Button Grid.Row="1" Content="Test2" Command="{Binding TestCommand}"/> <!--This works-->
</Grid>
</tb:TaskbarIcon.TrayPopup>
</tb:TaskbarIcon>
我相当有信心 ViewModel 中的所有内容都是正确的,因为我一直在使用我不久前编写的这个应用程序的早期版本。到目前为止,除了这个 NotifyIcon 之外,我还没有遇到任何问题。有什么想法吗?
【问题讨论】:
标签: c# wpf binding itemssource notifyicon