首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF DataGrid动态样式

WPF DataGrid动态样式
EN

Stack Overflow用户
提问于 2010-08-14 20:39:50
回答 1查看 1.3K关注 0票数 1

我在一个WPF应用程序中有一个DataGrid控件。我希望能够在运行时从组合框中选择一个值,单击一个按钮并突出显示DataGrid中满足此条件的所有行的背景色。在后面的代码中,在Windows窗体中很容易做到这一点,但我无法在WPF中理解它。

感谢您给予的任何帮助。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-14 22:41:54

你好,我创建了一个示例,它通过使用触发器和值转换器来解决这个问题。基本上,我在网格单元格上有一个触发器,它绑定到组合框的selecteditem。当您更改选择时,触发器将触发,单元格将使用valueconverter来查看selecteditem是否与网格单元格的值相同。

MainWindow.xaml

代码语言:javascript
复制
<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代码隐藏文件

代码语言:javascript
复制
namespace ComboBoxFilter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }

    }
}

NameValueConverter

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3483313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档