如何从一个列表框中的标签内容(how to get the label content from a

2019-10-20 07:28发布

我有一个标签,在列表框,还有在列表框中许多的DataTemplates,每个模板都有一个标签,我无法检索标签从后面的代码在cs文件的内容,数据模板是常见的,但每个标签都有不同它里面的文字,所以,我怎么能检索来自templates.As每个标签值还没有为模板,删除模板selected.so如果用户删除模板删除按钮,里面有列表框少模板,所以如何做我通过标签迭代现在检索值。

这里是模板的代码

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

任何帮助,感谢名单。

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


文章来源: how to get the label content from a list box