I have markup extensions to allow me use binding and cell template in GridView
at the same time. It works fine at runtime, but it does not work at design time, wondering if there is anything I could do to fix that. I've tested returning simple string instead of DataTemplate
just to make sure, that custom markup extensions work in general at design time - and it worked, so it should be somehow related to the fact, that DataTemplate
is returned.
[MarkupExtensionReturnType(typeof(DataTemplate))]
public class TemplateBuilderExtension : MarkupExtension
{
public string Path { get; set; }
public TemplateBuilderExtension() { }
public TemplateBuilderExtension(string path)
{
Path = path;
}
// Here be dirty hack.
internal static string TagPath { get; private set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
TagPath = Path;
var resourceExt = new StaticResourceExtension("GridViewTextCell");
// This line causes the evaluation of the Tag as the resource is loaded.
var baseTemplate = (DataTemplate)resourceExt.ProvideValue(serviceProvider);
return baseTemplate;
}
}
[MarkupExtensionReturnType(typeof(BindingExpression))]
public class TemplateBuilderTagExtension : MarkupExtension
{
public TemplateBuilderTagExtension()
{
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new Binding(TemplateBuilderExtension.TagPath);
}
}
<Window.Resources>
<DataTemplate x:Shared="false" x:Key="GridViewTextCell">
<Border BorderBrush="Blue" BorderThickness="1">
<TextBlock Text="{markupExtensions:TemplateBuilderTag}"></TextBlock>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<ListView SelectedIndex="5">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" CellTemplate="{markupExtensions:TemplateBuilder Id}" Width="300"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
Update: I've simplified code to be as short as possible, in real situation there are multiple GridView's through application, each grid contains multiple columns and those columns should reuse same template and also I can't use DataGrid because of performance issues.
Your extensions do not make much sense. That all can be written like this:
Binding itself is also an extension. You sort of trying to extend a extension...
How about you leave it be and use the normal approach instead? :)