如何从列表框中获取标签内容

时间:2014-06-18 12:40:04

标签: c# wpf templates listbox label

我在列表框中有一个标签,列表框中有很多数据窗口,每个模板都有一个标签,我无法从.cs文件后面的代码中检索标签内容,数据模板很常见但是每个标签里面有不同的文本。所以如何从模板中检索每个标签值。还有一个模板的删除按钮,删除所选的模板。如果用户删除模板,列表框内的模板较少,那么我如何现在迭代标签来检索价值。

这是模板的代码

<TabItem>
    <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">
                    <Label x:Name="tasklabel" Content="{Binding XPath=name}" Height="30"
               Width="170" Canvas.Top="10" Canvas.Left="150" 
               Background="LightGray"/>
                    <TextBox Height="30" Width="100" Canvas.Top="10"
                 Canvas.Left="370" Background="AliceBlue"/>
                    <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 Tasks}" 
  x:Name="tasklistBox" Height="700" Width="850"/>
        <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 Click="addtask" Canvas.Left="39" Canvas.Top="575" Width="139">Click to add the task</Button>
    </Canvas>
</TabItem>

这是按钮的后面代码

 private void addtask(object sender,RoutedEventArgs e)
    {
        foreach (ListBoxItem item in tasklistBox.Items)
        {
            // Getting the ContentPresenter of myListBoxItem
            ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(item);
            // Finding textBlock from the DataTemplate that is set on that ContentPresenter
            DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
            System.Windows.Forms.Label mydata = (System.Windows.Forms.Label)myDataTemplate.FindName("tasklabel", myContentPresenter);
            // Do something to the DataTemplate-generated TextBlock
            System.Windows.MessageBox.Show("element" + mydata);
        }
    }

在我的.cs文件中,我还加载了xml文件以删除模板。

{
            InitializeComponent();

            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>());
            Tasks = new ObservableCollection<XmlNode>(taskList);

            this.DataContext = this;
        }

任何帮助,thanx。

1 个答案:

答案 0 :(得分:1)

用这个替换你的foreach循环:

foreach (object item in taskslistBox.Items)
{
    var listBoxItem = taskslistBox.ItemContainerGenerator.ContainerFromItem(item);
    var myContentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
    var myDataTemplate = myContentPresenter.ContentTemplate;
    var mydata = (Label)myDataTemplate.FindName("tasklabel", myContentPresenter);
    var xmlElement = (XmlElement)mydata.Content;
    MessageBox.Show("element " + xmlElement.InnerText);
}