locked
Using a markup extension in a style setter for Text property of TextBlock control RRS feed

  • Question

  • Hi all,
    I am trying to use a custom markup extension in a style setter for TextBlock style and I have run into some strange behaviour:

    Doing this:
    <Style x:Key="MyStyle" TargetType="{x:Type TextBlock}">  
      <Style.Triggers> 
        <DataTrigger Binding="{Binding ElementName=checkBox, Path=IsChecked}" Value="true">  
          <Setter Property="Text" Value="{myNamespace:MyMarkupExtension}" /> 
        </DataTrigger> 
      </Style.Triggers> 
    </Style> 
    Doing this I get a runtime exception:

    Cannot convert the value in attribute 'Style' to object of type 'System.Windows.Style'. 'MyMarkupExtension' is not valid for Setter.Value. The only supported MarkupExtension types are DynamicResourceExtension and BindingBase or derived types.

    If I specify ToolTip in  the setter, it works fine:
    <Setter Property="ToolTipext" Value="{myNamespace:MyMarkupExtension}" />

    If I use the markup extension in a TextBlock definition, it works fine too:
    <TextBlock Text="{MyNamespace:MyMarkupExtension}"/> 

    Why it does not work in a style setter? Is it a bug? Is there a workaround?

    Thanks for any answers.

    Jiri
    Wednesday, October 15, 2008 3:56 PM

Answers

All replies

  •  

    Hi,

     

    Here is a markup extension in a  style Setter example .

    http://dotnet.dzone.com/news/extend-wpf-add-your-own-keywor


    Hope this helps.

    Thanks. 

     


    Jim Zhou -MSFT
    Friday, October 17, 2008 9:20 AM
  • Thanks a lot for your answer.

    I know how to use and design Markup extensions event in style setters. The problem is with TextBlock's Text property. If I try to use a custom Markup extension in a style setter which Property="TextBlock.Text" and Value="{myNamespace:MyMarkupExtension}" I get an exception :

    Cannot convert the value in attribute 'Style' to object of type 'System.Windows.Style'. 'MyMarkupExtension' is not valid for Setter.Value. The only supported MarkupExtension types are DynamicResourceExtension and BindingBase or derived types.

    This seems to happen with TextBlock's Text property. So it is weird. Is it a bug or a correct behaviour?

    Jiri
    Monday, October 20, 2008 1:22 PM
  • Hi,

    I have the same problem here. Did you find a solution?

    Thanks!

    Jus
    Tuesday, January 19, 2010 4:05 PM
  • Same here. Did anyone find a solution for this?
    Wednesday, January 30, 2013 2:56 AM
  • Any solution there after 5 years?
    Thursday, July 5, 2018 12:54 PM
  • Hi,

    This should work:

    <Setter Property="Text" Value="{Binding UseFallbackValue, FallbackValue={myNamespace:MyMarkupExtension}}"/>


    Tuesday, December 4, 2018 2:36 AM