سؤال

I have a simple IMarkupExtension as follows:

public class HelloWorldMarkup : IMarkupExtension<string>
{
    public string ProvideValue(IServiceProvider serviceProvider)
    {
        return "Hello World";
    }
    public override string ToString()
    {
        return "DesignTime Hello World";
    }
}

and my Xaml that uses it like this..

<StackPanel>
    <TextBlock Text="{my:HelloWorldMarkup}" />
    <HyperlinkButton Content="{my:HelloWorldMarkup}" />
</StackPanel>

At runtime, it all works as expected.

At design time however, the Content of the hyperlink shows the design time values (from ToString), but the TextBlock's Text does not show.

If I leave it like this, my designer will whinge to me for days.. Does anyone have any suggestions on how I can have my Markups display design time data in TextBlock Text?

Many thanks,

هل كانت مفيدة؟

المحلول

try..

<TextBlock DataContext="{my:HelloWorldMarkup}" Text="{Binding}" />

نصائح أخرى

You're halfway on the right track. There's some nice workaround to this ("by design") problem:

Use the interface IMarkupExtension and derive from some control with a content property ( e.g. ContentControl). Now listen to changes to the "Parent" property (you may have to use some tricky workaround using attached properties). The event callback should then call ProvideValue on its own using a custom simple IProvideValueTarget implementation. The result of ProvideValue must then be assigned to the "Content" property. This does not affect runtime as ProvideValue will be evaluated before the control and works like a charm in design time.

I'm also thinking about installing a Binding on the target property thus reducing the base class to FrameworkElement.

Refer to https://github.com/MrCircuit/XAMLMarkupExtensions and https://github.com/MrCircuit/WPFLocalizationExtension for an example of this process.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top