<Button Margin="5,5,5,5">Alpha</Button>
Sure, I guess it could work, but you are still using the wrong control for the job IMO. The UniformWrapPanel
inherits from a panel behind the scenes. Some of that code could be used with a simple panel instead; to inherit the same functionality.<Grid.ColumnDefinitions>
和 set the width to Auto
并通过在Xaml中设置控件的边距, you can set some "spacing"在您的元素之间。If you go onto MSDN and search formaybe it be best to use<Grid.ColumnDefinitions>
和 set the width toAuto
并通过在Xaml中设置控件的边距, you can set some "spacing"在您的元素之间。
Grid.ColumnDefinitions
you will see what I am suggesting. Had you already implemented as I suggested, you wouldn't need a wrap panel at all; least with numerous stack panels. That's a misuse of controls. 同样,这不是堆栈面板的用途,正如我上面已经向您解释的那样。我会再次引用自己,希望您能阅读我给您的建议。我说 :通过为我的包装面板的子代设定堆叠面板的宽度和高度,可以使它们很好地对齐,这只留下了一个小怪癖。
话虽如此;我并不是要暗示那里的任何地方都在包装面板内使用了许多堆叠面板。不可以,因为StackPanel不会按照您想要的方式包装内容。相反,它向一个方向扩展其内容;是水平的还是垂直的。这使您可以将项目堆叠在堆叠中另一个项目之上。
覆盖面板将通过将它们的子控件包装在一起来堆叠其子控件,这是基于每个子控件的最佳可能配合以充分填充该覆盖面板。
To be doubly clear, this was implying that you set a margin on your labels through your Xaml code, which will give you adequate spacing when your Coloums are being auto aligned to your Grid by using the并通过在Xaml中设置控件的边距
Auto
property when setting the width for each 列定义s
. <Grid.ColumnDefinitions>
. <Grid HorizontalAlignment="Left" Height="269" VerticalAlignment="Top" Width="509">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
</Grid>
那不是我给你的建议。
我并不是要暗示那里的任何地方都在包装面板内使用了许多堆叠面板。
If you go onto MSDN and search forGrid.ColumnDefinitions
you will see what I am suggesting. Had you already implemented as I suggested, you wouldn't need a wrap panel at all; least with numerous stack panels. That's a misuse of controls.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WPFTestApp.Window1"
Title="Window1" Width="Auto">
<Grid Width="Auto" AutomationProperties.IsOffscreenBehavior="Onscreen">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Margin="5,5,5,5" Content="Row AAAAA Column A" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row A Column B" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row A Column CCCCCCC" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row B Column A" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row B Column B" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row B Column C" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
</Grid>
</Window>
<Grid.ColumnDefinitions>
和 <Grid.RowDefinitions>
, and once they are created, you would add more labels etc. It's not something I will be doing for you, as its time consuming, and time I'd rather not waste doing it for you. It's a learning experience for you too, so play with what I gave you. 因此,您试图说仅使用网格是不可能的吗?
Your current problem is your lack of experience with Xaml code. But don't take that as me judging you, as I'm not. I admire your willingness to buy a book on WPF alone, then to further teach yourself, by dabbling with the various controls to try and achieve a very simple task. That's actually rude of me, since any task is easy when you know how to approach it. Whereas; in my case, I've spent the better part of my later career and life learning as much WPF as I could. So, this is actually all very boring for me, as it is only basic Xaml, and basic structuring. Of course, you're only partly there, since you have yet to write your code for the various layout to be altered when your user decides to do his clicky thing and add more controls, and for each control, you need more<Grid.ColumnDefinitions>
和<Grid.RowDefinitions>
, and once they are created, you would add more labels etc. It's not something I will be doing for you, as its time consuming, and time I'd rather not waste doing it for you. It's a learning experience for you too, so play with what I gave you.
祝你有一个美好的夜晚。
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Width="510" Height="300" Background="#FF191919" Foreground="LightSlateGray" WindowStartupLocation="CenterScreen" >
<WrapPanel Grid.Row="3"
Grid.Column="1"
Grid.ColumnSpan="3"
HorizontalAlignment="Center">
<Grid Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28" />
</Grid>
<Grid Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
</WrapPanel>
</Window>
@glasswizzard :
在文章#7的屏幕截图中,您有一些项目包含两行文本。两行文本是有意的,还是长行文本的结果,因为您为每个项目设置了特定的宽度,所以该行将行换成两行?
所有物品的宽度都一样吗?
如果它们都相同,那是您硬编码的东西,还是以某种方式计算出来的?
对于两行换行的项目,您是否决定要使文本顶部对齐,在数字上方对齐至底部还是在顶部与数字之间居中?
没有。只是要注意,当我回答帖子时,我会根据提出的问题回答帖子,而我只处理该问题的具体内容。您问如何解决这个问题,我为您提供了使用网格的示例。但是,您的代码正在使用多个网格。那不是我建议您去做的方式。如果您回读了,我建议使用 一 网格,并为您提供了使用文件后面的代码添加列/行定义,然后像在Xaml中所做的那样将标签或文本框添加到该网格的线索。除非您按照我上面的建议在文件背后的代码中执行此操作。我还说过,我并没有为您写这篇文章,因为它需要很多时间,而且我通常会为此付费,因为这也很耗时,但是我确实解释了如何解决,不是吗?我只是看不到任何包裹,因为它在网格中。
这是一个很难回答的问题。网格上有多种属性,还需要为要添加到该网格的每个新对象设置其子对象。对于要添加的每个新对象,您将需要其他列/行定义,然后再添加新对象,所以,是的,我想可能会有很多额外的代码。哈哈如果窗口大小足够减小,您认为还需要多少代码来包装这些列?
唯一缺少的是您对Xaml的经验不足。有很多不同的方法可以解决此问题,您只需要尝试更多。我觉得我缺少一些关键信息,这使得这似乎绝对不可能
在您的代码中:运行应用程序并确认调整窗口大小时看到的行为可以单独使用网格来完成
<Grid Margin="0 5" Height="75" Width="100" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<RowDefinition Height="*"/>
, well you should be using the Auto property when setting the height 和 宽度和 列定义的。特别是因为如果您不知道标签/其他对象的宽度,则auto属性将为您解决这一问题。您在正确的道路上,只需要使用 一 对象网格而不是 多 对象网格。这就是为什么我们可以添加列和行。您刚刚从使用多个堆栈面板更改为使用多个网格。返回并再次阅读帖子8。Why would they have to move or move to a different grid if you were using the Auto value as I have done in the example. You're not using the same structure I was. If you are needing to add more controls, then you need to add the relevant Column/RowDefinitions in your code behind file. You're not going to be able to do that, since you haven't named any of your Grid controls. In WPF, if you don't name your controls in Xaml, you simply can't access those controls in a code behind file. So how do you expect to add new Column/Row definitions to a control you can't access? Your Grids are nameless....我有自己的文本块(我一直以来都称它们为标签),每个都设置为它们合适的行/列,这是我无法通过的砖墙,它们必须移动,如何才能移动到另一个细胞?
<Grid Margin="0 5" Height="75" Width="100" >
. Read my comments above regarding using one Grid.您将从一个基本布局开始。 一格 -多个列/行定义。为每个定义添加基本数量的控件。当用户使用您的表单并将其添加到表单中时,您需要首先添加相关的“列/行”定义,然后将新的控件添加到该网格中。我了解您所写的内容,只是看不到任何包装方式,因为它位于网格中。
我对以上引用的意思是您的问题已经从"如何在网格中而不是在包装面板中完成我想做的事情"(根据我的建议),"调整窗口大小时如何处理网格". Your recent problem with resizing the window is a different problem, and one which should be addressed on a new topic when you reach that stage. First, stick with this topic and write the Xaml as I have shown and explained to you. Give your Grid a name, and then write your C# code in your Code behind file for adding the functionality of adding new controls to your grid. Once you reach this point, you will probably end up using a wrap panel around all of the grids items, (that is of course if you don't want to bother writing a lengthy class based on the Window Size from the Windows没有。只是要注意,当我回答帖子时,我会根据提出的问题回答帖子,而我只处理该问题的具体内容。您问如何解决这个问题,我为您提供了使用网格的示例。
SizeChangedEventArgs
to structure your grids elements based on the window size and how many items are in your grid.) Yes this would be avoided by using a wrap panel. That being said, I'd prefer to write an explicit class to handle the grids elements when the Window changes size. I don't think there are many programmers who would do it my way, so I am not advising that you do either, as the simpler thing at that point would be to use a wrap panel and be done with it. As an added exercise, you could always remove your wrap panel and write an explicit class to handle your Grids elements when your window size changes. <Grid x:Name="MyGrid" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Margin="5,5,5,5" Content="Row AAAAA Column A" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row A Column B" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row A Column CCCCCCC" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row B Column A" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row B Column B" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
<Label Margin="5,5,5,5" Content="Row B Column C" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
</Grid>
<WrapPanel Grid.Row="3"
Grid.Column="1"
Grid.ColumnSpan="3"
HorizontalAlignment="Center">
<Grid Margin="0 5" Height="75" Width="100">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
<Grid Margin="0 5" Height="75" Width="100" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Text="LoremIpsum Lorem" TextDecorations="Underline" HorizontalAlignment="Center" FontSize="14" FontFamily="Georgia"/>
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="0" HorizontalAlignment="Center" FontSize="28"/>
</Grid>
</WrapPanel>
foreach (ColumnDefinition c in MyGrid.ColumnDefinitions)
{
}
var CD = new ColumnDefinition();
CD.Width = GridLength.Auto;
MyGrid.ColumnDefinitions.Add(CD);
是的。我有一个带有几个标签的包装面板,用户可以启用或禁用这些标签(折叠或显示)。当它环绕时,第二行上的标签与上面的标签不对齐(因为它们的长度不同)。
有没有办法使它们彼此对齐,使它们的中心对齐?
WrapPanel
in the following XAML:<Window x:Class="WpfTiles.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTiles"
mc:Ignorable="d"
Title="Color Swatches" Height="450" Width="810">
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Colors}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical"
Margin="5">
<Border Height="60">
<TextBlock Text="{Binding Name}"
FontSize="16"
TextWrapping="Wrap"
TextAlignment="Center"
VerticalAlignment="Bottom"/>
</Border>
<Rectangle Fill="{Binding Brush}"
Height="40"/>
<TextBlock Text="{Binding HexValue}"
FontSize="10"
TextAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Window>
ItemWidth
attribute of the WrapPanel
:<Window x:Class="WpfTiles.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTiles"
mc:Ignorable="d"
Title="Color Swatches" Height="450" Width="810">
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Colors}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel ItemWidth="110"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical"
Margin="5">
<Border Height="60">
<TextBlock Text="{Binding Name}"
FontSize="16"
TextWrapping="Wrap"
TextAlignment="Center"
VerticalAlignment="Bottom"/>
</Border>
<Rectangle Fill="{Binding Brush}"
Height="40"/>
<TextBlock Text="{Binding HexValue}"
FontSize="10"
TextAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Window>
Width
of the StackPanel
. Use 100 for Width, instead of the 110 for ItemWidth. 110 == 100 + 5 + 5.)Border
around the TextBlock
that holds the color name. That is to force the text to be flushed bottom within the StackPanel
. There is a way to redo StackPanel
using a Grid
和 just row definitions and the existing textboxes and rectangles, but I was trying to stay focused on the creation and alignment of the tiles.DataContext
:using System.Windows;
namespace WpfTiles
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
}
using System.Collections.ObjectModel;
using System.Linq;
namespace WpfTiles
{
class MainWindowViewModel
{
public ObservableCollection<ColorInfo> Colors { get; }
public MainWindowViewModel()
{
Colors = new ObservableCollection<ColorInfo>(ColorInfo.EnumerateAllColors().OrderBy(c => c.Name));
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;
namespace WpfTiles
{
class ColorInfo
{
public string Name { get; }
public Color Color { get; }
public string HexValue => Color.ToString();
public Brush Brush => new SolidColorBrush(Color);
public ColorInfo(string name, Color color)
{
Name = GetTitleName(name);
Color = color;
}
public override string ToString() => $"{Name}";
string GetTitleName(string name)
{
var sb = new StringBuilder(name.Length + 3);
foreach(var ch in name)
{
if (char.IsUpper(ch) && sb.Length > 0)
sb.Append(' ');
sb.Append(ch);
}
return sb.ToString();
}
public static IEnumerable<ColorInfo> EnumerateAllColors()
{
foreach (var property in typeof(Colors).GetProperties())
{
var color = (Color) property.GetValue(null, null);
yield return new ColorInfo(property.Name, color);
}
}
}
}