如何从列表框中获取标签内容 [英] how to get the label content from a list box

查看:67
本文介绍了如何从列表框中获取标签内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表框中有一个标签,该列表框中有很多数据模板,并且每个模板都有一个标签,我无法从.cs文件中后面的代码中检索标签的内容,该数据模板是通用的,但每个标签内有不同的文本.因此,如何从模板中检索每个标签值.还有一个用于模板的删除按钮,该按钮会删除所选的模板.因此,如果用户删除模板,则列表框中的模板会更少,那么我现在如何遍历标签以获取价值.

i have a label in a listbox,there are many datatemplates in the listbox,and each template has a label,i am unable to retrieve the content of label from code behind in .cs file,the data template is common but every label has different text inside it.,so how can i retrieve each label value from the templates.As also there is a delete button for the template,which deletes the template selected.so if user deletes the templates,there are less templates inside listbox,so how do i iterate through the labels now to retrieve value.

这是模板的代码

<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文件.

In my .cs file i have also loaded the xml file for deleting the template.

{
            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;
        }

任何帮助,谢谢.

推荐答案

用以下代码替换您的foreach循环:

Replace your foreach loop with this:

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);
}

这篇关于如何从列表框中获取标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆