我有代码:风格数据触发到色彩行上数据网格
<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>
MusicItem是
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();
}
}
}
这应该设置数据网格中的行的背景颜色,但现在工作。 唧可以告诉触发器应该改变背景哪一行?
转换不了火。为什么? – netmajor 2012-07-26 21:12:50
是否在'DataGrid'中的每个项目上定义了'CurrentSongIndex'属性? – sellmeadog 2012-07-26 21:27:08
你是什么意思? CurrentSongIndex是播放歌曲后视图模型中的属性。该属性应该是绑定到datagrid项目的一部分吗? – netmajor 2012-07-26 21:30:20