2009-06-18 84 views
5

我在想,这是获得众所周知的标签输入[或输出,无所谓]组合的最佳和最快捷的方式在WPF中。它是一种简单的任务,只是觉得 “对象” 的快速输出的ME:WPF - 最佳实践[标签:输入]控制


名称 - 基督教

年龄 - 28

情绪 - 好


我知道,我可以在TextBlocks中使用Grid。但说实话,这个“short”XAML差不多长达半页(RowDefinitions,ColDefs,Grid.Col on each Label)

另一种方式,使用三个StackPanels(水平)和一个垂直似乎也是一个有点愚蠢。在这种情况下,我必须给每个标签固定宽度,以使缩进正确。它只是不“感觉”正确。因此,考虑到上面的情况,你得到一个自定义对象,你只需要将其转储为只读到您的GUI的3-6个属性,那么您将如何执行它(在WPF中,Silverlight也是如此,如果您真的在心情:)。

我当然可以为此编写一个usercontrol。但是,为什么推倒重来,如果它可能已经在那里...

最后,甚至进一步说明,我只是在现实生活中产生的例子,是这个职位的原因:

 <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> 
+0

您似乎在寻求“最佳/最佳实践”和“最快/最快输出/转储”两件事。你想要哪一个?布赖恩答给你一个快速的解决方案,和乔W.给你一个很好的格式解决方案,你在你的问题解雇... – micahtan 2009-06-19 05:31:33

+0

是的,你是对的,不是有史以来最好的问题。我认为布赖恩答答案尽可能短,我喜欢它没有格式化的真正快速和肮脏的东西。用户控制方法也非常好,也许它被投票了一点点。感谢所有您的想法... – 2009-06-19 10:26:39

回答

1

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

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

也许你应该重新考虑你的用户界面。为什么你想要标签 - 文本框在同一行?这是一个可怕的浪费空间。

为什么不标签超过 texbox?那么你已经有了一个简单的用户界面简单XAML:

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

添加一些造型为你的TextBlocks,你已经有了一个不错的,干净的UI,很少重复。

1

你可以使用共享的大小团体获得两个很好的内衬向上列的自动调整大小格的行为,同时仍然能够在复杂拉出到用户控件。

下面是一个使用LabeledEdit控件的例子,它可以做你正在寻找的东西。复杂性已经全部分解消失在用户控件,所有你需要做的就是记住设置Grid.IsSharedSizeScope上的StackPanel:

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

下面是为用户控件的源代码。 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); } 
     } 
    } 
}