<?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>Code Reference &#187; Dependency Properties</title>
	<atom:link href="http://sullivan.net/blog/tag/dependency-properties/feed/" rel="self" type="application/rss+xml" />
	<link>http://sullivan.net/blog</link>
	<description>A collection of code for my reference (and perhaps other people too)</description>
	<lastBuildDate>Wed, 25 Jan 2012 17:53:32 +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>Setting properties &#8211; in code vs. in XAML</title>
		<link>http://sullivan.net/blog/2009/08/setting-properties-in-code-vs-in-xaml/</link>
		<comments>http://sullivan.net/blog/2009/08/setting-properties-in-code-vs-in-xaml/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 10:36:10 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[Dependency Properties]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=107</guid>
		<description><![CDATA[Something you don't realize when first working with WPF is the different ways to set peroperties in code. There are times you want to set a property for an item - like a ListBox - and you don't see the property listed in the intellisense. Here is an exmaple of what I am talking about. [...]]]></description>
			<content:encoded><![CDATA[<p>Something you don't realize when first working with WPF is the different ways to set peroperties in code.<br />
There are times you want to set a property for an item - like a ListBox - and you don't see the property listed in the intellisense.</p>
<p>Here is an exmaple of what I am talking about.<br />
I want to turn off horizontal scrolling on a ListBox so the the content can become variable width and resize with the width of the app.</p>
<p>In XAML this is very easy to do:</p>
<pre class="brush: c#">
&lt;ListBox ScrollViewer.CanContentScroll=&quot;False&quot; ScrollViewer.HorizontalScrollBarVisibility=&quot;Disabled&quot;&gt;
        &lt;TextBlock TextWrapping=&quot;Wrap&quot; HorizontalAlignment=&quot;Left&quot;&gt;
             Lots and lots of text. Lots and lots of text. Lots and lots of text. Lots and lots of text. Lots and lots of text.
        &lt;/TextBlock&gt;
&lt;/ListBox&gt;
</pre>
<p>Here is how to do this in code:</p>
<pre class="brush: c#">
ListBox myList = new ListBox();

// &#039;Items&#039; property access in the standard way.
myList.Items.Clear();
myList.Items.Add(myItem);

// CanContentScroll is not a property of the ListBox. It is a property of the ScrollViewer in the ListBox&#039;s control template.
// These properties can only be set this way:
ScrollViewer.SetCanContentScroll(myList, false);
ScrollViewer.SetHorizontalScrollBarVisibility(myList, ScrollBarVisibility.Disabled);
</pre>
<p>As you can see, this way of setting the ScrollViewer properties is very logical when you see it. But without know it is done this way, you may not think of it.</p>
<p>This works because the ListBox is a dependency property.</p>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/08/setting-properties-in-code-vs-in-xaml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binding to a TextBox programmatically</title>
		<link>http://sullivan.net/blog/2009/08/binding-to-a-textbox-programmatically/</link>
		<comments>http://sullivan.net/blog/2009/08/binding-to-a-textbox-programmatically/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 12:28:28 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[Dependency Properties]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=65</guid>
		<description><![CDATA[We start with the text box in the XAML: &#60;TextBox x:Name=&#34;tbName&#34; Text=&#34;{Binding Name, UpdateSourceTrigger = PropertyChanged}&#34; /&#62; There are 2 important items here. The 1st is the Binding to the dependancy property declared in the code behind: 'Binding Name' The 2nd is the UpdateSourceTrigger. Setting it to 'PropertyChanged' triggers the binding whenever the content in [...]]]></description>
			<content:encoded><![CDATA[<p>We start with the text box in the XAML:</p>
<pre class="brush: c#">
&lt;TextBox x:Name=&quot;tbName&quot; Text=&quot;{Binding Name, UpdateSourceTrigger = PropertyChanged}&quot; /&gt;
</pre>
<p>There are 2 important items here.<br />
The 1st is the Binding to the dependancy property declared in the code behind: 'Binding Name'<br />
The 2nd is the UpdateSourceTrigger. Setting it to 'PropertyChanged' triggers the binding whenever the content in the box has been chagned.<br />
Without this, the binding is only updated on the LostFocus event. You would have to click on another form element or somehow change the focus for the changes in the text box to be noticed.<br />
A 3rd property (not shown here) is the Binding Mode. The default for a TextBox is 'TwoWay' so it was not necessary to add it. The other choices are 'OneWay', etc... </p>
<p>In the defination of the XAML file, the DataContext must be defined:</p>
<pre class="brush: c#">
DataContext=&quot;{Binding RelativeSource = {RelativeSource Self}}&quot;
</pre>
<p>Forget this and it will not know where to find the 'Name' property defined in the text box binding.</p>
<p>Define the DependencyProperty in the code behind page of the user control.</p>
<pre class="brush: c#">
public string Name
{
    get { return (string)GetValue(NameProperty); }
    set { SetValue(NameProperty, value); }
}

public static readonly DependencyProperty NameProperty = DependencyProperty.Register(&quot;Name&quot;, typeof (string),
        typeof (MyUserControl), new UIPropertyMetadata(string.Empty));
</pre>
<p>When you add 'MyUserControl' to a window or control programmatically, the binding must be spelled out like this:</p>
<pre class="brush: c#">
MyUserControl myUserControl = new MyUserControl();   // Create a new instance of the user control
Binding nameBinding= new Binding();  // Create a new binding
nameBinding.Source = personObject;  // The source of the the object itself, not the specific property of the object
nameBinding.Path = new PropertyPath(personObject.Name);    // The path is specifies what property in the object is bound
nameBinding.Mode = BindingMode.TwoWay;  // Again the mode is set to TwoWay
nameBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;  // Again the trigger is set to PropertyChange
myUserControl.SetBinding(MyUserControl.NameProperty, nameBinding);  // then the binding is set to the dependency property
</pre>
<p>You can see that several properties (Trigger and Mode) are set both here and in the XAML </p>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/08/binding-to-a-textbox-programmatically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binding and Dependency Properties</title>
		<link>http://sullivan.net/blog/2009/07/binding-and-dependency-properties/</link>
		<comments>http://sullivan.net/blog/2009/07/binding-and-dependency-properties/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 13:11:43 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[Dependency Properties]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=35</guid>
		<description><![CDATA[I found a great reference cheet for Binding over at ndb-tech. Thanks guys! The quick and dirty way to bind a property to a XAML item is to make that property a Dependency property. Then you can bind it to another property in the XAML. Here is an example o the XAML: &#60;Canvas &#62; &#60;TextBox [...]]]></description>
			<content:encoded><![CDATA[<p>I found a great reference cheet for Binding over at <a href="http://www.nbdtech.com/Free/WpfBinding.pdf">ndb-tech</a>. Thanks guys!</p>
<p>The quick and dirty way to bind a property to a XAML item is to make that property a Dependency property. Then you can bind it to another property in the XAML.</p>
<p>Here is an example o the XAML:</p>
<pre class="brush: c#">
&lt;Canvas &gt;
            &lt;TextBox Name=&quot;tb1&quot; Canvas.Top=&quot;{Binding ElementName=thisPopup, Path=CanvasTop}&quot; /&gt;
&lt;/Canvas&gt;
</pre>
<p>I found it odd that you have to name your UserControl for it to work. I am sure my syntax is a bit off and it is not necessary.</p>
<pre class="brush: c#">
&lt;UserControl x:Class=&quot;Sandbox.UserControl1&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    x:Name=&quot;myControl&quot;&gt;
</pre>
<p>The dependency property:</p>
<pre class="brush: c#">
public static DependencyProperty CanvasTopProperty = DependencyProperty.Register(&quot;CanvasTop&quot;, typeof(double), typeof(PopupControl));
public double CanvasTop
{
    get { return (double)GetValue(CanvasTopProperty); }
    set { SetValue(CanvasTopProperty, value); }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/07/binding-and-dependency-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

