<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FormatException &#187; WPF</title>
	<atom:link href="http://www.formatexception.com/category/wpf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.formatexception.com</link>
	<description>Ramblings on developing in the Windows World</description>
	<lastBuildDate>Fri, 23 Sep 2011 15:34:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>WP7, ApplicationBar, ImageUri and the Icon not showing up</title>
		<link>http://www.formatexception.com/2010/11/wp7-applicationbar-imageuri-and-the-icon-not-showing-up/</link>
		<comments>http://www.formatexception.com/2010/11/wp7-applicationbar-imageuri-and-the-icon-not-showing-up/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 17:25:06 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[WP7]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.formatexception.com/?p=286</guid>
		<description><![CDATA[I know this may sound stupid but when working with the WP7 ApplicationBar make sure the images you are using have the following properties: Build Action: Content Copy to Output: Copy always (or &#8216;Copy if newer&#8217;, either should work) I spent a half an hour trying to figure out why my application bar was showing [...]]]></description>
			<content:encoded><![CDATA[<p>I know this may sound stupid but when working with the WP7 ApplicationBar make sure the images you are using have the following properties:</p>
<blockquote><p><a href="http://www.formatexception.com/wp-content/uploads/2010/11/Build_Action.png"><img src="http://www.formatexception.com/wp-content/uploads/2010/11/Build_Action-300x182.png" alt="Build Action: Content" title="Build_Action" width="300" height="182" class="alignnone size-medium wp-image-311" /></a><br/>Build Action: Content<br />
Copy to Output: Copy always (or &#8216;Copy if newer&#8217;, either should work)</p></blockquote>
<p>I spent a half an hour trying to figure out why my application bar was showing an &#8220;X&#8221; instead of the image I was putting in IconUri.  When first adding images the properties default to:</p>
<blockquote><p>Build Action: Resource<br />
Copy to Output: Do not copy</p></blockquote>
<p>Since the application bar is referencing the image by Uri it can&#8217;t seem to find it embedded in the application.</p>
<p>My xaml looks like:</p>
<pre name="code" class="xml">&lt;phone:PhoneApplicationPage.ApplicationBar&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;shell:ApplicationBarIconButton
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IconUri="/Images/appbar.feature.settings.rest.png"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text="Settings"/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/shell:ApplicationBar&gt;
&lt;/phone:PhoneApplicationPage.ApplicationBar&gt;</pre>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.formatexception.com%2F2010%2F11%2Fwp7-applicationbar-imageuri-and-the-icon-not-showing-up%2F&amp;t=WP7%2C%20ApplicationBar%2C%20ImageUri%20and%20the%20Icon%20not%20showing%20up" title="Facebook"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.formatexception.com%2F2010%2F11%2Fwp7-applicationbar-imageuri-and-the-icon-not-showing-up%2F&amp;t=WP7%2C%20ApplicationBar%2C%20ImageUri%20and%20the%20Icon%20not%20showing%20up" title="HackerNews"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.formatexception.com/2010/11/wp7-applicationbar-imageuri-and-the-icon-not-showing-up/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Databinding, ListView/GridView and Converter</title>
		<link>http://www.formatexception.com/2008/10/databinding-listviewgridview-and-converter/</link>
		<comments>http://www.formatexception.com/2008/10/databinding-listviewgridview-and-converter/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 14:56:23 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Databinding]]></category>
		<category><![CDATA[GridView]]></category>
		<category><![CDATA[IValueConverter]]></category>
		<category><![CDATA[ListView]]></category>

		<guid isPermaLink="false">http://www.formatexception.com/?p=106</guid>
		<description><![CDATA[Had some trouble with using a converter when databinding on a GridView yesterday so I thought I&#8217;d do a quick write up. Imagine for one instance you have the following class: public class EntityDataItem { public string Name { get; set; } public string Type { get; set; } public DateTime CreatedTS { get; set; [...]]]></description>
			<content:encoded><![CDATA[<p>Had some trouble with using a converter when databinding on a GridView yesterday so I thought I&#8217;d do a quick write up.</p>
<p>Imagine for one instance you have the following class:</p>
<pre name="code" class="csharp">public class EntityDataItem
{
    public string Name { get; set; }
    public string Type { get; set; }
    public DateTime CreatedTS { get; set; }
}</pre>
<p>Now imagine you have the following xaml:</p>
<pre name="code" class="csharp">&lt;ListView Name="EntitiesGrid"&gt;
&lt;ListView.View&gt;
&lt;GridView&gt;
&lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/&gt;
&lt;GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}"/&gt;
&lt;GridViewColumn Header="CreatedTS" DisplayMemberBinding="{Binding CreatedTS}"/&gt;
&lt;/GridView&gt;
&lt;/ListView.View&gt;
&lt;/ListView&gt;</pre>
<p>To bind to the grid all you would have to do on the window load event is:</p>
<pre name="code" class="csharp">private void Window_Loaded(object sender, RoutedEventArgs e)
{
    List&lt;EntityDataItem&gt; items = new List&lt;EntityDataItem&gt;();
    items.Add(new EntityDataItem { Name = "A", Type = "Person", CreatedTS = DateTime.Now });
    items.Add(new EntityDataItem { Name = "B", Type = "Equipment", CreatedTS = DateTime.Now });
    items.Add(new EntityDataItem { Name = "C", Type = "Location", CreatedTS = DateTime.Now });
    items.Add(new EntityDataItem { Name = "D", Type = "Unit", CreatedTS = DateTime.Now });
    EntitiesGrid.ItemsSource = items;
}</pre>
<p>Well now, that&#8217;s all fine and good.  But now imagine you were, oh, I don&#8217;t know, working on some military project that wanted you do use a specific format for the date.  The way it is now when the binding happens it will use the default ToString() which isn&#8217;t acceptable in this case.</p>
<p>So we need to use a converter.  Converters let you go from any object to any object on databinding.  Let&#8217;s start by creating the converter:</p>
<pre name="code" class="csharp">[ValueConversion(typeof(DateTime), typeof(String))]
public class DateToMilitaryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return "";
        DateTime date = (DateTime)value;
        return date.ToString("dd MMM yyyy HHmm", DateTimeFormatInfo.InvariantInfo);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = value.ToString();
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return value;
    }
}</pre>
<p>then in the resources for your xaml define:</p>
<pre name="code" class="csharp">&lt;local:DateToMilitaryConverter x:Key="dateConverter"/&gt;</pre>
<p>assuming the local namespace is defined as your local assembly or whatever namespace you used for the converter.</p>
<p>Here&#8217;s where things get a bit tricky.  You can&#8217;t put a converter directly on a GridViewColumn.  Where you can put it is on the Binding.  To do that make your xaml look like:</p>
<pre name="code" class="csharp">&lt;ListView Name="EntitiesGrid"&gt;
    &lt;ListView.View&gt;
        &lt;GridView&gt;
            &lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/&gt;
            &lt;GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}"/&gt;
            &lt;GridViewColumn Header="CreatedTS"&gt;
                &lt;GridViewColumn.DisplayMemberBinding&gt;
                    &lt;Binding Path="CreatedTS" Converter="{StaticResource dateConverter}"/&gt;
                &lt;/GridViewColumn.DisplayMemberBinding&gt;
            &lt;/GridViewColumn&gt;
        &lt;/GridView&gt;
    &lt;/ListView.View&gt;
&lt;/ListView&gt;</pre>
<p>You can see here we are tapping into the DisplayMemberBinding propery of the GridViewColumn and assigning a Binding to it.  On the Binding we can use a converter.  As I mentioned though you can go from any object to any other object.  Remember, however, regardless of what you go to, the ToString() will be called on it.  A couple of points of interest on the converter.  The source of the data in the real binding, not my contrived example, is a DateTime? which means the value could be null.  Thus I have accounted for that in the converter.  Additionally I allow for a class cast exception rather then doing:</p>
<pre name="code" class="csharp">DateTime date = value as DateTime;
if (value == null)
    return "";</pre>
<p> in case someone else were to use my class where it doesn&#8217;t belong.  Basically the converter handles DateTime? but nothing else, as it should be.</p>
<p>Well, I meant this one to be short and sweet and it was.</p>
<p>Later,<br />
Brian</p>
<p><b>UPDATE:</b><br />
It has been brought to my attention that you can do converters inline with the DisplayMemberBinding.</p>
<pre name="code" class="csharp">&lt;GridViewColumn
    Header="CreatedTS"
    DisplayMemberBinding="{Binding Path=CreatedTS, Converter={StaticResource dateConverter}}"
/&gt;</pre>
<p>Live and learn <img src='http://www.formatexception.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Brian</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F10%2Fdatabinding-listviewgridview-and-converter%2F&amp;t=Databinding%2C%20ListView%2FGridView%20and%20Converter" title="Facebook"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F10%2Fdatabinding-listviewgridview-and-converter%2F&amp;t=Databinding%2C%20ListView%2FGridView%20and%20Converter" title="HackerNews"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.formatexception.com/2008/10/databinding-listviewgridview-and-converter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WPF &#8211; ContentProperty, it&#8217;s that simple</title>
		<link>http://www.formatexception.com/2008/09/wpf-contentproperty-its-that-simple/</link>
		<comments>http://www.formatexception.com/2008/09/wpf-contentproperty-its-that-simple/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 15:06:42 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.formatexception.com/?p=32</guid>
		<description><![CDATA[So there I am, creating a custom control called &#8220;RequiredLabel&#8221; that interestingly enough is a label that has a cool icon in it to show that whatever it is being used for is required. The control has two properties, Text that is the text of the label and Style that is the style of the [...]]]></description>
			<content:encoded><![CDATA[<p>So there I am, creating a custom control called &#8220;RequiredLabel&#8221; that<br />
interestingly enough is a label that has a cool icon in it to show that<br />
whatever it is being used for is required. The control has two<br />
properties, Text that is the text of the label and Style that is the<br />
style of the label. Both are registered as dependency properties so<br />
that they can be set in the xaml. I suppose it would have been fine to<br />
simply leave good enough as good enough but I wanted it so that in the<br />
xaml you can put the text for the label between the start and end tag<br />
for required label. It is simply expected behavior the the content of<br />
your control can be put in that way.</p>
<p>After much soul searching (i.e. Googling) I still couldn&#8217;t find the<br />
answer. How do I make one of my dependency properties the content that<br />
is between the tags? Well, thankfully I had an epiphany and remembered<br />
that somewhere deep in one of my WPF books the answer was held. It is<br />
as simple as putting the attribute above the class declaration with the<br />
dependency property name.</p>
<p>[ContentProperty("Text")]<br />
public partial class RequiredLabel : UserControl<br />
{</p>
<p>This makes it so that when I use my custom control it will look like:</p>
<p>&lt;customControls:RequiredLabel x:Name=&#8221;lblType&#8221; Style=&#8221;{StaticResource<br />
LabelGradient}&#8221;&gt;<br />
                            Defendant&#8217;s Name:<br />
 &lt;/customControls:RequiredLabel&gt;</p>
<p>It also makes it so that if I decide to extend the content to support<br />
user controls instead of just text really easy since I can switch out<br />
the TextBlock that makes up the user control to a panel.</p>
<p>Also if you want to get a glimmer of using dependency properties see<br />
attached code.</p>
<p><a href="http://www.formatexception.com/post_attachments/contentproperty/RequiredLabel.xaml">RequiredLabel.xaml</a><br />
<a href="http://www.formatexception.com/post_attachments/contentproperty/RequiredLabel.xaml.cs">RequiredLabel.xaml.cs</a></p>
<p>Brian</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F09%2Fwpf-contentproperty-its-that-simple%2F&amp;t=WPF%20-%20ContentProperty%2C%20it%27s%20that%20simple" title="Facebook"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F09%2Fwpf-contentproperty-its-that-simple%2F&amp;t=WPF%20-%20ContentProperty%2C%20it%27s%20that%20simple" title="HackerNews"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.formatexception.com/2008/09/wpf-contentproperty-its-that-simple/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WPF and Silverlight</title>
		<link>http://www.formatexception.com/2008/09/wpf-and-silverlight/</link>
		<comments>http://www.formatexception.com/2008/09/wpf-and-silverlight/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 15:00:07 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.formatexception.com/?p=30</guid>
		<description><![CDATA[ Check out the link below.  It is a bunch of tutorials on everything you could want to know about Silverlight plus a whole bunch of cool WPF stuff. http://channel9.msdn.com/posts/Dan/Mike-Taulty-44-Silverlight-20-Screencasts/ Share and Enjoy:]]></description>
			<content:encoded><![CDATA[<p><!--StartFragment --> Check out the link below.  It is a bunch of tutorials on everything you could want to know about Silverlight plus a whole bunch of cool WPF stuff.</p>
<p><a class="moz-txt-link-freetext" href="http://channel9.msdn.com/posts/Dan/Mike-Taulty-44-Silverlight-20-Screencasts/">http://channel9.msdn.com/posts/Dan/Mike-Taulty-44-Silverlight-20-Screencasts/</a></p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F09%2Fwpf-and-silverlight%2F&amp;t=WPF%20and%20Silverlight" title="Facebook"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F09%2Fwpf-and-silverlight%2F&amp;t=WPF%20and%20Silverlight" title="HackerNews"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.formatexception.com/2008/09/wpf-and-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

