The ItemsSource does *not* bind to the static resource "MyList", but to the data context.
I.e. it does not honor the "Source={StaticResource MyList}".
I'm getting the following error:
BindingExpression path error: 'Items' property not found on 'DataFormComboBox.MyData' 'DataFormComboBox.MyData' (HashCode=1723181). BindingExpression: Path='Items' DataItem='DataFormComboBox.MyData' (HashCode=1723181); target element is 'System.Windows.Controls.ComboBox' (Name=''); target property is 'ItemsSource' (type 'System.Collections.IEnumerable')..
Any clues?
I'm stuck here, since I don't know how to provide lookup-data to my ComboBoxes.
My scenario:
namespace DataFormComboBox { public class MyData { public string MyValue { get; set; } }
public class MyList { public MyList() { this.Items = new ObservableCollection<string>(); foreach (var item in new string[] { "One", "Two", "Three" }) this.Items.Add(item); }
public ObservableCollection<string> Items { get; private set; } }
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.LayoutRoot.Resources.Add("MyList", new MyList()); this.DataContext = new MyData(); } } }
Actually the above example scenario does not reflect my issue exactly,
since I learned that - for the above example - I just need to add the resource
prior to InitializeComponent() in order to make the ComboBox's ItemsSource bind correctly to my resource:
this.Resources.Add("MyList", new MyList()); InitializeComponent(); this.DataContext = new MyData();
It seems that the binding didn't find the resource and fell back to the data context.
Is this behaviour by design? I.e. silently performing a fall back although
the Source was explicitely set to a resource?
My actual scenario looks more like this:
namespace DataFormComboBox { public class MyData { public string MyValue { get; set; } }
public class MyList { public MyList() { this.Items = new ObservableCollection<string>();
foreach (var item in new string[] { "One", "Two", "Three" }) this.Items.Add(item); }
public ObservableCollection<string> Items { get; private set; } }
public partial class MainPage : UserControl { public MainPage() { this.Resources.Add("MyList", new MyList()); InitializeComponent(); this.DataContext = new MyData();
Casimodo72
Participant
1054 Points
464 Posts
DataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source
Aug 14, 2009 05:18 PM | LINK
I.e. it does not honor the "Source={StaticResource MyList}".
Regards,
Kasimier
Casimodo72
Participant
1054 Points
464 Posts
Re: DataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source
Aug 14, 2009 05:57 PM | LINK
Actually the above example scenario does not reflect my issue exactly,
since I learned that - for the above example - I just need to add the resource
prior to InitializeComponent() in order to make the ComboBox's ItemsSource bind correctly to my resource:
lee_sl
Contributor
4222 Points
864 Posts
Re: Re: DataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source
Aug 14, 2009 06:11 PM | LINK
I am not sure why you want to add it to Resources in codebehind and get it from resources.
you can add Contentloaded event handler and get the combobox and set the Itemssource directly to your list
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/
Casimodo72
Participant
1054 Points
464 Posts
Re: Re: DataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source
Aug 14, 2009 06:13 PM | LINK
Related posts (problems with StaticResource used in dynamically generated DataTemplates):
http://silverlight.net/forums/t/70078.aspx
http://silverlight.net/forums/t/62926.aspx
Casimodo72
Participant
1054 Points
464 Posts
Re: Re: DataForm - EditTemplate - ComboBox - ItemsSource Binding does not honor Source
Aug 14, 2009 07:05 PM | LINK
@lee_sl : Great, setting the ItemsSource in ContentLoaded of the DataForm works fine.
Thanks for the tip.
Actually I would love to be able to create the whole template
programmatically without resorting to the XamlReader thingy.
But that's not supported in Silverlight.
The workaround (using ContentLoaded) is still awkward:
1) I have to use the XamlReader for DataTemplate creation, but cannot use StaticResource in such XAMLs.
2) I need to post-process the elements in such DataTemplates when the templates are applied.
This means that setting e.g. Converters on Bindings needs also be done here.
This is currently not a problem for me, because I'm using a kind of descriptor/builder/manager
for DataTemplate generation and integration with the DataForm.
Something along the following lines:
This description is then applied to a DataForm by a manager.
But when someone actually writes DataTemplates in XAML, where's the fun then?
Thanks & regards,
Kasmier