-
August 13th, 2011, 10:16 AM
#1
Binding from a XML file to UserControl through DependencyProperty
Hello everyone,
I am making a sample application test with WPF creating some UserControls to increase maintainability.
In this way I created a toolbar usercontrol where I put a combobox and I created a dependency property, since it's the best tool for binding than datacontext when having more than one binding, and in main xaml I called a xmldataprovider loading a xml file to fill the combo box in the UserControl.
in the toolbar's usercontrol xaml file I put
<pre><ComboBox ItemsSource="{Binding Path=epToolbarItemSource, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" DisplayMemberPath="shortname" /></pre>
In the toolbar's usercontrol xaml.cs file I declared the dependency property as:
<pre>public static DependencyProperty ItemSourceToolbarProperty;
static epToolbar()
{
epToolbar.ItemSourceToolbarProperty = DependencyProperty.Register("epToolbarItemSource", typeof(IEnumerable),
typeof(epToolbar));
}
public IEnumerable ItemSourceToolbar
{
get { return (IEnumerable)GetValue(epToolbar.ItemSourceToolbarProperty); }
set { SetValue(epToolbar.ItemSourceToolbarProperty, value); }
}
</pre>
In the Window.xaml file I declared the xmldataprovider as:
<pre><XmlDataProvider x:Key="dpOperators" XPath="operators" Source="/Data/Operators.xml" IsInitialLoadEnabled="True" /></pre>
and I declared the usercontrol setting the dependency property I thought it was correctly declared, epToolbarItemSource:
<pre><uc:epToolbar DockPanel.Dock="Top" epToolbarItemSource="{Binding Source=dpOperators, Mode=TwoWay}" /></pre>
In the examples found about Dependency Properties I always read the dependency property can be called as a property attribute when calling the usercontrol but in this case it says an error like: "The property 'epToolbarItemSource' was not found in type 'epToolbar'."
So I am asking here what's the right syntax to bind the content of xml file to the combo in the way I add new operators in the xmldataprovider so they would be inserted in xml file and refreshed the combo box?
Thanks in advance to all
Cheers, Luigi
Last edited by npuleio; August 14th, 2011 at 11:45 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|