关于 xml:如何在列表框上应用数据模板 | 珊瑚贝

how to apply data template on listbox


我有一个数据模板,并试图将它应用到一个列表框,我在模板上有文本框、标签和一个按钮,但它没有显示在列表框上,我还没有任何数据绑定,但它仍然存在必须显示文本框、标签、按钮,但它没有显示,
这是用作资源的数据模板的代码

1
2
3
4
5
6
7
8
9
10
11
<Window.Resources>
            <DataTemplate x:Key=“tasktemplate1”>
                <Canvas Height=“50” Width=“850” Background=“lightgray”>
                    <Label   Height=“30” Width=“170” Canvas.Top=“10” Canvas.Left=“150” Background=“LightGray”>
                    </Label>
                    <TextBox Height=“30” Width=“60” Canvas.Top=“10” Canvas.Left=“370” Background=“Black”></TextBox>
                    <Label Canvas.Left=“500” Canvas.Top=“10”>$</Label>
                    <Button Click=“deletebuttonclick” Canvas.Top=“12” Height=“10” Width=“30” Canvas.Left=“600”></Button>
                </Canvas>
            </DataTemplate>
        </Window.Resources>

和我的列表框的代码

1
2
3
4
5
6
7
8
9
<TabItem>
        <Canvas Height=“700” Width=“850”>
            <ListBox ItemTemplate=“{StaticResource tasktemplate1}” ItemsSource=“{Binding}” x:Name=“listBox” Height=“700” Width=“850”>
            </ListBox>
            <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 Canvas.Left=“39” Canvas.Top=“575” Width=“139”>Click to add the task</Button>
        </Canvas>
    </TabItem>

我哪里出错了?任何帮助,谢谢

  • 您没有正确设置列表框的 ItemsSource,因此列表不包含任何项目,因此未应用数据模板..,将 ItemSource 设置为项目列表
  • bt 为什么它不显示空标签和文本框。?


DataTemplate 用于表示 listBoxItems 的视觉布局。但是,ItemsSource 集合为空或计数为 0,(假设因为您将其设置为仅绑定)因此不会生成任何项目并显示在您的列表框中。您需要传递带有一些对象的 ItemsSource,以便将 DataTemplate 应用于 ListBoxItems.

为了测试,您可以在 XAML 本身中定义 ItemsSource:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<Canvas Height=“700” Width=“850”>
    <Canvas.Resources>
        <ObjectDataProvider x:Key=“EnumerableRange”
            xmlns:sys=“clr-namespace:System;assembly=mscorlib”
            xmlns:linq=“clr-namespace:System.Linq;assembly=System.Core”
            ObjectType=“{x:Type linq:Enumerable}” MethodName=“Range”>
            <ObjectDataProvider.MethodParameters>
                <sys:Int32>1</sys:Int32>
                <sys:Int32>10</sys:Int32>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Canvas.Resources>
    <ListBox ItemTemplate=“{StaticResource tasktemplate1}”
             ItemsSource=“{Binding Source={StaticResource EnumerableRange}}”
             x:Name=“listBox” Height=“700” Width=“850”/>
     ……
</Canvas>

在上面的示例中,我基本上提供了一个包含 10 个项目的整数集合作为 ItemsSource。因此,您将看到 10 个模板。如果您想查看更多项目,请在资源 EnumerableRange.

中将数字从 10 更新为其他值

  • 如果我想要它是动态的,意味着它取决于外部的 xml 文件,它有一定数量的任务,假设有 11 个任务。
  • 为此,您必须在后面的代码中创建集合并将 listBox 的 ItemsSource 设置为该集合。
  • 我被困在我的标签上,因为我希望显示任务,如果你能帮助我,请帮助我。
  • 我的 xml 存储位置是 store.tymesheet.com/templates/Software-Developer.xml
  • 您能否针对此 XML 的要求单独提出一个问题?一些快照会有所帮助。你想要的样子。从您的评论来看,这个 XML 的外观并不直观。向单独的问题添加更多详细信息。
  • 好的,我可以给你一个完整的场景,我会添加一个新问题
  • 当然。当你添加问题时让我知道。
  • -我已经发布了我的问题


来源:https://www.codenong.com/24260525/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9290.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?