我想知道,在WPF中,哪一种是获得著名标签输入或输出的最佳和最快的方式并不重要。这是一个简单的任务,只需考虑一下"object“ME的快速输出:
名字-克里斯蒂安
年龄- 28岁
心情好
我知道,我可以在TextBlocks中使用网格。但老实说,这个“短”的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>发布于 2009-06-18 21:49:30
如果使用3.5sp1,则可以在绑定中使用StringFormat。像这样的东西应该管用..。
<TextBlock Text="{Binding LastLogRun.LogMessageCount, StringFormat={}Log Count - {0}}" />发布于 2009-06-18 21:57:01
您可以使用共享大小组来获得两个排列良好的列的自动大小网格行为,同时仍然能够将复杂性提取到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-18 21:25:47
也许你应该重新考虑一下你的UI。为什么你想要标签-文本框在同一行?这是对空间的可怕浪费。
为什么不在文本框上贴标签呢?然后有一个简单的UI和简单的XAML:
<StackPanel Orientation="Vertical">
<TextBlock>Name</TextBlock>
<TextBox />
<TextBlock>Age</TextBlock>
<TextBox />
<TextBlock>Mood</TextBlock>
<TextBox />
</StackPanel>为您的TextBlocks添加一些样式,您就有了一个很好的、干净的UI,并且很少重复。
https://stackoverflow.com/questions/1015226
复制相似问题