<?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>WPF &#187; ElementName</title>
	<atom:link href="http://www.wpfsharp.com/tag/elementname/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wpfsharp.com</link>
	<description>WPF and C#</description>
	<lastBuildDate>Fri, 17 May 2013 03:36:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Tutorial &#8211; Binding one element property to another</title>
		<link>http://www.wpfsharp.com/2010/04/13/tutorial-binding-one-element-property-to-another/</link>
		<comments>http://www.wpfsharp.com/2010/04/13/tutorial-binding-one-element-property-to-another/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 05:09:12 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[C# (C-Sharp)]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[ElementName]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://rhyous.com/?p=1834</guid>
		<description><![CDATA[The properties of WPF elements can be bound to properties of other WPF Elements. Lets do some simple examples of binding one element to another. For this tutorial, I assume you are in Visual Studio 2008.  I assume that you already know how to create a new Project and choose WPF Application.  All examples assume [...]]]></description>
				<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.wpfsharp.com%2F2010%2F04%2F13%2Ftutorial-binding-one-element-property-to-another%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://www.wpfsharp.com/2010/04/13/tutorial-binding-one-element-property-to-another/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.wpfsharp.com/2010/04/13/tutorial-binding-one-element-property-to-another/"  data-text="Tutorial &#8211; Binding one element property to another" data-count="horizontal"></a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://www.wpfsharp.com/2010/04/13/tutorial-binding-one-element-property-to-another/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://www.wpfsharp.com/2010/04/13/tutorial-binding-one-element-property-to-another/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>The properties of WPF elements can be bound to properties of other WPF Elements. Lets do some simple examples of binding one element to another.</p>
<p>For this tutorial, I assume you are in Visual Studio 2008.  I assume that you already know how to create a new Project and choose WPF Application.  All examples assume you have a new WPF Application.</p>
<p>I am the believer that one example isn&#8217;t enough, so I am going to give you three examples:</p>
<h1 style="font-size: 16px;">Example 1 &#8211; Binding and Element&#8217;s property to CheckBox.IsChecked</h1>
<p>This example will demonstrate binding a <code>Button</code>&#8216;s <code>IsEnabled</code> property to a <code>CheckBox</code>&#8216;s <code>IsChecked</code> property.</p>
<h2 style="font-size: 15px;">Step 1 &#8211; Add the elements</h2>
<ol>
<li>Add two items from the Toolbox:
<ul>
<li><code>CheckBox</code></li>
<li><code>Button</code></li>
</ul>
<p>The <code>Button</code> is named <code>button1</code> and the <code>CheckBox</code> is named <code>checkBox1.</code></li>
<li>Change the text of the <code>checkBox1</code> to &#8220;Enable button&#8221;.  This can be done either in the Properties or in the XAML.</li>
</ol>
<h2 style="font-size: 15px;">Step 2 &#8211; Adding Binding to the Button</h2>
<ol>
<li>In the XAML, locate the <code>button1</code> element.</li>
<li>Add the following to the <code>button1</code> element:<code>IsEnabled="{Binding ElementName=checkBox1, Path=IsChecked}"</code>In your project, <code>ElementName</code> could be any item. In this example, we only have two elements so far: <code>button1</code>, and <code>checkBox1</code>.The XAML now looks like this (only two new lines exist):
<pre class="brush: xml; title: ; notranslate">
&lt;Window x:Class=&quot;BindingATextBoxToASlider.Window1&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    Title=&quot;Window1&quot; Height=&quot;300&quot; Width=&quot;300&quot;&gt;
    &lt;Grid&gt;
        &lt;Button Content=&quot;Button&quot; Height=&quot;23&quot; Margin=&quot;12,34,416,0&quot; Name=&quot;button1&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;75&quot; IsEnabled=&quot;{Binding ElementName=checkBox1, Path=IsChecked}&quot;/&gt;
        &lt;CheckBox Content=&quot;CheckBox&quot; Height=&quot;16&quot; Margin=&quot;12,12,408,0&quot; Name=&quot;checkBox1&quot; VerticalAlignment=&quot;Top&quot; /&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;
</pre>
</li>
<li>Compile and run your program.</li>
<li>Check and uncheck the box an watch the binding do its work as it enables and disables the button.</li>
</ol>
<p>Ok, so that was pretty cool.  We have a simple example of binding one Element to another.</p>
<h2 style="font-size: 15px;">You can shoot yourself in the foot or Don&#8217;t be stupid!</h2>
<p>Yes, you can shoot yourself in the foot by doing something stupid.</p>
<p>You could bind an element to itself. Let&#8217;s try it just so you can see it happen.</p>
<p>Add the same binding you added to <code>button1</code> to <code>checkBox1</code>.</p>
<p>Compile and see what happens.</p>
<h1 style="font-size: 16px;">Example 2 &#8211; Binding and Element&#8217;s property to Slider.Value</h1>
<p>This example uses a <code>Slider</code> and a <code>TextBox</code>.</p>
<h2 style="font-size: 15px;">Step 1 &#8211; Add the elements</h2>
<ol>
<li>Add two items from the Toolbox:
<ul>
<li><code>TextBox</code></li>
<li><code>Slider</code></li>
</ul>
<p>The <code>Slider</code> is named <code>slider1</code> and the <code>TextBox</code> is named <code>textBox1.</code></li>
</ol>
<h2 style="font-size: 15px;">Step 2 &#8211; Adding Binding to the TextBox</h2>
<ol>
<li>In the XAML, locate the <code>textBox1</code> element.</li>
<li>Add the following to the <code>textBox1</code> element:<code>Text="{Binding ElementName=slider1, Path=Value}"</code><code>ElementName</code> can be any item. In this example, we only have two elements so far: <code>slider1</code>, and <code>textBox1</code>.The XAML now looks like this (only two new lines exist):
<pre class="brush: xml; title: ; notranslate">
&lt;Window x:Class=&quot;BindingATextBoxToASlider.Window1&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    Title=&quot;Window1&quot; Height=&quot;300&quot; Width=&quot;300&quot;&gt;
    &lt;Grid&gt;
       &lt;TextBox Height=&quot;23&quot; Margin=&quot;79,62,99,0&quot; Name=&quot;textBox1&quot; VerticalAlignment=&quot;Top&quot; Text=&quot;{Binding ElementName=slider1, Path=Value}&quot;/&gt;
        &lt;Slider Height=&quot;22&quot; Margin=&quot;79,34,99,0&quot; Name=&quot;slider1&quot; VerticalAlignment=&quot;Top&quot; /&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;
</pre>
</li>
<li>Compile and run your program.</li>
<li>Slide the slider and watch the binding do its work as its value is displayed in the textBox1 as it changes.</li>
</ol>
<h2 style="font-size: 15px;">Calculations in XAML Bindings are Unsupported</h2>
<p>Ok, so maybe you want to try to do calculations in the XAML Binding.  It doesn&#8217;t work.</p>
<p>You can enter this and while it will compile, the Binding won&#8217;t work:</p>
<p><code>Text="{Binding ElementName=slider1, Path=(int)Value}"</code></p>
<p>You can enter this and while it will compile, the Binding won&#8217;t work:</p>
<p><code>Text="{Binding ElementName=slider1, Path=Value + 1}"</code></p>
<h1 style="font-size: 16px;">Example 3 &#8211; Binding and Element&#8217;s property to CheckBox.IsChecked</h1>
<p>Ok, lets do a slight more complex example.  We are going to have more than two elements.  We are going to have a <code>ListBox</code> that contains a list of items (<code>ListBoxItems</code>).  We are going to have a <code>TextBox</code> that displays the content of the selected item.</p>
<h2 style="font-size: 15px;">Step 1 &#8211; Add the elements</h2>
<ol>
<li>Add two items from the Toolbox:
<ul>
<li><code>TextBox</code></li>
<li><code>ListBox</code></li>
</ul>
</li>
<li>Add multiple items to <code>listBox1</code>. This can be done either in the XAML or by clicking on the button for <code>Items</code> in the Properties of the <code>listBox1</code>.</li>
</ol>
<h2 style="font-size: 15px;">Step 2 &#8211; Adding Binding to the TextBox</h2>
<ol>
<li>In the XAML, locate the <code>textBox1</code> element.</li>
<li>Add the following to the <code>textBox1</code> element:<code>Text="{Binding ElementName=listBox1, Path=SelectedItem.Content}"</code>Notice that we are using a property of a property for the Path.  This is allowed.  <code>SelectedItem</code> is a property of <code>listBox1</code>, and <code>Content</code> is a property of <code>SelectedItem</code>.The XAML now looks like this (only two new lines exist):
<pre class="brush: xml; title: ; notranslate">
&lt;Window x:Class=&quot;BindingATextBoxToAListBoxSelectedItem.Window1&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    Title=&quot;Window1&quot; Height=&quot;300&quot; Width=&quot;300&quot;&gt;
    &lt;Grid&gt;
        &lt;TextBox Height=&quot;23&quot; Margin=&quot;12,23,12,0&quot; Name=&quot;textBox1&quot; VerticalAlignment=&quot;Top&quot; Text=&quot;{Binding ElementName=listBox1, Path=SelectedItem.Content}&quot;/&gt;
        &lt;ListBox Margin=&quot;12,52,12,110&quot; Name=&quot;listBox1&quot;&gt;
            &lt;ListBoxItem&gt;c:&lt;/ListBoxItem&gt;
            &lt;ListBoxItem&gt;d:&lt;/ListBoxItem&gt;
            &lt;ListBoxItem&gt;e:&lt;/ListBoxItem&gt;
            &lt;ListBoxItem&gt;f:&lt;/ListBoxItem&gt;
            &lt;ListBoxItem&gt;g:&lt;/ListBoxItem&gt;
            &lt;ListBoxItem&gt;h:&lt;/ListBoxItem&gt;
        &lt;/ListBox&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;
</pre>
</li>
<li>Compile and run your program.</li>
<li>Select different items in the list and watch the textBox1 change to display the content of the selected item.</li>
</ol>
<hr />Copyright ® Rhyous.com &#8211; Linking to this post is allowed without permission and as many as ten lines of this page can be used along with this link.  Any other use of this page is allowed only by permission of Rhyous.com.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wpfsharp.com/2010/04/13/tutorial-binding-one-element-property-to-another/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
