如何在textBlock上设置on click效果并打开一个新的WPF窗口?
嗨,我是WPF的新手,我正在努力学习它。 所以现在我想知道如何在ListBox中的文本块上创建onclick效果。 我想点击listBox中的任何项目并打开一个新窗口。 我一定做错了什么,但我不知道它是什么。 到目前为止,我有以下内容。
上面的代码在我的XAML文件中。 如果是这样,我还需要其他东西。 应该在哪里?
这是MVVM警察! ;)
Xaml:使用绑定到ICommand而不是System.Windows.Interactivity和forinstance galasoft mvvm light。 我没有测试下面的代码,我只是用notepad ++写的。现在我在这里看到一件事,你在datatemplate和listboxitem中做这个…你的TextBlock会在LI而不是VM上查找命令,所以你需要一个时髦的装订。 检查它是否有效,但是您希望您的click事件在vm的datacontext上执行,而不是在listbox项上执行,因此必须稍微更改绑定(vacation … =))列表框中的项目包含在ListBoxItems中datacontext设置LI应该呈现的内容,列表中的项目。
您可能想要更改frpm下面的KeyUp绑定
至:
确保将UserControl替换为您的控件/页面/ cust ctrl / window的名称。
... xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:command="http://www.galasoft.ch/mvvmlight" xmlns:local="clr-namespace:YOURNAMSPACE" ...
现在您将需要一个viewmodel,您将其设置为datacontext。 下面是一个简单基类的例子(扩展由galasoft提供的ViewModelBase以增加function是很好的。
VM基类(简化):
public class SomeBaseClass : INotifyPropertyChanged { // Other common functionality goes here.. public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator]// Commment out if your don't have R# protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } }
VM:
public class ViewModelListStuff : SomeBaseClass { private string name; public ICommand PreviewMouseLeftButtonDownCommand { get; set; } public ICommand KeyUpCommand { get; set; } public String Name { get { return name; } set { if (value == name) return; name = value; OnPropertyChanged(); } } // I would have exposed your cvsSomething here as a property instead, whatever it is. public ViewModelListStuff() { InitStuff(); } public void InitStuff() { PreviewMouseLeftButtonDownCommand = new RelayCommand(PreviewMouseLeftButtonDown); KeyUpCommandnCommand = new RelayCommand(KeyUp); } private void KeyUp(KeyEventArgs e) { // Do your stuff here... } private void PreviewMouseLeftButtonDown(MouseButtonEventArgs e) { // Do your stuff heere } }
希望能帮助到你! 在我们将由命令调用的方法中创建断点,并观察命令方法的输出和堆栈跟踪。
干杯
了Stian