WPF ListView Highlight Color don't change

后端 未结 3 2061
星月不相逢
星月不相逢 2021-01-15 23:40

I have a ListView with three TextBlock for each Item. The first one has the default color (black) and the others has the property Foreground set to gray.

When I sele

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 00:10

    You could achieve what you are trying to do by converting your code from having DataTemplate for a ListViewItem to having a ControlTemplate

    This is what I tried:

    ListViewItem Style:

    
    

    and then I removed DataTemplate from the ListView XAML:

    
    
    

    However, if you must use DateTemplate, then what you could do is have a property called IsSelected on your ViewModel, ResultatRechercheViewModel and then have DataTriggers on that property in your DataTemplate.

    Updated DataTemplate:

    
        
            
                
                    
                    
                    
                    
                
                
                
                
    
            
            
                
                    
                    
                
            
        
    
    

    And, you need to update your ViewModel code to set IsSelected property, Below is code from my MainViewModel:

    public ResultatRechercheViewModel ResultatSelectione
    {
        get { return _resultatSelectione; }
        set
        {
            if (_resultatSelectione != null)
            {
                _resultatSelectione.IsSelected = false;
            }
    
            _resultatSelectione = value;
    
            _resultatSelectione.IsSelected = true;
        }
    }
    

    Hope this resolves your problem or gives you some ideas to resolve your problem.

提交回复
热议问题