在我的文本块前面添加标签

发布于 2024-11-04 14:03:53 字数 1550 浏览 0 评论 0原文

我有一个从网络服务获取数据的列表,但数据需要一个标签或另一个文本块来说明该数据是什么。有没有办法将其插入到此列表框中?

所以我想要的结果是

“HIN Number:” - 然后显示绑定到 HINNumber 的文本块

“类别字母:” - 然后显示绑定到categoryLetter的文本块

“类别1:” - 然后显示绑定到category1的文本块

“类别 2:” - 然后显示绑定到类别 2 的文本块

“类别 3:” - 然后显示绑定到类别 3 的文本块

我想我可以将另一个文本块对齐到匹配的数据绑定文本块旁边,但有些值为空,所以列表框的位置不断变化,所以这是行不通的。

        <ListBox x:Name="HINList" Margin="0,300,-12,0" ItemsSource="{Binding Details}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock Text="{Binding HINNumber}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding CategoryLetter}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I have a list which gets data from a web service but the data needs a label or another textblock to say what that data is. Is there a way of inserting this within this listbox?

so my desired outcome would be

"HIN Number:" - then display the textblock which is bound to HINNumber

"Category Letter:" - then display the textblock which is bound to categoryLetter

"Category 1:" - then display the textblockwhich is bound to category1

"Category 2:" - then display the textblockwhich is bound to category2

"Category 3:" - then display the textblockwhich is bound to category3

I thought I could just align another textblock up next to the matching databound textblock but some values are null and so the position of the listbox is constantly changing so this wouldnt work.

        <ListBox x:Name="HINList" Margin="0,300,-12,0" ItemsSource="{Binding Details}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock Text="{Binding HINNumber}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding CategoryLetter}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最丧也最甜 2024-11-11 14:03:53

这取决于您是否希望标签位于每个文本块的左侧或上方。

上面更简单 - 只需在堆栈面板中使用额外的文本块:

<StackPanel Margin="0,0,0,17" Width="432">
    <TextBlock Text="HIN Number" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="{Binding HINNumber}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="Category Letter" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="{Binding CategoryLetter}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="Category 1" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="Category 2" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>

    <StackPanel Visibility={Binding Category3Visibility}>
        <TextBlock Text="Category 3" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
        <TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    </StackPanel>       

</StackPanel>


    public System.Windows.Visibility Category3Visibility
    {
        get { return string.IsNullOrEmpty(Category3) ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible; }
    }

如果您希望标签位于右侧,请使用 2 列网格来布局控件。

It depends if you want the label to the left of or above each textblock.

Above is easier - just use extra textblocks in the stackpanel:

<StackPanel Margin="0,0,0,17" Width="432">
    <TextBlock Text="HIN Number" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="{Binding HINNumber}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="Category Letter" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="{Binding CategoryLetter}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="Category 1" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="Category 2" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>

    <StackPanel Visibility={Binding Category3Visibility}>
        <TextBlock Text="Category 3" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
        <TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    </StackPanel>       

</StackPanel>


    public System.Windows.Visibility Category3Visibility
    {
        get { return string.IsNullOrEmpty(Category3) ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible; }
    }

If you want the labels to the right, use a 2 column grid to layout the controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文