how to get the label content from a list box

2019-08-03 07:54发布

问题:

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.

here is the code for template

<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>

here is the behind code for button

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

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

any help,thanx.

回答1:

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