Adding FontStyle to the property sheet doesn't change the text style

Yanıt Adding FontStyle to the property sheet doesn't change the text style

  • 11 Haziran 2012 Pazartesi 11:35
     
      Kod İçerir

    Hello,

    I've been reading this MSDN article on overriding properties for custom controls, and I am trying to add a font style property to my control's property sheet.I tried a simple custom control, as follows...

    <UserControl x:Class="TestControls.Presentation.Controls.Ferret"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:vc="clr-namespace:TestControls.Presentation.Controls">
      <TextBlock Text="{Binding Value}" />
    </UserControl>
    

    ...and this works fine. I can bind this to a data item, and the value is displayed. However, I want to set the font style. According to that MSDN article, it should be as easy as adding the following to the .lsml file...

        <Control.PropertyOverrides>
          <ControlPropertyOverride
            Property=":RootControl/Properties[FontStyle]"
            EditorVisibility="PropertySheet"/>
        </Control.PropertyOverrides>
    

    However, the font of the TextBlock in the .xaml wasn't changed.

    I tried binding the FontStyle property of the TextBlock to the FontStyle property of the control, but this didn't work, and the output window showed a binding error saying it couldn't find the FontStyle property, which makes sense as I didn't define one.

    What am I doing wrong? I'm obviously missing something simple, as the article made it sound like all you needed to do was add the property override.

    Thanks for any help


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/

Tüm Yanıtlar

  • 15 Haziran 2012 Cuma 07:21
    Moderatör
     
     Yanıt Kod İçerir

    One tiny thing you need to add to the metadata:

    <Control.PropertyOverrides>
      <ControlPropertyOverride
        Property=":RootControl/Properties[FontStyle]"
        EditorVisibility="PropertySheet"
        IsReadOnly="False"/>
    </Control.PropertyOverrides>

    The framework logic ignores the property value setting if it is read-only (which it is read-only by default). This wasn't obvious at all until I looked up how the property was being used.

    No need to do any kind of binding to this property on the content item. The ContentItemPresenter will set it's own FontFamily, FontSize, FontWeight, FontStyle, and Foreground based on the LightSwitch FontStyle property setting. TextBlock will inherit these properties so long as nothing between the ContentItemPresenter (the control that hosts your control) and the TextBlock in the visual tree sets these properties.


    Justin Anderson, LightSwitch Development Team

    • Yanıt Olarak İşaretleyen Mr Yossu 17 Haziran 2012 Pazar 12:34
    •  
  • 17 Haziran 2012 Pazar 12:36
     
     

    That's brilliant Justin, thanks very much.

    Please could you pass a request to whoever is in charge of the MSDN page I linked in my original post, and have them update the page to make this clear. Actually, while they are at it, that whole page could do with being made more clear. It took me a long time to work out what was being said. It could do with a lot of clarification, and a lot more sample code.

    Thanks again, now I have another custom control to release on the unsuspecting public!


    FREE custom controls for Lightswitch! A collection of useful controls for Lightswitch developers. Download from the Visual Studio Gallery.

    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/