简体   繁体   English

WPF Telerik创建自定义网格列

[英]WPF Telerik Create custom grid column

I need to create custom grid column based on kendo GridViewColumn. 我需要基于kendo GridViewColumn创建自定义网格列。

My xaml: 我的xaml:

<telerikGrid:RadGridView ItemsSource="{Binding CmbContest}" AutoGenerateColumns="False" >
            <telerikGrid:RadGridView.Columns>
                <telerikGrid:GridViewDataColumn Header="TestColumn" DataMemberBinding="{Binding Name}" ></telerikGrid:GridViewDataColumn>
                <local:MultiDropDownColumn Header="SelectColumn"  SelectedItem="{Binding SelectedDropDown, Mode=TwoWay}"></local:MultiDropDownColumn>
            </telerikGrid:RadGridView.Columns>
        </telerikGrid:RadGridView>

Here is my MultiDropDownColumn class: 这是我的MultiDropDownColumn类:

public class MultiDropDownColumn : Telerik.Windows.Controls.GridViewBoundColumnBase
    {               
        [BindableAttribute(true)]
        public string SelectedItem
        {
            get
            {
                return (string)GetValue(SelectedItemProperty);
            }
            set
            {
                SetValue(SelectedItemProperty, value);
            }
        }

        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {

            TextBlock tb = cell.Content as TextBlock;
            if (tb == null)
            {
                tb = new TextBlock();
            }
            tb.Text = this.SelectedItem;
            return tb;
        }

        public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register(
            "SelectedItem",
            typeof(string),
            typeof(MultiDropDownColumn),
            new PropertyMetadata(string.Empty, OnSelectedItemChanged));

        private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {           
        }

    }

Assigned Data: 分配的数据:

public class ViewModel
    {
        public List<DTO> CmbContest { get; set; }
        public ViewModel()
        {                
            CmbContest = new List<DTO>
            {
                new DTO{ Name="row1", SelectedDropDown="2"},
                new DTO{ Name="row2", SelectedDropDown="1"},
            };
        }

    }

When I assign data to this column, it appears empty: 当我将数据分配给此列时,它显示为空:

空结果

The column itself has no DataContext . 列本身没有DataContext It's a bit unclear what you are trying to do here but if you want to want to show the value of the source property in your column you should bind the TextBlock to it. 尚不清楚您要在此处执行的操作,但是如果要在列中显示source属性的值,则应将TextBlock绑定到该属性。

This requires you to store the path to the property of the source somewhere, for example in your dependency property. 这就要求您将指向源属性的路径存储在某个位置,例如在依赖项属性中。 This displays the value: 这将显示值:

public class MultiDropDownColumn : Telerik.Windows.Controls.GridViewDataColumn
{
    public string SelectedItem
    {
        get
        {
            return (string)GetValue(SelectedItemProperty);
        }
        set
        {
            SetValue(SelectedItemProperty, value);
        }
    }

    public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
    {

        TextBlock tb = cell.Content as TextBlock;
        if (tb == null)
        {
            tb = new TextBlock();
        }
        tb.SetBinding(TextBlock.TextProperty, new Binding(SelectedItem));
        return tb;
    }

    public static readonly DependencyProperty SelectedItemProperty =
    DependencyProperty.Register(
        "SelectedItem",
        typeof(string),
        typeof(MultiDropDownColumn),
        new PropertyMetadata(string.Empty, OnSelectedItemChanged));

    private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    }
}

XAML: XAML:

<telerikGrid:RadGridView x:Name="CmbContest" ItemsSource="{Binding CmbContest}" AutoGenerateColumns="False" >
    <telerikGrid:RadGridView.Columns>
        <telerikGrid:GridViewDataColumn Header="TestColumn" DataMemberBinding="{Binding Name}" ></telerikGrid:GridViewDataColumn>
        <local:MultiDropDownColumn Header="SelectColumn" SelectedItem="SelectedDropDown" />
    </telerikGrid:RadGridView.Columns>
</telerikGrid:RadGridView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM