7

我想知道,这是在 WPF 中获得众所周知的标签输入 [或输出,无关紧要] 组合的最好和最快的方法。它是一个简单的任务,想想“对象”ME的快速输出:


名字 - 克里斯蒂安

年龄 - 28

心情——好


我知道,我可以使用带有 TextBlocks 的 Grid。但老实说,“短” XAML 几乎是半页长(每个标签上的 RowDefinitions、ColDefs、Grid.Col)

另一种方法,使用三个 StackPanels(水平)和一个垂直似乎也有点愚蠢。在这种情况下,我必须给每个标签一个固定的宽度,以使缩进正确。它只是不“感觉”正确。

因此,鉴于上述情况,您有一个具有 3-6 个属性的自定义对象,您只想将其作为只读转储到您的 GUI,您将如何做(在 WPF 中,Silverlight 也是,如果您真的有心情:)。

当然,我可以为此编写一个用户控件。但是为什么要重新发明轮子,如果它可能已经存在的话......

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

      <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>
4

4 回答 4

2

您可以使用共享大小组来获得两个排列整齐的列的自动调整大小的网格行为,同时仍然能够将复杂性提取到 UserControl 中。

下面是一个使用 LabeledEdit 控件的示例,它可以满足您的需求。复杂性都被考虑到了 UserControl 中,您需要做的就是记住在 StackPanel 上设置 Grid.IsSharedSizeScope:

<Window x:Class="WpfApplication5.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Name="Self" Title="Window1" Height="300" Width="300">
    <StackPanel Grid.IsSharedSizeScope="True">
        <local:LabeledEdit Label="Name"/>
        <local:LabeledEdit Label="Age" Text="28"/>
        <!-- and with databinding... -->
        <local:LabeledEdit Label="Width"
                           Text="{Binding Width, ElementName=Self}"/>
        <local:LabeledEdit Label="Height"
                           Text="{Binding Height, ElementName=Self}"/>
    </StackPanel>
</Window>

这是 UserControl 的源代码。LabeledEdit.xaml:

<UserControl x:Class="WpfApplication5.LabeledEdit"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Name="Self">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" SharedSizeGroup="LabeledEdit_Labels"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0" Content="{Binding Label, ElementName=Self}"/>
        <TextBox Grid.Column="1" Text="{Binding Text, ElementName=Self}"/>
    </Grid>
</UserControl>

LabeledEdit.xaml.cs:

using System.Windows;

namespace WpfApplication5
{
    public partial class LabeledEdit
    {
        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(object), typeof(LabeledEdit));
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(LabeledEdit),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public LabeledEdit()
        {
            InitializeComponent();
        }

        public object Label
        {
            get { return GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
    }
}
于 2009-06-18T21:57:01.817 回答
1

如果您使用的是 3.5sp1,则可以在绑定中使用 StringFormat。像这样的东西应该工作......

<TextBlock Text="{Binding LastLogRun.LogMessageCount, StringFormat={}Log Count - {0}}" />
于 2009-06-18T21:49:30.193 回答
0

也许你应该重新考虑你的 UI。为什么要将标签 - 文本框放在同一行上?这是对空间的可怕浪费。

为什么不在texbox上贴标签?然后你就有了一个简单的 UI简单的 XAML:

<StackPanel Orientation="Vertical">
  <TextBlock>Name</TextBlock>
  <TextBox />
  <TextBlock>Age</TextBlock>
  <TextBox />
  <TextBlock>Mood</TextBlock>
  <TextBox />
</StackPanel>

为您的 TextBlocks 添加一些样式,您就拥有了一个漂亮、干净的 UI,几乎没有重复。

于 2009-06-18T21:25:47.307 回答
0

silverlight 工具包有一个非常酷的DataForm控件!

于 2009-06-19T06:37:43.017 回答