[WPF] problem analysis for setting ToolTip in style

Source: Internet
Author: User

Just opened to the wisdom of the "WPF" in the style Set ToolTip blog post, although eventually gave a temporary solution to the problem, but did not analyze and explain the problem, It is with him: But as for why it is not possible to place TextBlock directly in Setter.value is an unsolved mystery.

Take advantage of the noon gap, followed a bit, here I will give you a complete analysis.

To describe the problem, first of all, to the problematic XAML, of course, you can also go to the sage's blog to view the detailed description.

<textblock x:name= "Textblockcontainer" text= "ABC" margin= "ten" >
    <!--The following wording is not a problem-->
    <!--< tooltipservice.tooltip>
        <textblock 
                        text= "//Get text from somewhere by binding"
                        textwrapping= "Wrap"
                        width= " />
    </ToolTipService.ToolTip>-->
    <!--use style to assign values to TOOLTIP, error! Will throw exception-->
    <TextBlock.Style>
        <style targettype= "TextBlock" >
            <setter property= "Tooltipservice.tooltip" >
                <Setter.Value>
                    <textblock x:name= "Tooltipblock"
                        text= "//Get the text from somewhere by binding,"
                        textwrapping= "Wrap"
                        width= "/>
                </Setter.Value>
            </ setter>
        </Style>  
    </TextBlock.Style>
</TextBlock>

The information for the exception is:

Exception

System.Windows.Markup.XamlParseException occurred
  message= "Cannot add content of type" System.Windows.Controls.TextBlock ' to an object of type ' System.Object '.  Error at object ' System.Windows.Controls.TextBlock ' in markup file ' Wpfapplication1;component/window1.xaml ' line 17 Position. "
  Source= "PresentationFramework"
  linenumber=17
  lineposition=30
  namecontext= "Value"
  stacktrace: At
       System.Windows.Markup.XamlParseException.ThrowException (String message, Exception innerexception, Int32 LineNumber, Int32 lineposition, Uri BaseUri, Xamlobjectids currentxamlobjectids, Xamlobjectids contextxamlobjectids, Type objectType)  innerexception:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.