2012-07-26 52 views
1

我有代码:风格数据触发到色彩行上数据网格

<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(); 
     } 
    } 
} 

这应该设置数据网格中的行的背景颜色,但现在工作。 唧可以告诉触发器应该改变背景哪一行?

回答

1

Style将应用于DataGrid中的每一行。 DataTrigger中的Binding应该相对于每行的DataContext。这确保了对于每一行,绑定将被评估。

请澄清/确认以下事项:

  • 正是请问这个 “不行”?没有行被突出显示,所有行都被突出显示?
  • 您的转换器是否工作?你是否已经验证它在返回true时预计能正确评估触发器绑定?

UPDATE

看你更新的代码示例,问题是,CurrentSongIndex不是在每个DataGridRowDataContext。根据你的XAML,你有ItemsSource="{Binding Path=MusicItems}"

当网格的每一行都是数据绑定时,DataGridRow.DataContext被设置为相应的Song。发生这种情况时,绑定不再可以访问CurrentSongIndex,因为它是MusicWindowViewModel的一部分。

试着改变你的数据触发绑定到这样的事情:

{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.CurrentSongIndex, Converter={StaticResource CurrentSongIndexConverter}} 

这将迫使结合来看看谁的DataContext窗口DataContext是包含CurrentSongIndex财产MusicWindowViewModel

+0

转换不了火。为什么? – netmajor 2012-07-26 21:12:50

+0

是否在'DataGrid'中的每个项目上定义了'CurrentSongIndex'属性? – sellmeadog 2012-07-26 21:27:08

+0

你是什么意思? CurrentSongIndex是播放歌曲后视图模型中的属性。该属性应该是绑定到datagrid项目的一部分吗? – netmajor 2012-07-26 21:30:20