如何将数据从DataGridColumn绑定到DataGridColumnHeader

时间:2019-04-18 22:51:54

标签: wpf data-binding custom-controls

在WPF中,我试图创建一个自定义控件。

此控件派生自DataGrid。

每个列标题都应具有一个下拉列表,其中包含关联列的唯一值。

最终,我想基于下拉列表中的选定项创建谓词,并过滤显示在网格中的结果。

这是Generic.xaml文件:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:customControls="clr-namespace:WpfCustomControlLibrary2">

    <Style TargetType="{x:Type customControls:FilterDataGrid}"
           BasedOn="{StaticResource {x:Type DataGrid}}">
        <Style.Resources>
            <Style TargetType="{ x:Type DataGridColumnHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <StackPanel Orientation="horizontal">
                                <TextBlock Text="{Binding}"/>
                                <ComboBox ItemsSource="{Binding}" MaxWidth="600" MinWidth="200" >
                                    <ComboBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <CheckBox Selector.IsSelected="True" 
                                                    IsChecked="{Binding RelativeSource={RelativeSource Self},Path=(Selector.IsSelected), Mode=TwoWay}"/>
                                                <TextBlock Text="{Binding Element}"/>
                                            </StackPanel>
                                        </DataTemplate>
                                    </ComboBox.ItemTemplate>
                                </ComboBox>
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Height" Value="30"/>
                <Setter Property="Background" Value="Black"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="ToolTip" Value="Click to sort."/>
                    </Trigger>
                </Style.Triggers>

            </Style>
        </Style.Resources>
    </Style>
</ResourceDictionary>

该控件本身的代码为:

    using System;
    using System.Windows.Controls;
    using System.Windows;
    using System.ComponentModel;
    using System.Windows.Data;

namespace WpfCustomControlLibrary2
{
    public class FilterDataGrid : DataGrid
    {
        private ICollectionView Datalist { get; set; }
        public bool IsFilter { get; set; }

        public FilterDataGrid()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FilterDataGrid), new FrameworkPropertyMetadata(typeof(FilterDataGrid)));
        }
        protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
        {
            Datalist = CollectionViewSource.GetDefaultView(newValue);
            //Datalist.Filter = Predicate
            base.OnItemsSourceChanged(oldValue, Datalist);
            if (IsFilter)
                FilterColumnPredicates();
            UpdateSource();
        }
        private void UpdateSource()
        {
            throw new NotImplementedException();
        }

        private void FilterColumnPredicates()
        {
            throw new NotImplementedException();
        }
    }
}

为便于使用,这是MainWindow.xaml.cs文件:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<TestData> cs = new List<TestData>
            {
                new TestData("!", "@"),
                new TestData("1", "2"),
                new TestData("a", "s"),
                new TestData("d", "f"),
                new TestData("z", "x"),
                new TestData("c", "v")
            };
            this.Bob.IsFilter = true;
           var a = cs.Select(s => new { Value = s.SomeThing }.ToString()).ToList();
            ObservableCollection<string> ib = new ObservableCollection<string>(a);

            this.Bob.ItemsSource = cs.Select(s => new { s.SomeThing, s.SomeValue });
            this.Bob.ItemsSource = cs.Select(s => new { s.SomeThing, s.SomeValue });
        }
    }
    public class TestData
    {
        public string SomeValue;
        public string SomeThing;
        public TestData(string val1, string val2)
        {
            SomeValue = val1;
            SomeThing = val2;
        }
    }
}

现在,我无法直接绑定到列(显示的内容)(因为这样我将永远无法获得从过滤器中删除的值重新出现)。
有什么方法可以绑定到DataGrid ItemsSource的关联列中的值?

0 个答案:

没有答案
相关问题