I have an XML like this ::
<?xml version="1.0" encoding="utf-8" ?>
<Rows>
<Row Id="1">
<Devices>
<Device DeviceId="123">Device 1</Device>
<Device DeviceId="abcd" >Device 2</Device>
</Devices>
<Methods>
<Method>Method 1</Method>
<Method>Method 2</Method>
</Methods>
</Row>
<Row Id="2">
<Devices>
<Device>Device 1</Device>
<Device>Device 2</Device>
</Devices>
<Methods>
<Method>Method 1</Method>
<Method>Method 2</Method>
</Methods>
</Row>
</Rows>
I have to DataBind the above XML to a DataGrid.
My DataGrid is as follows:
<wpfkit:DataGrid AutoGenerateColumns="False" DataContext="{Binding Grid}"
ItemsSource="{Binding Path=Elements[Row]}"
Width="Auto"
FrozenColumnCount="2"
SelectionMode="Extended"
CanUserAddRows="False"
x:Name="CommonPEGrid"
Loaded="CommonPEGrid_Loaded">
</wpfkit:DataGrid>
My code where I am associating DataContext is as follows:: I have created some template columns in code behind and associated DataTemplate with them.
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
Grid = XElement.Load("PE.xml");
}
public XElement Grid
{
get;
set;
}
}
My codebehind is as follows :::
public partial class MainView : Window
{
DataGrid dg;
public MainView()
{
InitializeComponent();
}
private void CommonPEGrid_Loaded(object sender, RoutedEventArgs e)
{
dg = sender as DataGrid;
DataGridTemplateColumn column = null;
column = new DataGridTemplateColumn();
column.Header = "Device";
column.CellTemplate = this.FindResource("DeviceDefault") as DataTemplate;
dg.Columns.Add(column);
column = new DataGridTemplateColumn();
column.Header = "Commands";
column.CellTemplate = this.FindResource("MethodDefault") as DataTemplate;
dg.Columns.Add(column);
}
}
Now I don't know how to display the elements in XML in a ComboBox using DataTemplate. It gives me so many errors::: Also the combobox items is always empty :( :( . Where am I going wrong. Experts please guide me !!! My code is as follows::
<DataTemplate x:Key="DeviceDefault">
<ComboBox ItemsSource="{Binding XPath=Devices}" SelectedIndex="0" TextSearch.TextPath="Value" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Width="auto" FontSize="9" FontWeight="2" Height="auto" Margin="2" Text="{Binding Element[Device].Value}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>