【发布时间】:2014-10-17 13:43:31
【问题描述】:
在使用 MVVM 的 WPF 中,我想在单击鼠标时将视图模型中的属性设置为显示的文本。那就是我希望 ItemsControl 中的 PreviewMouseDown 事件在视图模型中设置一个属性。
在以下 XAML 中,我使用 ItemsControl 来显示来自 FormattedText ObservableCollection 的字符串。下面的 XAML 显示 FormattedText 一切顺利。
但是,如何将 PreviewMouseDown 绑定到视图模型的每个生成项?
我在 ItemsControl 中使用 DataTemplate 的所有尝试最终导致:
System.Windows.Data 错误:26:ItemTemplate 和 ItemTemplateSelector 已被 ItemsControl 的容器类型忽略;
XAML
<ItemsControl
ItemsSource="{Binding Strings}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas
Background="Transparent"
Width="{x:Static h:Constants.widthCanvas}"
Height="{x:Static h:Constants.heightCanvas}"
/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
添加
h:MouseBehaviour.PreviewMouseDownCommand="{Binding PreviewMouseDown}"
Canvas 定义永远不会导致命令被调用,我无法将它添加到 DataTemplate 中。
感谢任何帮助或更好的想法。
【问题讨论】:
标签: wpf xaml mvvm itemscontrol