in

vbCity Blogs

New (temp) place for vbCity Blogs

Ged Mead's Blog

December 2008 - Posts

  • WPF Text Tricks - MultiLine Text and Multiple Spaces

     

      When it comes to text handling in WPF, you have a massive range of tools available, some of which can offer you a level of granularity that was previously unachievable. I plan to write about some of these text tools, such as Spans, Runs and Glyphs in future blogs, but for today I want to look at something a little less deep.

      Recently I needed to insert a carriage return in a block of text. The text was being created in the XAML markup, and I didn't particularly want to resort to code-behind for what is essentially a UI feature. So I had to do a bit of digging to find how I could create, for instance, this result:-

      

      where, if I don't override it, the two words would be placed one after the other on one line because there is more than enough width for them both.

      As you might expect, there are several ways of achieving this, but I think the easiest one is to use a LineBreak element. This snippet shows the key part of the code that creates and formats the Button's Content, which in this example is of course text only.

        <Button Margin="12,45" >

          <TextBlock> Click <LineBreak/> Me!</TextBlock>

        </Button>

     

       You might think it a bit strange when you see that XML type item stuck into the middle of a block of text, but this is perfectly valid syntax. Because the Content isn't a single block of text, it's necessary to use the TextBlock to house the text and the LineBreak instruction.

      Actually, what I really wanted to do wasn't quite as plain as the above screenshot. It was more along these lines:-

      

      The markup for this is still not particularly complex. It just requires more properties to be set specifically:

        <Button  Margin="12,45" Background="LightSkyBlue">
          <Button.Content>
            <TextBlock TextAlignment="Center" FontSize="16" FontFamily="Calibra"
                       FontWeight="Bold" Foreground="Navy" FontStyle="Italic" >
              Click <LineBreak/> Me!</TextBlock>
          </Button.Content>
        </Button>

     

      I did mention that there are several ways and these include using a StackPanel containing multiple TextBlocks or setting the Padding property on a single TextBlock. An alternative you might consider is to preserve the whitespace. This effectively means that however you enter the text layout in your XAML will be faithfully reproduced in the WPF display.

      The following markup:

        <Button  Margin="12,40">
          <TextBlock FontSize="14" VerticalAlignment="Center"
    HorizontalAlignment="Center" xml:space="preserve">Click
    Me!</TextBlock>
        </Button>

     

      will achieve a similar result:

      

      However, I really don't think the way the Markup sits in the XAML markup window is particularly neat. If you're interested in experimenting, try moving the word "Me!" so that it is more neatly aligned. Or Start the word "Click" on a new line of its own, maybe aligned with the markup above it.

      The results are unlikely to be what you wanted to achieve and this is because the preservation is absolute. A XAML twist on WYSIWYG!

      However, I included it here because the space preserve approach will be very useful to you in those cases where you want more than a single space between words or characters. Simply type out the text with the exact spacing that you require, set the XML Space attribute to Preserve and your requirements will be honored.

        <Button  Margin="12,40">
          <TextBlock FontSize="14" VerticalAlignment="Center"
              HorizontalAlignment="Center" 
              xml:space="preserve">Name:    Ged</TextBlock>
        </Button>

     

      

       So there are a couple of tips for dealing with text in WPF.  There's more to come!

    Posted Dec 12 2008, 04:51 PM by XTab with no comments
    Filed under: ,
  • Runtime Error in Internet Explorer 7

     

        For the past week or so I've been getting those annoying little popup "Runtime Error - Do you want to Debug?" message boxes in Internet Explorer 7.   I've had them very occasionally in the past, but they were becoming a regular occurrence - sometimes three or four times in a row on the same page.

       I managed to ignore them until that point, but finally decided that I'd better do something about it.  

       A quick search came up with some advice that (after a minor tweak) seems to work fine.   So if you're sat there reading this because you keep getting similar messages, then here's what fixed it for me:

    1. In IE7, go to the Tools menu.
    2. Select "Manage Add-ons"
    3. Then select "Enable or Disable Add-ons"
    4. You can then trawl through the list of Add-ons that is displayed, select any or all of them and change its Setting to "Disable".

       I did this for most of the Add-ons that I thought were probably the most recent.  (They're not dated, but I know I've successfully used several of the applications that use named Add-ons for a long time without this problem, so I left those in).

      So far, the error messages haven't reappeared.

      I will re-enable some of the Add-ons as and when they are asked for by sites I visit and - now that I know the score - will make sure I know the name of whatever is being added, so that I can dump it if it turns out to be the rogue that caused this week's problems.

     

     

  • Formatting Blog Code - CopySourceAsHTML

     

      For several years, when I've needed to include code snippets in my blogs and articles, I've used a neat little application that VBCity member HotDog wrote. This has stood me in good stead until the day that XAML arrived on the scene. Unfortunately, XAML kinda confuses HotDog's AutoFormatter tool and I end up with missing angle brackets.

      A bit of searching led me to a tool called CopySourceAsHTML. Although I had seen this used with Visual Studio 2005, there was a minor problem using it with 2008 for a while, but this was easily fixed with a minor tweak.

      Now though there is an official Visual Studio 2008 version. The extra good news about this is that it will also copy XAML to HTML.

      One thing to watch out for if you have used the tool before and usually right click on a code snippet to copy is as HTML:

      

         

     

       you will expect to see the Copy As HTML option in the menu.

      However, this doesn't appear in the context menu if you right click on the markup in the XAML Markup Pane. But it is available and all you have to do is go to the IDE main menu, select Edit and the Copy As HTML menu item will be available for you there.

      This great little utility is written (and updated) by Colin Coller of J T Leigh and Associates. You can download it from here.

    Posted Dec 08 2008, 08:44 AM by XTab with no comments
    Filed under: , ,
  • WPF Gotchas: What's the WPF Equivalent of the Text Property?

     

     

     One of the things that often pulls me up in mid flow when I'm putting WPF UIs together in a Window is that some of the core properties seem to have disappeared. Now, some of these are so fundamental that I know they have to be there, but obviously hiding under a different name.

      Just recently, this happened to me twice in a row and I ended up scrolling through the Properties in the Object Browser, getting dizzy with the whizzing list of choices until I finally found them. All I was looking for was a very simple thing in both cases. I just wanted to apply some text to a control.

      The first one was the good old CheckBox. In Windows Forms this has a Text property, of course, and it's whatever textual information you want to place at the side of the little square check area. The WPF CheckBox will allow you to do a zillion clever things to change its look - add Borders, TextBlocks, Images, Panels, whatever you like - and at times I'm really happy about that. But as it happened this time I just wanted to plonk a couple of words of description at the side.

      You'll have gathered that there isn't a Text property for the WPF CheckBox and after a bit of a search, I did find its replacement - the Content property. If you assign plain text to this property, WPF will honor your choice. So this:

       17 <CheckBox x:Name="CheckBox1" Content="Click Here" />

     

      will do the job nicely.

      Subsequently I discovered that the Content property is in fact the default property of a CheckBox, so this too will work:

       17  <CheckBox x:Name="CheckBox1">"Click Here"</CheckBox>

     

      Of course if you do want something more exotic, then you still use the Content property - you just add whatever multiple layers of content you desire.

       The other control for which I had to search for the same kind of thing was the Expander. Again I only wanted a property that would just tack some plain text to the side of the Expander's little arrow.

      This time the answer is to use the Expander's Header property.

    <Expander Header="See More Choices"></Expander>

     

    You can assign plain text to this property, but again you have the option to, well, expand the Expander's Header to make it a little more graphically interesting.

     

         

      using this XAML:

          <Expander.Header>

              <Border BorderBrush="Navy" BorderThickness="2" CornerRadius="6">

                <TextBlock Margin="10,2,10,2"

                Foreground="Navy">What do you want to do?</TextBlock>

              </Border>

            </Expander.Header>

     

      Although Content is the default property, don't fall into the trap of trying to use it as:

       20  <Expander> Click to view choices </Expander>

      because that inserts the text as the first (and in this case, only) item in the area that you get to see when the Expander arrow is clicked. It will be the only item because Content isn't a container and you need to use something like a StackPanel if you have more than one child that you want to include in the expanded area.

      Something like this:

          <Expander.Content>

              <StackPanel >

                <CheckBox Margin="2,4" Content=" Do This"></CheckBox>

                <CheckBox Margin="2,4" Content=" Do That"></CheckBox>

                <CheckBox Margin="2,4" >

                  <CheckBox.Content>

                    <StackPanel Orientation="Horizontal">

                      <Image Width="16" Source="DCP_1997.jpg"></Image>

                      <TextBlock Text=" View Profile"></TextBlock>

                    </StackPanel>

                  </CheckBox.Content>

                </CheckBox>

                <TextBlock TextWrapping="Wrap" Margin="3,30,0,0"

              Text="Select a task from the list above." />

              </StackPanel>

            </Expander.Content>

     

      to produce this result:

     

      

    Posted Dec 08 2008, 08:02 AM by XTab with no comments
    Filed under: , ,
Copyright 1998-2009 vbCity.com LLC
Powered by Community Server (Non-Commercial Edition), by Telligent Systems