Made a simple test project where i try to bind to a xmldatasource in a proto viewmodel
public partial class Window1 : Window
{
//private XmlDataProvider _provider = new XmlDataProvider();
private MyViewModel _myViewModel = new MyViewModel();
public Window1()
{
InitializeComponent();
this.DataContext = _myViewModel ;
}
}
public class MyViewModel
{
public MyViewModel()
{
LoadXMLData();
}
private XmlDataProvider _provider = new XmlDataProvider();
public XmlDataProvider Reports
{
get { return _provider; }
set { _provider = value; }
}
private void LoadXMLData()
{
string filePath = Directory.GetCurrentDirectory() + @"\Reports2.xml";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(filePath);
_provider.Document = doc;
_provider.XPath = @"Reports/Report";
}
}
If i try to bind a listbox like this. I get nothing
<ListBox x:Name="TeamsListBox" Margin="0,0,0,20" DockPanel.Dock="Left"
ItemsSource="{Binding Reports}"
ItemTemplate="{StaticResource teamItemTemplate}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single">
</ListBox>
If i instead change datacontext to
this.DataContext = _myViewModel.Reports
And listbox to
<ListBox x:Name="TeamsListBox" Margin="0,0,0,20" DockPanel.Dock="Left"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource teamItemTemplate}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single">
</ListBox>
Then it works, how do i bind to the viewmodel so i can fill it with more than just on xmldatasource
If i put a breakpoint on property Report i can see that it is called when i do {Binding Reports} but the list is still empty.
UPDATE
I can do this binding in code and then it works
Binding binding = new Binding();
binding.Source = _myViewModel.Reports;
binding.XPath = @"Reports/Report";
TeamsListBox.SetBinding(ListBox.ItemsSourceProperty, binding);
Why cant i do that in XAML