样式数据触发数据网格上的颜色行

时间:2012-07-26 20:36:04

标签: wpf datagrid datatrigger

我有代码:

    <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 个答案:

答案 0 :(得分:1)

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

请澄清/验证以下内容:

  • 这究竟是什么“不起作用”?是否没有突出显示任何行,所有行都被突出显示?
  • 您的转换器是否有效?在预期正确评估触发器绑定时,您是否确认它正在返回true

<强>更新

查看更新后的代码示例,问题是每个CurrentSongIndex中的DataContext不在DataGridRow。根据您的XAML,您有ItemsSource="{Binding Path=MusicItems}"

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

尝试将数据触发器绑定更改为以下内容:

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

这会强制绑定查看DataContext窗口的DataContextMusicWindowViewModel是包含CurrentSongIndex属性的{{1}}。