开发者

Setting controls in a Telerik GridView cell

Here's a concrete example of what I am attempting to do with the Telerik GridView control. Let's say I have an application that will read a delimited file (say CSV) which will have n columns. The value n can vary from file-to-file but is constant within a file. The application starts with an empty grid and adds columns as required to match the input file. The grid displays all data as strings in all cells. This works with either binding to a BindingList or putting the record (objects) into the GridView.Items list.

Now, what I want to do is put a single row at the top of the grid (a row that will not scroll) that contains comboboxes. That is, at the top of each column, the first row contains a combobox. On the first pass, the combobox will only be a drop list, but on the next pass I will add another row with a set of comboboxes that will be editable. For now, let's only consider drop lists.

The specific problem that I have is that I do not see how to set a specific type of control for a particular cell. Telerik provides a GridViewComboBoxColumn class that will define the behavior for an entire column but that's not what I need.

Because of the variable number of columns, I think that the code-behind would be the place to work this magic. I may have to do something in the xaml but, since I've only been in WPF for a few months, nothing is jumping out at me.

I've done something like this with the DataGridView and XtraGrid, but this one has me stumped. Pointers would be much appreciated.


In response to Jonathan D's answer, I have taken the provided code and modified it to recognize when a cell on the 0th row is being edited. When this is the case, a drop list is presented when the user initiates an edit operation.

using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace LLamasoft.DataGuru.Plugin.Internal.ConfigurationUI
{
    public class RadGridViewComboboxHeaderC开发者_如何学Column : GridViewBoundColumnBase
    {
        public static readonly DependencyProperty SelectedStringProperty =
            DependencyProperty.Register("SelectedString", typeof(string), typeof(RadGridViewComboboxHeaderColumn), new PropertyMetadata(null));

        public string SelectedString
        {
            get { return (string) GetValue(SelectedStringProperty); }
            set { SetValue(SelectedStringProperty, value); }
        }

        public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
        {
            // we need the row on which this cell lives
            GridViewDataControl gridViewDataControl = cell.ParentRow.GridViewDataControl;
            object currentEditItem = gridViewDataControl.Items.CurrentEditItem;
            int index = gridViewDataControl.Items.IndexOf(currentEditItem);

            FrameworkElement frameworkElement = null;
            if (index == 0)
            {
                BindingTarget = ComboBox.SelectedValueProperty;
                ComboBox comboBox = new ComboBox();
                // seed some values,
                // this list should be set right after construction if static, otherwise via callback if dynamic
                comboBox.Items.Add(string.Empty);
                comboBox.Items.Add("apples");
                comboBox.Items.Add("oranges");
                if (!comboBox.Items.Contains(cell.Value))
                {
                    comboBox.Items.Add(cell.Value);
                }
                comboBox.SelectedValue = SelectedString;
                frameworkElement = comboBox;
            }
            else
            {
                BindingTarget = TextBox.TextProperty;
                TextBox textBox = new TextBox();
                textBox.Text = SelectedString;
                frameworkElement = textBox;
            }

            frameworkElement.SetBinding(BindingTarget, CreateValueBinding());

            return frameworkElement;
        }

        public override object GetNewValueFromEditor(object editor)
        {
            // ensure that the control will return the correct value when queried for it
            ComboBox comboBox = editor as ComboBox;
            if (comboBox != null)
            {
                // bound to comboBox.SelectedValue which carries the correct value
            }
            TextBox textBox = editor as TextBox;
            if (textBox != null)
            {
                // bound to textBox.Text which carries the correct value
            }

            return base.GetNewValueFromEditor(editor);
        }

        private Binding CreateValueBinding()


   {
        Binding valueBinding = new Binding();
        valueBinding.Mode = BindingMode.TwoWay;
        valueBinding.NotifyOnValidationError = true;
        valueBinding.ValidatesOnExceptions = true;
        valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
        valueBinding.Path = new PropertyPath(this.DataMemberBinding.Path.Path);

        return valueBinding;
    }
}

}

The good news is that this shows that any edit control can be used in any cell based on requirements.

The bad parts are: 1) a dummy record has to be inserted at the 0th list position and must be maintained there, 2) the data is being stored back into the field on the 0th record and may require a different data type than is on the equivalent fields on the other records, and 3) the combobox is only shown when the cell is in the edit mode.

The latter issue for me may not be an issue elsewhere. I want a visual cue that the user is expected to interact with the cells at the top of the columns. Using this method, there is no differentiating factor between the top row and the rest of the rows until the edit operation begins. My ideal solution would have the cells always show their comboboxes.

One other issue that I find difficult to believe that I am facing is the fact that I cannot easily pin/freeze topmost rows. I want this line to always remain at the top after scrolling. There is no _grid.Rows[0].IsPinned = true functionality.

Telerik has responded to my request for info and suggests that I use a template selector to determine how the cell is represented. (http://www.telerik.com/community/forums/wpf/gridview/need-just-first-row-in-grid-to-be-all-comboboxes.aspx#1820310). At this point, I turn my attention to testing that method.


You want to create your own column

using System.Windows;
using System.Windows.Data;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System;

namespace Inspect
{
    public class DateTimePickerColumn : GridViewBoundColumnBase
    {
        public TimeSpan TimeInterval
        {
            get
            {
                return (TimeSpan) GetValue(TimeIntervalProperty);
            }
            set
            {
                SetValue(TimeIntervalProperty, value);
            }
        }

        public static readonly DependencyProperty TimeIntervalProperty =
            DependencyProperty.Register("TimeInterval", typeof(TimeSpan), typeof(DateTimePickerColumn), new PropertyMetadata(TimeSpan.FromHours(1d)));



        public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
        {
            this.BindingTarget = RadDateTimePicker.SelectedValueProperty;

            RadDateTimePicker picker = new RadDateTimePicker();
            picker.IsTooltipEnabled = false;

            picker.TimeInterval = this.TimeInterval;

            picker.SetBinding(this.BindingTarget, this.CreateValueBinding());

            return picker;
        }

        public override object GetNewValueFromEditor(object editor)
        {
            RadDateTimePicker picker = editor as RadDateTimePicker;
            if (picker != null)
            {
                picker.DateTimeText = picker.CurrentDateTimeText;
            }

            return base.GetNewValueFromEditor(editor);
        }

        private Binding CreateValueBinding()
        {
            Binding valueBinding = new Binding();
            valueBinding.Mode = BindingMode.TwoWay;
            valueBinding.NotifyOnValidationError = true;
            valueBinding.ValidatesOnExceptions = true;
            valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
            valueBinding.Path = new PropertyPath(this.DataMemberBinding.Path.Path);

            return valueBinding;
        }
    }
}

That is how you create a custom column. If you modify the CreateCellEditElement Method it will let you create custom cells how you like. You should even be able to detect the row number

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜