0

我在WPF中有GridView一个。ListViewListViewGridView列)有一个ObservableCollection填充它们的边界。

GridView给定 中的索引,如何更改 中的一行的背景ObservableCollection?我需要在 C# 中以编程方式执行此操作。

<ListView Margin="12,39,0,0" Name="Library" DataContext="{Binding}" ItemsSource="{Binding _songData}" Height="681" VerticalAlignment="Top" MouseDoubleClick="Library_MouseDoubleClick"  ContextMenu="{StaticResource MyContextMenu}" AlternationCount="2" Background="AliceBlue" HorizontalAlignment="Left" Width="846">
        <ListView.View>
            <GridView x:Name="gvLibrary">
                <!--<GridViewColumn Width="20" Header="hi" DisplayMemberBinding="{Binding isSongPlaying}" x:Name="gvColumnPlaying" />-->
                <GridViewColumn Width="220" Header="Title" DisplayMemberBinding="{Binding Title}" x:Name="gvColumnTitle" />
                <GridViewColumn Width="180" Header="Artist" DisplayMemberBinding="{Binding Artist}" x:Name="gvColumnArtist" />
                <GridViewColumn Width="180" Header="Album" DisplayMemberBinding="{Binding Album}" x:Name="gvColumnAlbum" />
                <GridViewColumn Width="180" Header="Location" DisplayMemberBinding="{Binding Location}" x:Name="gvColumnLocation" />
                <GridViewColumn Width="80" Header="File Type" DisplayMemberBinding="{Binding Ext}" x:Name="gvColumnFileType" />
            </GridView>
        </ListView.View>
    </ListView>
4

1 回答 1

0

最简单的方法是使用 aStyle和 a DataTrigger。首先IsSelected在 中包含的项目类上添加一个属性ListView

public class Song : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private bool _IsSelected;
    public bool IsSelected 
    {
        get { return _IsSelected; }
        set
        {
            _IsSelected = value;
            NotifyPropertyChanged("IsSelected");
        }
    }

    ...
}

然后将样式应用于您ListViewItem的 s:

    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

现在,每当您以编程方式设置 IsSelected 属性时,都会突出显示相应的行:

_songData[5].IsSelected = true;
于 2013-01-27T14:31:23.480 回答