开发者

WPF DataGrid Dynamic Styling

I have a DataGrid control in a WPF application. I want to be able to at runtime, select a value from a combo box, click a button and highlight the background color of all rows that meet this condition within the DataGrid. This was pretty easy to do in Windows Forms in the code behind, but I can not figure it out in WPF.

A开发者_开发知识库ny help gratefully received. Thank you


Hello I created a sample which solves this by using triggers and a valueconverter. Basically, I have a trigger on the gridcell, which is bound to the selecteditem of the combo-box. When you change selection, the trigger fires, and the cell uses the valueconverter to see if the selecteditem is the same as the value of the grid cell.

MainWindow.xaml

<Window x:Class="ComboBoxFilter.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ComboBoxFilter="clr-namespace:ComboBoxFilter" Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ComboBoxFilter:NameValueConverter x:Key="NameValueConverter" />
</Window.Resources>
<Grid>
    <StackPanel>
        <ComboBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" x:Name="TheComboBox" />
        <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Names}" x:Name="DataGrid"  >
            <DataGrid.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <DataTrigger Value="True" >
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource NameValueConverter}">
                                    <Binding Path="SelectedItem.Name" ElementName="TheComboBox" Mode="TwoWay" />
                                    <Binding Path="Name"  />                                        
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="Background" Value="Green" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>
        </DataGrid>
    </StackPanel>
</Grid>

MainWindow code behind file

namespace ComboBoxFilter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }

    }
}

NameValueConverter

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace ComboBoxFilter
{
    public class NameValueConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var string1 = values[0];
            var string2 = values[1];
            return string1 == string2;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

ViewModel

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace ComboBoxFilter
{
    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            Names = new ObservableCollection<Person>
                        {
                            new Person {Name = "Andy"},
                            new Person {Name = "hkon"},
                            new Person {Name = "dandy"},
                            new Person {Name = "Andy"}
                        };
        }

        private Person _selectedPerson;
        public Person SelectedPerson
        {
            get { return _selectedPerson; }
            set { _selectedPerson = value; NotifyPropertyChanged("SelectedPerson"); }
        }

        public ObservableCollection<Person> Names { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    public class Person
    {
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜