<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>WPF</title>
        <link>http://geekswithblogs.net/CodeCurve/category/8733.aspx</link>
        <description>All things WPF</description>
        <language>en-GB</language>
        <copyright>CodeCurve</copyright>
        <managingEditor>cjsmithie@googlemail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>UserControl in WPF considered harmful?</title>
            <link>http://geekswithblogs.net/CodeCurve/archive/2008/09/28/usercontrol-in-wpf-considered-harmful.aspx</link>
            <description>&lt;p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;I am currently working on a large app that is using WPF as its UI technology.  I’m on a small team of talented developers (i.e. I think my colleagues are talented!) and we came at WPF six months ago from scratch, although everyone apart from me had WinForms experience.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;As we built our app, we made frequent use of the WPF UserControl.  I think the main reasons for this were:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul type="disc"&gt;
    &lt;li class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;Visual Studio lists UserControl as an item and this encourages its use. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;It’s a convenient container for XAML that can be reused anywhere. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;It feels WinForms-ish and allows you to do whatever you like in the code behind &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;As we got further into the project, however, it soon became apparent that UserControl allows you to do things that don’t fit very well with “the way of WPF” and we have pretty much refactored out every UserControl in the app, in favour of DataTemplate, ContentPresenter and custom controls, e.g. sub-classes of ContentControl.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;This is very much inspired by the &lt;a href="http://www.pnpguidance.net/Post/Prism2Drop1CompositeWPFSilverlightApplicationGuidancePatternsPractices.aspx"&gt;&lt;span style="COLOR: blue"&gt;Prism&lt;/span&gt;&lt;/a&gt; project and the use of Model – View – ViewModel (see &lt;a href="http://blogs.msdn.com/dancre/archive/tags/DM-V-VM/default.aspx"&gt;&lt;span style="COLOR: blue"&gt;Dan Crevier’s blog&lt;/span&gt;&lt;/a&gt; for a good description of this), which we have found a very natural way of structuring the app.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;From my experience so far, I’d observe the following:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul type="disc"&gt;
    &lt;li class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;DataTemplate rocks as a way of representing data in the UI &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;If you need to wire things up, bindings and triggers are the first things to think about &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;Code behind comes relatively low down on the list.  Certainly, if a lot of behaviour is going in the code behind I would think quite hard about why. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;span style="FONT-SIZE: 12pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;I’m still in the early days of my journey with WPF, but I’m very impressed with DataTemplate and the use of M-V-VM.  I wanted to get my experiences with UserControl out there and see if anyone else has had similar experiences.  What are the legitimate uses of UserControl?  Any comments?&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/CodeCurve/aggbug/125505.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>CodeCurve</dc:creator>
            <guid>http://geekswithblogs.net/CodeCurve/archive/2008/09/28/usercontrol-in-wpf-considered-harmful.aspx</guid>
            <pubDate>Sun, 28 Sep 2008 20:14:33 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/CodeCurve/comments/125505.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/CodeCurve/archive/2008/09/28/usercontrol-in-wpf-considered-harmful.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/CodeCurve/comments/commentRss/125505.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Aligning text baseline in WPF</title>
            <link>http://geekswithblogs.net/CodeCurve/archive/2008/09/27/aligning-text-baseline-in-wpf.aspx</link>
            <description>&lt;p class="MsoNormal"&gt;I had a very frustrating experience last week trying to align the baselines of adjacent pieces of text in a WPF app.&lt;span&gt;  &lt;/span&gt;The problem seems to be that this is supported behaviour in a flow document, but not in general UI (e.g. the visual tree of a Window).&lt;/p&gt;
&lt;p class="MsoNormal"&gt;After a lot of fiddling, head scratching and googling we discovered that the easiest way to do this is to have two TextBlock elements inside an outer TextBlock.&lt;span&gt;  &lt;/span&gt;The following markup:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;img height="256" alt="" width="491" src="/images/geekswithblogs_net/CodeCurve/WPF_Baseline/XAML_markup.png" /&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;produces this end result:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;img height="70" alt="" width="154" src="/images/geekswithblogs_net/CodeCurve/WPF_Baseline/UI.png" /&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;The TextBlock has magically communicated with its TextBlock children and pulled them into line.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;However, we are using a Label next to a ContentPresenter to present arbitrary data with a DataTemplate and we couldn’t find a way to automatically line up the baseline of text in each control.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;This isn’t really surprising, since the ContentPresenter could present just about anything.&lt;span&gt;  &lt;/span&gt;It just so happens in our app that this is a line of text (with a different font size to the label next to it).&lt;/p&gt;
&lt;p class="MsoNormal"&gt;In the end, we had to resort to top aligning the Label (small font size) and the ContentPresenter (larger font size) and binding the Margin property on the Label to its FontSize property via a converter.&lt;span&gt;  &lt;/span&gt;This did the trick, but involved a hack because we couldn’t understand the Baseline property on the FontFamily class.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;It seems from the MSDN docs that the Baseline property on this class should be the proportion of a font’s height represented by the distance from the very top to the baseline of the text, i.e. it should be less than 1.&lt;span&gt;  &lt;/span&gt;However, for Verdana it is 1.005!&lt;/p&gt;
&lt;p class="MsoNormal"&gt;The code in our converter ended up looking like this and I’m a bit shamefaced to be publishing it!&lt;span&gt;  &lt;/span&gt;I know that we will have to revisit this in the future and I’m frustrated that we couldn’t find an elegant solution to the problem.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;img height="187" alt="" width="606" src="/images/geekswithblogs_net/CodeCurve/WPF_Baseline/Converter.png" /&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;v:shape id="Picture_x0020_7" style="VISIBILITY: visible; WIDTH: 451.5pt; HEIGHT: 139.5pt; mso-wrap-style: square" type="#_x0000_t75" o:spid="_x0000_i1025"&gt;&lt;v:imagedata o:title="" src="file:///C:\Users\Chris\AppData\Local\Temp\msohtmlclip1\01\clip_image005.png"&gt;&lt;/v:imagedata&gt;&lt;/v:shape&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;The 0.75 bit is particularly gratuitous.&lt;span&gt;  &lt;/span&gt;Can anyone shed any light on this?&lt;/p&gt; &lt;img src="http://geekswithblogs.net/CodeCurve/aggbug/125493.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>CodeCurve</dc:creator>
            <guid>http://geekswithblogs.net/CodeCurve/archive/2008/09/27/aligning-text-baseline-in-wpf.aspx</guid>
            <pubDate>Sat, 27 Sep 2008 17:19:20 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/CodeCurve/comments/125493.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/CodeCurve/archive/2008/09/27/aligning-text-baseline-in-wpf.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/CodeCurve/comments/commentRss/125493.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
