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>

音乐项是

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

这应该将背景颜色设置为数据网格中的行,但现在可以使用。我怎么能告诉触发器它应该改变哪一行的背景?

4

1 回答 1

1

Style应用于DataGrid. BindinginDataTrigger应该是相对于每一DataContext行的。这确保了对于每一行,绑定都会被评估。

请澄清/验证以下内容:

  • 这究竟是如何“不起作用”的?是否没有突出显示行,所有行都突出显示?
  • 你的转换器好用吗?您是否已验证它是否true在预期正确评估触发器绑定时返回?

更新

查看您更新的代码示例,问题在于CurrentSongIndex不在DataContexteach 中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窗口。DataContextMusicWindowViewModelCurrentSongIndex

于 2012-07-26T20:57:32.547 回答