我正在尝试对按钮进行简单的绑定,该按钮我以前使用过并且运行良好。但是现在在新的解决方案中,它不会触发绑定命令。 这是XAML:
<Window x:Class="MusicPlayer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MusicPlayer"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Grid.Row="1" Grid.Column="1"/>
<ListView Grid.Row="1" Grid.Column="2" DataContext="{Binding Musics}"
SelectedItem="{Binding CurrentMusic}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
<Button Content="Next" Grid.Row="3" Grid.Column="3" Command="{Binding
nextCommand}"/>
<Button Content="Previous" Grid.Row="3" Command="{Binding
previousCommand}"/>
<Button Content="Play" Grid.Row="3" Grid.Column="1" Command="{Binding
playCommand}"/>
<Button Content="Open" Grid.Row="0" Grid.Column="0" Command="{Binding
openCommand}"/>
</Grid>
</Window>
这是视图模型:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MusicPlayer
{
public class MusicPlayerViewModel : INotifyPropertyChanged
{
private Music _currentMusic;
public DelegateCommand nextCommand;
public DelegateCommand previousCommand;
public DelegateCommand playCommand;
public DelegateCommand openCommand;
public Music CurrentMusic
{
get { return _currentMusic; }
set
{
if(_currentMusic != value)
{
_currentMusic = value;
OnPropertyChanged("CurrentMusic");
}
}
}
public ObservableCollection<Music> Musics = new ObservableCollection<Music>();
public MusicPlayerViewModel()
{
CurrentMusic = new Music { Name = "X", PathString = "Y" };
Musics.Add(CurrentMusic);
nextCommand = new DelegateCommand(param => DoNext());
previousCommand = new DelegateCommand(param => DoPrevious());
playCommand = new DelegateCommand(param => DoPlay());
openCommand = new DelegateCommand(param => DoOpen());
}
public void DoNext()
{
CurrentMusic = new Music();
OnPropertyChanged("DoNext");
}
public void DoPrevious()
{
OnPropertyChanged("DoPrevious");
}
public void DoPlay()
{
OnPropertyChanged("DoPlay");
}
public void DoOpen()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Mp3 files (*.mp3)|*.mp3|All files (*.*)|*.*";
Nullable<bool> result = dialog.ShowDialog();
if (result == true)
{
string filename = dialog.FileName;
CurrentMusic.Name = filename;
}
OnPropertyChanged("DoOpen");
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
由于某些原因,只有文本块绑定有效,而其他都不起作用,这很奇怪,因为我在以前的解决方案中一直使用此模板,这很好。我在app.xaml.cs中将窗口的数据上下文设置为MusicPlayerViewModel,所以这应该不是问题。我将不胜感激。