2014-06-17 54 views
0

我在xml文件中有一個任務列表,我希望它將它綁定到標籤,在每個模板中的每個標籤上一個接一個,我無法根據xml文件中任務的數量來加載模板,任務的數量應該等於模板的數量,並且標籤中應該包含任務的名稱,im卡在這裏是數據模板的代碼如何根據xml文件中的數據數量在列表框中加載數據模板

<Window.Resources> 
      <DataTemplate x:Key="tasktemplate1"> 
       <Canvas Height="50" Width="850"> 
        <Label Height="30" Width="170" Canvas.Top="10" Canvas.Left="130" Background="LightGray"> 
        </Label> 
        <TextBox Height="30" Width="120" Canvas.Top="10" Canvas.Left="370" Background="LightBlue"></TextBox> 
        <Label Canvas.Left="500" Canvas.Top="10">$</Label> 
        <Button Click="deletebuttonclick" Canvas.Top="12" Height="10" Width="30" Canvas.Left="600" ></Button> 
       </Canvas> 
      </DataTemplate> 
     </Window.Resources> 

的列表框代碼是這樣的

<TabItem> 
     <Canvas Height="700" Width="850"> 

      <ListBox ItemTemplate="{StaticResource tasktemplate1}" ItemsSource="{Binding NameList}" x:Name="listBox" Height="700" Width="850"> 
      </ListBox> 
      <Label Canvas.Top="-18" Canvas.Left="185">Select Task</Label> 
      <Label Canvas.Top="-18" Canvas.Left="377" RenderTransformOrigin="0.58,0.462">Enter Bill Rates</Label> 
      <Button Canvas.Left="39" Canvas.Top="575" Width="139">Click to add the task</Button> 
     </Canvas> 
    </TabItem> 

目前,我將它綁定到一個nameli ST我已經手工創建的代碼隱藏如此依賴於該代碼4周的DataTemplates被創建,但我沒能獲得從XML文件中的任務我的模板標籤

private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     XmlDocument doc1 = new XmlDocument(); 
     doc1.Load("http://store.tymesheet.com/templates/Software-Developer.xml"); 
     XmlElement root = doc1.DocumentElement; 
     XmlNodeList nodes = root.SelectNodes("Tasks/task"); 
     string temp=null; 
     foreach(XmlNode node in nodes) 
     { 
      temp = node["name"].InnerText; 
     } 

     _nameList = new List<string> 
        { 
         "1", 
         "2", 
         "3", 
         "4", 
        }; 
     DataContext = this; 
    } 

    private List<string> _nameList; 
    public List<string> NameList 
    { 
     get { return _nameList; } 
    } 

爲XML文件中的截圖上 enter image description here

我的模板的外觀的屏幕截圖是在這裏

enter image description here

回答

1

如果你需要得到name的任務,您可以直接在XAML中使用XmlDataProvider,它將充當您的列表框的ItemsSource

<Canvas Height="700" Width="850"> 
    <Canvas.Resources> 
     <XmlDataProvider x:Key="Tasks" XPath="tasks" 
      Source="http://store.tymesheet.com/templates/Software-Developer.xml"/> 
     <DataTemplate x:Key="tasktemplate1"> 
      <Canvas Height="50" Width="850" Background="lightgray"> 
       <Label Content="{Binding XPath=name}" Height="30" 
         Width="170" Canvas.Top="10" Canvas.Left="150" 
         Background="LightGray"/> 
       <TextBox Height="30" Width="60" Canvas.Top="10" 
         Canvas.Left="370" Background="Black"/> 
       <Label Canvas.Left="500" Canvas.Top="10">$</Label> 
       <Button Click="deletebuttonclick" 
         Canvas.Top="12" Height="10" Width="30" 
         Canvas.Left="600"/> 
      </Canvas> 
     </DataTemplate> 
    </Canvas.Resources> 
    <ListBox ItemTemplate="{StaticResource tasktemplate1}" 
      ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
      x:Name="listBox" Height="700" Width="850"/> 
    .... 
</Canvas> 

設置路徑的ChildNodes和標籤綁定XPathname


UPDATE

如果你想做到這一點在後面的代碼,你必須填寫列表和設置的ItemsSource是該列表。 (請確保您添加的命名空間System.Linq的使用擴展方法,如選擇,OfType等)

XmlDocument doc = new XmlDocument(); 
doc.Load("http://store.tymesheet.com/templates/Software-Developer.xml"); 
var taskList = doc.ChildNodes.OfType<XmlNode>() 
       .Where(node => node.Name == "tasks") 
       .SelectMany(node => node.ChildNodes.OfType<XmlNode>()) 
       .Select(node => node["name"].InnerText); 
listBox.ItemsSource = taskList; 

XAML

綁定標籤內容有約束力的是這樣的:

<Canvas Height="700" Width="850"> 
    <Canvas.Resources> 
     <DataTemplate x:Key="tasktemplate1"> 
      <Canvas Height="50" Width="850" Background="lightgray"> 
       <Label Content="{Binding}"  <-- HERE 
         Height="30" Width="170" 
         Canvas.Top="10" Canvas.Left="150" 
         Background="LightGray"/> 
       <TextBox Height="30" Width="60" Canvas.Top="10" 
         Canvas.Left="370" 
         Background="Black"/> 
       <Label Canvas.Left="500" Canvas.Top="10">$</Label> 
       <Button Click="deletebuttonclick" Canvas.Top="12" Height="10" 
         Width="30" Canvas.Left="600"/> 
      </Canvas> 
     </DataTemplate> 
    </Canvas.Resources> 
    <ListBox ItemTemplate="{StaticResource tasktemplate1}" 
       x:Name="listBox" Height="700" Width="850"/> 
    .... 
</Canvas> 
+0

比wat有關創建模板的數量嗎?它會和xml文件中的任務數量完全一樣嗎? – Safwan

+1

是因爲我已綁定到ChildNodes集合,因此它將等於集合中的任務數。在你提供的XML中將顯示11個任務。 –

+0

如果我想從後面的代碼加載它 – Safwan

相關問題