我有代码:
<UserControl x:Class="MediaNet.View.MusicWindow.MusicWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:musicVM="clr-namespace:MediaNet.ViewModel.MusicWindowViewModel"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="557">
<UserControl.DataContext>
<musicVM:MusicWindowViewModel />
</UserControl.DataContext>
<UserControl.Resources>
<musicVM:TimeSpanConverter x:Key="TimeSpanConverter" />
<musicVM:CurrentSongIndexConverter x:Key="CurrentSongIndexConverter" />
</UserControl.Resources>
<DataGrid Grid.Row="1" AutoGenerateColumns="True" VerticalAlignment="Top" ItemsSource="{Binding Path=MusicItems}" SelectedIndex="{Binding Path=SelectedIndex}" >
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentSongIndex, Converter={StaticResource CurrentSongIndexConverter}, RelativeSource={RelativeSource Mode=Self}}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.ContextMenu>
<ContextMenu >
<MenuItem Command="Delete">
<MenuItem.Icon>
<Image />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Song options">
<MenuItem Header="Play to this song" Command="{Binding SetStopPositionCommand}" />
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
音乐项是
ObservableCollection<Song>
查看模型:
namespace MediaNet.ViewModel.MusicWindowViewModel
{
public class MusicWindowViewModel : INotifyPropertyChanged, IDisposable
{
#region CurrentSongIndex
private int _currentSongIndex;
public int CurrentSongIndex
{
get { return _currentSongIndex; }
set
{
_currentSongIndex = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("CurrentSongIndex"));
}
}
}
#endregion
}
}
转换器:
namespace MediaNet.ViewModel.MusicWindowViewModel
{
class CurrentSongIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int CurrentSongIndex = (int)value;
return CurrentSongIndex > 0;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
这应该将背景颜色设置为数据网格中的行,但现在可以使用。我怎么能告诉触发器它应该改变哪一行的背景?