我在WPF应用程序中使用Telerik Grid。如果我在XAML中定义所有内容,它可以正常工作,还有图像列:
<!-- IsActiveGroup -->
<telerik:GridViewDataColumn DataMemberBinding="{Binding IsActiveGroup}" Width="45">
<telerik:GridViewDataColumn.Header>
<TextBlock Text="Column name" ToolTip="Some tooltip"/>
</telerik:GridViewDataColumn.Header>
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=IsActiveImageSource, Converter={StaticResource imgConverter}}" Stretch="UniformToFill" RenderOptions.BitmapScalingMode="HighQuality" Width="20" Height="20" ToolTip="{Binding IsActiveGroup}" />
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
但是,我必须以编程方式执行此操作,但我不知道如何完成此操作。我试过了:
var column = new GridViewDataColumn();
column.UniqueName = uniqueName;
var headerTextBlock = new TextBlock();
headerTextBlock.Text = headerText;
headerTextBlock.ToolTip = headerTooltip;
column.Header = headerTextBlock;
var factory = new FrameworkElementFactory(typeof(Image));
factory.SetValue(Image.SourceProperty, new Binding("IsActiveImageSource")
{
Converter = new BinaryImageConverter()
});
factory.SetValue(Image.StretchProperty, Stretch.UniformToFill);
var cellTemplate = new DataTemplate() { VisualTree = factory };
column.CellTemplate = cellTemplate;
this.gridView.Columns.Add(column);
但是这没有做任何事情,我也不知道如何设置RenderOptions。
有人可以帮忙吗?
谢谢!
答案 0 :(得分:1)
我想你忘了加载模板:
factory.SetValue(Image.StretchProperty, Stretch.UniformToFill);
var cellTemplate = new DataTemplate() { VisualTree = factory };
cellTemplate.LoadContent();
column.CellTemplate = cellTemplate;
this.gridView.Columns.Add(column);
如果要在加载DataTemplate后为其分配一个DataContext:
cellTemplate.LoadContent();
cellTemplate.DataContext = YourDataContext;
希望它有所帮助!