设计时的MarkupExtension.ProvideValue(IServiceProvider serviceProvider)

时间:2013-08-25 11:13:53

标签: c# wpf xaml

如果你自己编写MarkupExtension,你可能偶然发现IServiceProvider serviceProvider - 参数在设计时在运行期间是不同的。例如,您无法获得IRootObjectProvider。那么有关于参数对象实现哪个Interfaces的文档,是否有解决方法来获取根对象?

2 个答案:

答案 0 :(得分:2)

写了一个小助手,看看哪些服务可用:

public class ServiceExtension : MarkupExtension
{
    public ServiceExtension()
    {
    }

    public ServiceExtension(Type serviceType)
    {
        ServiceType = serviceType;
    }

    [ConstructorArgument("serviceType")]
    public Type ServiceType { get; set; }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public object Service { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (ServiceType == null)
        {
            Service= "ServiceType == null";
            return this;
        }
        var service = serviceProvider.GetService(ServiceType);
        Service= service ?? "null";
        return this;
    }
}

<Style TargetType="{x:Type HeaderedContentControl}">
    <Setter Property="Header" Value="{Binding ServiceType}" />
    <Setter Property="Content" Value="{Binding Service}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Col1" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Grid.Column="0"
                                      Margin="0,0,5,0"
                                      ContentSource="Header" />
                    <ContentPresenter Grid.Column="1" ContentSource="Content" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ItemsControl Grid.IsSharedSizeScope="True">
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IXamlNameProvider}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IXamlNameResolver}}" />

    <HeaderedContentControl DataContext="{local:Service {x:Type x:IProvideValueTarget}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type x:IXamlTypeResolver}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type x:IValueSerializerContext}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type componentModel:ITypeDescriptorContext}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IDestinationTypeProvider}}" />

    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IXamlNamespaceResolver}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IXamlSchemaContextProvider}}" />

    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IRootObjectProvider}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type x:IUriContext}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IAmbientProvider}}" />
</ItemsControl>

答案 1 :(得分:1)