2012-07-26 53 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