我有代码:
<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();
}
}
}
这应该将背景颜色设置为数据网格中的行,但现在可以正常工作。 我可以告诉触发器它应该改变哪一行背景吗?
答案 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
窗口的DataContext
,MusicWindowViewModel
是包含CurrentSongIndex
属性的{{1}}。