I have below xml file. I have copied it into my project debug/bin folder & have also attached to my project.
<?xml version="1.0" standalone="yes" ?>
- <NorthwindDataSet xmlns="http://tempuri.org/NorthwindDataSet.xsd">
- <Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
- <Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constitución 2222</Address>
<City>México D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customers>
</NorthwindDataSet>
I want to bind properties like CustomerName, City in my WPF application. I tried to bind it in XAML as below, but not getting success. Need suggetsion, what I am doing wrong.
<Window.Resources>
<XmlDataProvider x:Key="NorthData" Source="Northwind.xml" XPath="/Customers"/>
</Window.Resources>
<Grid>
<Label Content="{Binding XPath=Address,FallbackValue=BindingFailed,Source={StaticResource NorthData}}" Height="28" HorizontalAlignment="Left" Margin="118,94,0,0" Name="label1" VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData},XPath=City,FallbackValue=BindingFailed}" Height="100" HorizontalAlignment="Left" Margin="128,144,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>
Using your code I modified the XPath a little bit, and removed the reference to the XSD which you didn't provide, and it worked:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="NorthData" XPath="NorthwindDataSet/Customers">
<x:XData>
<NorthwindDataSet xmlns="">
<Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
<Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constitucion 2222</Address>
<City>Mexico D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customers>
</NorthwindDataSet>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<Label Content="{Binding XPath=Address
, FallbackValue=BindingFailed
, Source={StaticResource NorthData}}"
Height="28"
HorizontalAlignment="Left"
Margin="118,94,0,0" Name="label1"
VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData}
, XPath=City
, FallbackValue=BindingFailed}"
Height="100"
HorizontalAlignment="Left"
Margin="128,144,0,0"
Name="listBox1"
VerticalAlignment="Top"
Width="120" />
</Grid>
</Window>
You have to set the DataContext to your XML
<Window.Resources>
<XmlDataProvider x:Key="NorthData" Source="Northwind.xml" XPath="/Customers"/>
</Window.Resources>
<Grid DataContext="{StaticResource NorthData}">
<Label Content="{Binding XPath=Address,FallbackValue=BindingFailed,Source={StaticResource NorthData}}" Height="28" HorizontalAlignment="Left" Margin="118,94,0,0" Name="label1" VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData},XPath=City,FallbackValue=BindingFailed}" Height="100" HorizontalAlignment="Left" Margin="128,144,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>
I have an xml file as shown:
<?xml version="1.0" encoding="utf-8"?>
<PhoneBook xmlns="http://tempuri.org/PhoneBook.xsd">
<Person>
<PersonItem name="name1" famil="famil1">
<Numbers>
<NumbersItem number="123456" />
<NumbersItem number="789100" />
</Numbers>
</PersonItem>
</Person>
<Person>
<PersonItem name="name2" famil="famil2">
<Numbers>
<NumbersItem number="654321" />
<NumbersItem number="111213" />
</Numbers>
</PersonItem>
</Person>
</PhoneBook>
I have successfully bound a listbox to this XML using the following:
Imports <xmlns:DaftarTelephone="http://tempuri.org/PhoneBook.xsd">
Dim xmlPhoneBook = XDocument.Load(CurDir() & "\PhoneBook.xml")
Dim q = From el In xmlPhoneBook ...<PhoneBook:PersonItem>
lst.ItemsSource = q 'my listbox
Here's my XAML:
<Page.Resources>
<DataTemplate x:Key="PersonItemTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=Attribute[famil].Value}"/>
<TextBlock Text="{Binding Path=Attribute[name].Value}"/>
<TextBlock Text="{Binding Path=Attribute[number].Value}"/>
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid x:Name="LayoutRoot" >
<ListBox x:Name="lst" ItemsSource="{Binding}" ItemTemplate="{DynamicResource PersonItemTemplate}" />
</Grid>
family and name is shown correctly. But numbers are not shown!!!
I'm having some problems with data binding in XAML and WPF. Specifically, I'm trying to bind data from an XmlDataProvider to a ListBox.
The problem is this, when I'm in design mode in Visual Studio 2010, the xml items show up correctly, but when I run the app the listbox is just empty.
Here is what my xaml looks like. I'm not using any code behind, so this is all there is:
<Window x:Class="WpfTest9_Binding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="309" Width="622">
<Window.DataContext>
<XmlDataProvider XPath="Servers">
<x:XData>
<Servers>
<Server name="Server01" active="true" />
<Server name="Server02" active="false" />
<Server name="Testserver01" active="true" />
<Server name="Testserver02" active="true" />
</Servers>
</x:XData>
</XmlDataProvider>
</Window.DataContext>
<Grid>
<ListBox ItemsSource="{Binding XPath=*}" Margin="12">
<ListBox.ItemTemplate>
<DataTemplate>
<Border CornerRadius="5" Margin="5" BorderThickness="2" BorderBrush="#FFC14343">
<StackPanel Orientation="Horizontal" Margin="5">
<CheckBox IsChecked="{Binding XPath=#active}" />
<Label Content="{Binding XPath=#name}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Like I said above, the strange thing is that it looks like it's working while in design mode, but it fails to fill the listbox when I run the application. I'm not getting any error messages or warnings either.
What's wrong?
Ok, the solution was surprisingly simple.
As pointed out in this post, Listbox content not being populated with content from xml when using XmlDataProvider , all I had to do was to add an empty namespace attribute to the xml element. Like this:
<Servers xmlns="">
<Server name="Server01" active="true" />
<!-- ... -->
</Servers>
I have a UI in a window, which is bound to an XML file. I need to update the UI whenever I manually go and change my XML file.
Following is the WAML file:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="dataSource" Source="Data/Media.xml">
</XmlDataProvider>
</Window.Resources>
<Grid>
<ListBox Name="listBox1" DataContext="{Binding Source={StaticResource dataSource}, XPath=/Media/Book/#Title, Mode=OneWay}" />
</Grid>
</Window>
The following is the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Media>
<Book Author='John' Title='Fish are my aaaa friends' />
<Book Author='Dave' Title='Fish are my enemies' />
<Book Author='Jane' Title='Fish are my food' />
</Media>
I have a window object in the service layer. To initialise it, I did the following:
winobj.DataContext = node.OuterXml; winobj.Show(); where node is my XML string which I get from the server. If I ask you to update this winobj or reset its datacontext so that the UI is updated, how will you do it?
My UI is bound in the following way.
<Window.Resources>
<XmlDataProvider x:Key="Data" XPath="//WindowUpdate" />
</Window.Resources>
<Label Content="{Binding Source={StaticResource Data},
XPath=Window/Children/Label[#id\=\'lblInterval\']/#text}" ...
You could use a FileSystemWatcher to catch changes to the file, how you change the data is up to you (e.g. expose a bindable property and change that).
I am a little unclear on how I would set the SelectedValuePath of a TabControl to the text of the selected TabItems header. I feel like this should be fairly simple, and probably involves content.something but I have always been a bit confused about the Content property.
ItemTemplate — template for tab headers. Just add textblock with the same binding as in property SelectedValuePath.
<UserControl.Resources>
<XmlDataProvider x:Key="Employees" XPath="/Employees/*">
<x:XData>
<Employees xmlns="">
<Employee Name="Terry Adams" Type="FTE" EmployeeNumber="1" />
<Employee Name="Claire O'Donnell" Type="FTE" EmployeeNumber="12345" />
<Employee Name="Palle Peterson" Type="FTE" EmployeeNumber="5678" />
<Employee Name="Amy E. Alberts" Type="CSG" EmployeeNumber="99222" />
<Employee Name="Stefan Hesse" Type="Vendor" EmployeeNumber="-" />
</Employees>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="HeaderDataTemplate">
<TextBlock Text="{Binding XPath=#EmployeeNumber}" />
</DataTemplate>
<DataTemplate x:Key="ContentDataTemplate">
<TextBlock Text="{Binding XPath=#Name}" />
</DataTemplate>
</UserControl.Resources>
<TabControl ItemsSource="{Binding Source={StaticResource Employees}}"
ItemTemplate="{StaticResource HeaderDataTemplate}"
ContentTemplate="{StaticResource ContentDataTemplate}"
SelectedValue="12345"
SelectedValuePath="#EmployeeNumber"/>