我需要根据kendo GridViewColumn创建自定义网格列。
我的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>
这是我的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)
{
}
}
指定数据:
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"},
};
}
}
当我为此列分配数据时,它显示为空:
答案 0 :(得分:0)
列本身没有DataContext
。我们在这里尝试做什么有点不清楚,但是如果你想在列中显示source属性的值,你应该将TextBlock
绑定到它。
这要求您将源路径存储到源的某个位置,例如在依赖项属性中。这会显示值:
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:强>
<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>