Silverlight Controls and Silverlight Toolkithttp://forums.silverlight.net//35.aspx/1?Silverlight+Controls+and+Silverlight+ToolkitDiscussions around using and developing Silverlight controls and the Silverlight ToolkitMon, 01 Jan 0001 00:00:00 -050035269544http://forums.silverlight.net//p/119528/269544.aspx/1?DataForm+EditTemplate+ComboBox+ItemsSource+Binding+does+not+honor+SourceDataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source <pre class="prettyprint">Hi, <br></pre><pre class="prettyprint">I'm currently trying to generate data templates for the DataForm programmatically using the XamlReader.</pre><pre class="prettyprint">In conjunction with ComboBoxes I stumbled upon the following issue:</pre><pre class="prettyprint"><pre class="prettyprint">&lt;<span class="tag">ComboBox</span><br><span class="attr"> SelectedItem=</span><span class="attrv">"{Binding MyValue, Mode=TwoWay}"</span><br><span class="attr"> ItemsSource=</span><span class="attrv">"{Binding Items, Source={StaticResource MyList}}"</span> /&gt;</pre></pre><pre class="prettyprint">The ItemsSource does *not* bind to the static resource "MyList", but to the data context.</pre><pre class="prettyprint">I.e. it does not honor the "Source={StaticResource MyList}".<br></pre><pre class="prettyprint">I'm getting the following error: <br></pre><pre class="prettyprint">BindingExpression path error: 'Items' property not found on<br>'DataFormComboBox.MyData' 'DataFormComboBox.MyData' (HashCode=1723181).<br>BindingExpression: Path='Items' DataItem='DataFormComboBox.MyData'<br>(HashCode=1723181); target element is<br>'System.Windows.Controls.ComboBox' (Name=''); target property is<br>'ItemsSource' (type 'System.Collections.IEnumerable').. <br></pre><pre class="prettyprint">&nbsp;</pre><pre class="prettyprint">Any clues?</pre><pre class="prettyprint">I'm stuck here, since I don't know how to provide lookup-data to my ComboBoxes. <br></pre><pre class="prettyprint"><br></pre><pre class="prettyprint">My scenario: <br></pre><pre class="prettyprint"><span class="kwd">namespace</span> DataFormComboBox<br>{<br> <span class="kwd">public class</span> MyData<br> {<br> <span class="kwd">public string</span> MyValue { <span class="kwd">get</span>; <span class="kwd">set</span>; }<br> }<br><br> <span class="kwd">public class</span> MyList<br> {<br> <span class="kwd">public</span> MyList()<br> {<br> <span class="kwd">this</span>.Items = <span class="kwd">new</span> ObservableCollection&lt;<span class="kwd">string</span>&gt;();<br> <span class="kwd">foreach</span> (var item <span class="kwd">in new string</span>[] { <span class="st">"One"</span>, <span class="st">"Two"</span>, <span class="st">"Three"</span> })<br> <span class="kwd">this</span>.Items.Add(item);<br> }<br><br> <span class="kwd">public</span> ObservableCollection&lt;<span class="kwd">string</span>&gt; Items { <span class="kwd">get</span>; <span class="kwd">private set</span>; }<br> }<br><br> <span class="kwd">public</span> partial <span class="kwd">class</span> MainPage : UserControl<br> {<br> <span class="kwd">public</span> MainPage()<br> {<br> InitializeComponent();<br> <span class="kwd">this</span>.LayoutRoot.Resources.Add(<span class="st">"MyList"</span>, <span class="kwd">new</span> MyList());<br> <span class="kwd">this</span>.DataContext = <span class="kwd">new</span> MyData();<br> }<br> }<br>}</pre>&nbsp;<pre class="prettyprint">&lt;<span class="tag">UserControl</span><br><span class="attr"> x:Class=</span><span class="attrv">"DataFormComboBox.MainPage"</span><br><span class="attr"> xmlns=</span><span class="attrv">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><br><span class="attr"> xmlns:x=</span><span class="attrv">"http://schemas.microsoft.com/winfx/2006/xaml"</span><br><span class="attr"> xmlns:d=</span><span class="attrv">"http://schemas.microsoft.com/expression/blend/2008"</span><br><span class="attr"> xmlns:mc=</span><span class="attrv">"http://schemas.openxmlformats.org/markup-compatibility/2006"</span><br><span class="attr"> xmlns:df=</span><span class="attrv">"clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"</span><br><span class="attr"> mc:Ignorable=</span><span class="attrv">"d"</span><br><span class="attr"> d:DesignWidth=</span><span class="attrv">"640"</span><br><span class="attr"> d:DesignHeight=</span><span class="attrv">"480"</span>&gt;<br> &lt;<span class="tag">Grid</span><br><span class="attr"> x:Name=</span><span class="attrv">"LayoutRoot"</span>&gt;<br> &lt;<span class="tag">df:DataForm</span> <br><span class="attr"> AutoGenerateFields=</span><span class="attrv">"False"</span><br><span class="attr"> AutoEdit=</span><span class="attrv">"True"</span><br><span class="attr"> CurrentItem=</span><span class="attrv">"{Binding}"</span>&gt;<br> &lt;<span class="tag">df:DataForm.EditTemplate</span>&gt;<br> &lt;<span class="tag">DataTemplate</span>&gt;<br> &lt;<span class="tag">StackPanel</span>&gt;<br> &lt;<span class="tag">df:DataField</span>&gt;<br> &lt;<span class="tag">ComboBox</span><br><span class="attr"> SelectedItem=</span><span class="attrv">"{Binding MyValue, Mode=TwoWay}"</span><br><span class="attr"> ItemsSource=</span><span class="attrv">"{Binding Items, Source={StaticResource MyList}}"</span> /&gt;<br> &lt;/<span class="tag">df:DataField</span>&gt;<br> &lt;/<span class="tag">StackPanel</span>&gt;<br> &lt;/<span class="tag">DataTemplate</span>&gt;<br> &lt;/<span class="tag">df:DataForm.EditTemplate</span>&gt;<br> &lt;/<span class="tag">df:DataForm</span>&gt;<br> &lt;/<span class="tag">Grid</span>&gt;<br>&lt;/<span class="tag">UserControl</span>&gt;</pre> <p>&nbsp;</p> <p>Regards,</p> <p>Kasimier <br> </p> 2009-08-14T17:18:58-04:00269558http://forums.silverlight.net//p/119528/269558.aspx/1?Re+DataForm+EditTemplate+ComboBox+ItemsSource+Binding+does+not+honor+SourceRe: DataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source <p>Actually the above example scenario does not reflect my issue exactly,</p> <p>since I learned that - for the above example - I just need to add the resource</p> <p>prior to InitializeComponent() in order to make the ComboBox's ItemsSource bind correctly to my resource:<br> </p> <pre class="prettyprint"><span class="kwd">this</span>.Resources.Add(<span class="st">"MyList"</span>, <span class="kwd">new</span> MyList());<br>InitializeComponent();<br><span class="kwd">this</span>.DataContext = <span class="kwd">new</span> MyData();</pre><pre class="prettyprint">It seems that the binding didn't find the resource and fell back to the data context.</pre><pre class="prettyprint">Is this behaviour by design? I.e. silently performing a fall back although</pre><pre class="prettyprint">the Source was explicitely set to a resource?</pre><pre class="prettyprint">&nbsp;</pre><pre class="prettyprint">My actual scenario looks more like this:</pre><pre class="prettyprint"><pre class="prettyprint"><span class="kwd">namespace</span> DataFormComboBox<br>{<br> <span class="kwd">public class</span> MyData<br> {<br> <span class="kwd">public string</span> MyValue { <span class="kwd">get</span>; <span class="kwd">set</span>; }<br> }<br><br> <span class="kwd">public class</span> MyList<br> {<br> <span class="kwd">public</span> MyList()<br> {<br> <span class="kwd">this</span>.Items = <span class="kwd">new</span> ObservableCollection&lt;<span class="kwd">string</span>&gt;();<br><br> <span class="kwd">foreach</span> (var item <span class="kwd">in new string</span>[] { <span class="st">"One"</span>, <span class="st">"Two"</span>, <span class="st">"Three"</span> })<br> <span class="kwd">this</span>.Items.Add(item);<br> }<br><br> <span class="kwd">public</span> ObservableCollection&lt;<span class="kwd">string</span>&gt; Items { <span class="kwd">get</span>; <span class="kwd">private set</span>; }<br> }<br><br> <span class="kwd">public</span> partial <span class="kwd">class</span> MainPage : UserControl<br> {<br> <span class="kwd">public</span> MainPage()<br> {<br> <span class="kwd">this</span>.Resources.Add(<span class="st">"MyList"</span>, <span class="kwd">new</span> MyList());<br> InitializeComponent();<br> <span class="kwd">this</span>.DataContext = <span class="kwd">new</span> MyData();<br><br> MyDataForm.EditTemplate = BuildTemplate();<br> MyDataForm.ApplyTemplate();<br> }<br><br> DataTemplate BuildTemplate()<br> {<br> <span class="cmt">// This would be the generated XAML.</span><br> <span class="kwd">string</span> xaml = @<span class="st">"&amp;lt;DataTemplate<br> xmlns='http:<span class="cmt">//schemas.microsoft.com/winfx/2006/xaml/presentation'</span><br> xmlns:x='http:<span class="cmt">//schemas.microsoft.com/winfx/2006/xaml'</span><br> xmlns:df='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit'&gt;<br> &lt;StackPanel&gt;<br> &lt;df:DataField&gt;<br> &lt;ComboBox<br> SelectedItem='{ Binding MyValue, Mode=TwoWay }'<br> ItemsSource='{ Binding Items, Source={StaticResource MyList } }' /&gt;<br> &lt;/df:DataField&gt;<br> &lt;/StackPanel&gt;<br> &lt;/DataTemplate&gt;"</span>;<br><br> DataTemplate template = (DataTemplate)XamlReader.Load(xaml);<br><br> <span class="kwd">return</span> template;<br> }<br> }<br>}</pre></pre><pre class="prettyprint">&nbsp;&nbsp;<pre class="prettyprint">&lt;<span class="tag">UserControl</span><br><span class="attr"> x:Class=</span><span class="attrv">"DataFormComboBox.MainPage"</span><br><span class="attr"> xmlns=</span><span class="attrv">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><br><span class="attr"> xmlns:x=</span><span class="attrv">"http://schemas.microsoft.com/winfx/2006/xaml"</span><br><span class="attr"> xmlns:d=</span><span class="attrv">"http://schemas.microsoft.com/expression/blend/2008"</span><br><span class="attr"> xmlns:mc=</span><span class="attrv">"http://schemas.openxmlformats.org/markup-compatibility/2006"</span><br><span class="attr"> xmlns:df=</span><span class="attrv">"clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"</span> <br><span class="attr"> mc:Ignorable=</span><span class="attrv">"d"</span><br><span class="attr"> d:DesignWidth=</span><span class="attrv">"640"</span><br><span class="attr"> d:DesignHeight=</span><span class="attrv">"480"</span>&gt;<br> &lt;<span class="tag">Grid</span><br><span class="attr"> x:Name=</span><span class="attrv">"LayoutRoot"</span>&gt; <br> &lt;<span class="tag">df:DataForm</span> <br><span class="attr"> x:Name=</span><span class="attrv">"MyDataForm"</span><br><span class="attr"> AutoGenerateFields=</span><span class="attrv">"False"</span><br><span class="attr"> AutoEdit=</span><span class="attrv">"True"</span><br><span class="attr"> CurrentItem=</span><span class="attrv">"{Binding}"</span>&gt;<br> &lt;/<span class="tag">df:DataForm</span>&gt;<br> &lt;/<span class="tag">Grid</span>&gt;<br>&lt;/<span class="tag">UserControl</span>&gt;</pre></pre><pre class="prettyprint">&nbsp;</pre><pre class="prettyprint">Does this mean that when loading the XAML with XamlReader</pre><pre class="prettyprint">the binding to the resource is discarded somehow, since there are no resources in</pre><pre class="prettyprint">scope at that time? Somehow I expected such references to be resolved when</pre><pre class="prettyprint">the template is added to the visual tree, i.e. when performing the following:</pre><pre class="prettyprint"><pre class="prettyprint">MyDataForm.EditTemplate = BuildTemplate(); MyDataForm.ApplyTemplate();</pre></pre><pre class="prettyprint">Any clues? <br></pre><pre class="prettyprint">Regards,</pre><pre class="prettyprint">Kasimier <br></pre><pre class="prettyprint">&nbsp;</pre> 2009-08-14T17:57:56-04:00269575http://forums.silverlight.net//p/119528/269575.aspx/1?Re+Re+DataForm+EditTemplate+ComboBox+ItemsSource+Binding+does+not+honor+SourceRe: Re: DataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source <p>I am not sure why you want to add it to Resources in codebehind and get it from resources.</p> <p>you can add Contentloaded event handler and get the combobox&nbsp; and set the Itemssource directly to your list</p> 2009-08-14T18:11:58-04:00269579http://forums.silverlight.net//p/119528/269579.aspx/1?Re+Re+DataForm+EditTemplate+ComboBox+ItemsSource+Binding+does+not+honor+SourceRe: Re: DataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source <p>Related posts (problems with StaticResource used in dynamically generated DataTemplates):<br> </p> <p>http://silverlight.net/forums/t/70078.aspx <br> </p> <p>http://silverlight.net/forums/t/62926.aspx <br> </p> 2009-08-14T18:13:46-04:00269603http://forums.silverlight.net//p/119528/269603.aspx/1?Re+Re+DataForm+EditTemplate+ComboBox+ItemsSource+Binding+does+not+honor+SourceRe: Re: DataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source <p>&nbsp;@lee_sl : Great, setting the ItemsSource in ContentLoaded of the DataForm works fine.</p> <p>Thanks for the tip. <br> </p> <p>&nbsp;</p> <p>Actually I would love to be able to create the whole template</p> <p>programmatically without resorting to the XamlReader thingy.</p> <p>But that's not supported in Silverlight.</p> <p>&nbsp;</p> <p>The workaround (using ContentLoaded) is still awkward:</p> <p>1) I have to use the XamlReader for DataTemplate creation, but cannot use StaticResource in such XAMLs.<br> </p> <p>2) I need to post-process the elements in such DataTemplates when the templates are applied.</p> <p>&nbsp;&nbsp; This means that setting e.g. Converters on Bindings needs also be done here.<br> </p> <p>&nbsp;</p> <p>This is currently not a problem for me, because I'm using a kind of descriptor/builder/manager</p> <p>for DataTemplate generation and integration with the DataForm.</p> <p>Something along the following lines: <br> </p> <pre class="prettyprint">MembershipServiceUser user = MembershipServiceUser.Create();<br><br>LookupDataSource sourceA =<br> <span class="kwd">new</span> LookupDataSource(<br> () =&gt; <span class="kwd">new string</span>[] { <span class="st">"Kasimier"</span>, <span class="st">"Uwe"</span>, <span class="st">"Christa"</span> }<br> );<br><br>LookupDataSource sourceB =<br> <span class="kwd">new</span> LookupDataSource(<br> () =&gt; <span class="kwd">new string</span>[] { <span class="st">"me@casimodo.net"</span>, <span class="st">"you@casimodo.net"</span>, <span class="st">"her@casimodo.net"</span> }<br> );<br><br>EntityPresentationDescriptor&lt;MembershipServiceUser&gt; descriptor =<br> new EntityPresentationDescriptor&lt;MembershipServiceUser&gt;();</pre><pre class="prettyprint"><membershipserviceuser><membershipserviceuser><br>descriptor.AddLookupSource(sourceA);<br>descriptor.AddLookupSource(sourceB);<br><br>descriptor<br> .AddProperty(o =&gt; o.UserName, sourceA)<br> .AddProperty(o =&gt; o.Email, sourceB);</membershipserviceuser></membershipserviceuser></pre> <p>This description is then applied to a DataForm by a manager.</p> <p>&nbsp;</p> <p>But when someone actually writes DataTemplates in XAML, where's the fun then?<br> </p> <p><br> </p> <p>Thanks &amp; regards,</p> <p>Kasmier <br> </p> <p>&nbsp;</p> 2009-08-14T19:05:40-04:00