Wpf 样式数据触发器为datagrid上的行添加颜色
我有密码:Wpf 样式数据触发器为datagrid上的行添加颜色,wpf,datagrid,datatrigger,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" xmln
<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>
转换器:
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();
}
}
}
这应该将背景色设置为数据网格中的行,但现在可以了。
我能告诉触发器应该将背景更改到哪一行吗?该样式将应用于数据网格中的每一行。DataTrigger中的绑定应该相对于每行的DataContext。这可以确保对每一行都计算绑定
请澄清/核实以下内容:
这到底是怎么回事?是否没有突出显示行,所有行都突出显示?
你的转换器工作吗?您是否验证了它在正确评估触发器绑定时返回true?
更新
查看更新后的代码示例,问题在于CurrentSongIndex不在每个DataGridRow的DataContext中。根据XAML,您有ItemsSource={Binding Path=MusicItems}
当网格的每一行都被数据绑定时,DataGridRow.DataContext被设置为相应的歌曲。当这种情况发生时,绑定不再具有对CurrentSongIndex的访问权限,因为它是MusicWindowViewModel的一部分
尝试将数据触发器绑定更改为以下内容:
{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.CurrentSongIndex, Converter={StaticResource CurrentSongIndexConverter}}
这将强制绑定查看窗口的DataContext,其DataContext是包含CurrentSongIndex属性的MusicWindowViewModel。该样式将应用于DataGrid中的每一行。DataTrigger中的绑定应该相对于每行的DataContext。这可以确保对每一行都计算绑定
请澄清/核实以下内容:
这到底是怎么回事?是否没有突出显示行,所有行都突出显示?
你的转换器工作吗?您是否验证了它在正确评估触发器绑定时返回true?
更新
查看更新后的代码示例,问题在于CurrentSongIndex不在每个DataGridRow的DataContext中。根据XAML,您有ItemsSource={Binding Path=MusicItems}
当网格的每一行都被数据绑定时,DataGridRow.DataContext被设置为相应的歌曲。当这种情况发生时,绑定不再具有对CurrentSongIndex的访问权限,因为它是MusicWindowViewModel的一部分
尝试将数据触发器绑定更改为以下内容:
{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.CurrentSongIndex, Converter={StaticResource CurrentSongIndexConverter}}
这将强制绑定查看窗口的DataContext谁的DataContext是MusicWindowViewModel,其中包含CurrentSongIndex属性。DataGrid中的每个项都定义了CurrentSongIndex属性吗?你是什么意思?CurrentSongIndex是视图模型中的属性,在播放歌曲后会发生更改。这个属性应该是绑定到datagrid项的类型的一部分吗?但是它是在哪里定义的呢?如果CurrentSongIndex不在每个DataGridRow的范围内,绑定将失败,这将解释为什么没有调用转换器。是否可以扩展代码示例以包含视图模型?是否在DataGrid中的每个项上定义了CurrentSongIndex属性?这是什么意思?CurrentSongIndex是视图模型中的属性,在播放歌曲后会发生更改。这个属性应该是绑定到datagrid项的类型的一部分吗?但是它是在哪里定义的呢?如果CurrentSongIndex不在每个DataGridRow的范围内,绑定将失败,这将解释为什么没有调用转换器。是否可以扩展代码示例以包含视图模型?
{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.CurrentSongIndex, Converter={StaticResource CurrentSongIndexConverter}}