c# - WPF 突出显示 Datagrid 的选定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17110114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:00:37  来源:igfitidea点击:

c# - WPF highlight Selected Row of Datagrid

wpfmvvmdatagridtreeview

提问by Ayda Sayed

I am working on a WPF application MVVM pattern where I have to create some sort of relationship between a TreeViewand a Grid. The idea is to highlight a row with a id equal to the treenode id.

我正在研究 WPF 应用程序 MVVM 模式,我必须在其中创建 aTreeView和 a之间的某种关系Grid。这个想法是突出显示一个id等于treenode id的行。

Display color property

显示颜色属性

 public Brush DisplayColor
    {
        set
        {
            _displayColor = value;
            NotifyPropertyChanged("DisplayColor");
        }

        get { return _displayColor; }

    }

**Select TreeNode value.id** 



    private MessageElementViewModel _selectedMessageElement;
            public MessageElementViewModel SelectedMessageElement
            {
                set
                {
                    if (_selectedMessageElement == value) return;
                    this._selectedMessageElement = value;
                    SearchGrid(value.Id, messageFields);

                }
                get
                {
                    return this._selectedMessageElement;
                }

            }
    // search matching Id in the grid

             public void SearchGrid(int id, ObservableCollection<MessageFieldViewModel> msgField)
            {
                 if (msgField.Any())
                    DisplayColor = msgField.Last().Id == id ? Brushes.CadetBlue : Brushes.Black;
            }

XAML: Call the display color property to highlight a matching id.
DataGrid


 <DataGrid   ItemsSource="{Binding MessageFields}" Margin="4,0,380,6" Grid.Row="2" AutoGenerateColumns="False"  IsReadOnly="True"   SelectedValue="{Binding SelectedMessageField, Mode=TwoWay}"
                    RowBackground="{Binding Path=DisplayColor}">

TreeView:



 <TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding MessageElements[0].Children[0].Children,  Mode=TwoWay}"   
                  SelectedItemChanged="TreeView_OnSelectedItemChanged"
                 SelectedValuePath="Id">

Code Behind for treeview select item;



readonly MainWindowModel _mainWindowModel = new MainWindowModel();

    private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (_mainWindowModel != null)
            _mainWindowModel.SelectedMessageElement = (MessageElementViewModel)e.NewValue;
    }

EDIT:

编辑:

     <DataGrid Name="Datagrid"    ItemsSource="{Binding MessageFields}" Margin="4,0,380,6" Grid.Row="2" AutoGenerateColumns="False"  IsReadOnly="True"   SelectedValue="{Binding SelectedMessageField}"
                            AlternatingRowBackground ="{Binding Path=DisplayColor}" RowBackground="{Binding Path=DisplayColor}" Background="CadetBlue">
                    <DataGrid.Columns >
                        <DataGridTextColumn Header="ID" Binding="{Binding Id}" Width="*"  />      <!--Foreground="{Binding Path=DisplayColor}-->

                        <DataGridTextColumn Header="Code" Binding="{Binding Code}" Width="*" />
                        <DataGridTextColumn Header="Field Name" Binding="{Binding Name}" Width="*" />
                        <DataGridTextColumn Header="Position" Binding="{Binding Position}" Width="*"   />
                        <DataGridTextColumn Header="Length" Binding="{Binding Length}" Width="*"  />
         </DataGrid.Columns>
    </DataGrid> 

Why is my is my display color property is not working for matching Ids?

为什么我的显示颜色属性不适用于匹配 ID?

采纳答案by Ayda Sayed

I guess I found a way to resolve this but I hate it cause I am doing it from code behind. Would be nice if I can do it in my VM.

我想我找到了解决这个问题的方法,但我讨厌它,因为我是从代码后面做的。如果我可以在我的 VM 中做到这一点就好了。

    private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {

        foreach (MessageFieldViewModel item in Datagrid.ItemsSource)
        {
            if (_mainWindowModel != null)
                _mainWindowModel.SelectedMessageElement = (MessageElementViewModel) e.NewValue;


            foreach (MessageFieldViewModel rowItem in Datagrid.ItemsSource)
            {
                var row = Datagrid.ItemContainerGenerator.ContainerFromItem(rowItem) as DataGridRow;
                if (_mainWindowModel.SelectedMessageElement != null)
                    if (rowItem.Id == _mainWindowModel.SelectedMessageElement.Id)

                        row.Background = _mainWindowModel.DisplayColor;

                     else
                    row.Background = Brushes.White;

            }
        }


    }

回答by WiiMaxx

You could simply use Triggers

你可以简单地使用触发器

XAML

XAML

    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="Background" Value="Red"/><!--default color-->
            <Style.Triggers>
                <DataTrigger Binding="{Binding DisplayColor}" Value="Black">
                    <Setter Property="Background" Value="Black"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding DisplayColor}" Value="CadetBlue">
                    <Setter Property="Background" Value="CadetBlue"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>

suggesting your Datagrid.ItemsSourceis an List<MyClass>your MyClasshas to contain the following Property

这表明你Datagrid.ItemsSource是一个List<MyClass>MyClass必须包含以下Property

public string DisplayColor //don't use Brush
{
    set
    {
        _displayColor = value;
        NotifyPropertyChanged("DisplayColor");
    }

    get { return _displayColor; }

}

further explanations HERE

进一步的解释在这里