【问题标题】:how to get the label content from a list box如何从列表框中获取标签内容
【发布时间】:2014-06-18 12:40:03
【问题描述】:

我在列表框中有一个标签,列表框中有很多数据模板,每个模板都有一个标签,我无法从.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;
        }

任何帮助,谢谢。

【问题讨论】:

  • 标签的绑定属性是什么?
  • Content="{Binding XPath=name}"
  • 不绑定,如果你想访问标签,为你的标签命名,使用FindName方法获取标签的内容
  • 文章中有FindVisualChild&lt;T&gt;方法的实现。查看页面底部(最后一个代码sn-p)。将其复制并粘贴到您的应用程序中。
  • 您只需要像这样更改 Xpath 和 itemsource 即可获得所有值 prntscr.com/3u1v27 我得到了这个 o/p prntscr.com/3u1w4x

标签: c# wpf templates listbox label


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多