如何更改数据网格的RowBackground颜色

时间:2013-06-14 17:02:51

标签: wpf mvvm datagrid treeview

我目前正在研究WPF应用程序MVVM模式,我必须在树视图和网格之间创建某种关系。这种关系基于ID。我们的想法是突出显示id等于treenode id的行。

显示颜色属性

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

        get { return _displayColor; }

    }

选择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;
        }

    }

//在网格中搜索匹配的ID

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

XAML:调用display color属性以突出显示匹配的id。 数据网格         

的TreeView:

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

                  SelectedItemChanged="TreeView_OnSelectedItemChanged">

树视图选择项的代码;

    readonly MainWindowModel _mainWindowModel = new MainWindowModel();

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

编辑:

<DataGrid   ItemsSource="{Binding MessageFields}" Margin="4,0,380,6" Grid.Row="2" AutoGenerateColumns="False"  IsReadOnly="True"   SelectedValue="{Binding SelectedMessageField}"
                    RowBackground="{Binding Path=DisplayColor}">
            <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>

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

谢谢大家。

1 个答案:

答案 0 :(得分:2)

如果您使用的是MVVM模式,请不要在ViewModel中的VisualTree中定义Stuff,如Windows,DataGrids,画笔,等等

现在问你的问题

这是一个简单的例子:
如何更改数据网格的RowBackground颜色

XAML

    <DataGrid ItemsSource="{Binding Source}">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="Background" Value="Red"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ColorSwitch}" Value="false">
                        <Setter Property="Background" Value="Green"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
    </DataGrid>

代码隐藏

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new VM();
    }
}

视图模型

public class VM
{
    public List<myItem> Source {get;set;}

    public VM()
    {
        Source = new List<myItem>();
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=false});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=false});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=true});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=false});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=true});
    }
}

Simpleobject

public class myItem
{
    public string Field1 {get;set;}
    public string Field2 {get;set;}
    public bool ColorSwitch {get;set;}
}
相关问题