WPF-常规[Label:Input]控件的最佳实践 [英] WPF - Best Practice for the run-of-the-mill [Label:Input] Control

查看:95
本文介绍了WPF-常规[Label:Input]控件的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,这是在WPF中获得众所周知的Label Input(或输出,无关紧要)组合的最佳和最快方法.它是一个简单的任务,只需考虑一下对象" ME的快速输出:

I am wondering, which is the best and quickest way to get the well known Label Input [or output, doesn't matter] combination in WPF. Its a simple Task, just think of a quick output of the "object" ME:

姓名-基督教徒

年龄-28

心情-好

我知道,我可以将Grid与TextBlocks一起使用.但老实说,为此的短" XAML将近半页长(每个Label上的RowDefinitions,ColDefs,Grid.Col)

I know, I can use a Grid with TextBlocks. But to be honest, the "short" XAML for this is nearly half a page long (RowDefinitions, ColDefs, Grid.Col on each Label)

另一种方法是,使用三个具有一个垂直方向的StackPanels(水平)似乎也有些愚蠢.在这种情况下,我必须给每个Label一个固定的宽度,以使缩进正确.而且只是感觉"不对.

The alternative way, using three StackPanels (horizontal) with one vertical seems also a little bit stupid. In this case, I have to give each Label a fixed width, to get the indent correct. And it just does not "feel" right.

因此,在上述情况下,您获得了一个具有3-6个属性的自定义对象,您只想将其作为只读内容转储到GUI中,您将如何处理(如果您真的很想在WPF中使用Silverlight, :).

So, given the Situation above, you got a custom object with 3-6 Properties you just want to dump as readonly to your GUI, how would you do it (in WPF, Silverlight too, if you are really in the mood :).

我当然可以为此编写一个用户控件.但是,如果可能已经在那儿了,为什么还要重新发明轮子呢?

I can, of course, write a usercontrol for this. But why reinvent the wheel, if it might be already there...

最后,为了进一步说明,我刚刚在现实生活中创建的示例是这篇文章的原因:

And finally, to illustrate even further, the example I just created in real life and was the reason for this post:

      <StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Log Count"  Width="100"/>
            <TextBlock Text="{Binding LastLogRun.LogMessageCount}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Start Time" Width="100"/>
            <TextBlock Text="{Binding LastLogRun.StartTime}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="End Time" Width="100"/>
            <TextBlock Text="{Binding LastLogRun.EndTime}"/>
        </StackPanel>
    </StackPanel>

推荐答案

如果您使用的是3.5sp1,则可以在绑定中使用StringFormat.这样的事情应该起作用...

If you're using 3.5sp1 you can use StringFormat in the binding. Something like this should work...

<TextBlock Text="{Binding LastLogRun.LogMessageCount, StringFormat={}Log Count - {0}}" />

这篇关于WPF-常规[Label:Input]控件的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆