WPF

时间:2017-11-07 15:48:53

标签: c# wpf

我正在尝试将按钮内的图像对齐到右侧。但它似乎没有做我想要它做的事情。我现在已经试了几个小时,所以我觉得是时候问这里了。这是我的代码atm。

public class DirectoryButton : Button
{
    public DirectoryButton(string name, string content)
    {
        Name = name.Replace(" ", "");
        //Content = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative)) + content;
        //Content = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative));
        var img = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative));

        var sp = new UniformGrid
        {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            Columns = 3,
            Rows = 1
        };
        sp.Children.Add(new Label());
        sp.Children.Add(new Label { Content = content, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });
        sp.Children.Add(new Image { Source = img, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center});
        //sp.Orientation = Orientation.Horizontal;
        Content = sp;
        //Content = content;
        Margin = new Thickness(0, 5, 0, 5);
        Height = 65;
        Width = 350;
        FontWeight = FontWeights.Bold;
        Foreground = new SolidColorBrush(Colors.Black);
        Background = new SolidColorBrush(Colors.Gray);
        HorizontalContentAlignment = HorizontalAlignment.Center;
        VerticalAlignment = VerticalAlignment.Center;
        Visibility = Visibility.Visible;
    }
}

button Image

这会使按钮中的图像显示但不在右侧。我无法在xaml中做任何事情,因为所有内容都是通过programmaticaly创建的。

此致

比约

2 个答案:

答案 0 :(得分:0)

HorizontalContentAlignment设置为Stretch

public class DirectoryButton : Button
{
    public DirectoryButton(string name, string content)
    {
        Name = name.Replace(" ", "");
        //Content = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative)) + content;
        //Content = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative));
        var img = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative));

        var sp = new UniformGrid
        {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            Columns = 3,
            Rows = 1
        };
        sp.Children.Add(new Label());
        sp.Children.Add(new Label { Content = content, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });
        sp.Children.Add(new Image { Source = img, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center });
        //sp.Orientation = Orientation.Horizontal;
        Content = sp;
        //Content = content;
        Margin = new Thickness(0, 5, 0, 5);
        Height = 65;
        Width = 350;
        FontWeight = FontWeights.Bold;
        Foreground = new SolidColorBrush(Colors.Black);
        Background = new SolidColorBrush(Colors.Gray);
        HorizontalContentAlignment = HorizontalAlignment.Stretch; //<--
        VerticalAlignment = VerticalAlignment.Center;
        Visibility = Visibility.Visible;
    }

这会使UniformGrid延伸以填满整个Button,而Image则会向右移动。

答案 1 :(得分:0)

我试图在上面的评论中解释。

您提供给我们的代码就像它应该的那样。

您创建一个包含三个单元格的UniformGrid,并将三个子项添加到网格中。 网格的每个单元格都具有相同的大小。

你的空标签将到达左侧。中心的下一个标签。 您将图像添加到右侧单元格并设置HorizontalAlignment = HorizontalAlignment.Right。这将使您的图像在右边缘的单元格内对齐。还有你网格的右边缘。

现在,您将UniformGrid添加为Content到您的按钮。 使用以下代码行

HorizontalContentAlignment = HorizontalAlignment.Center; 

您将把网格置于按钮中心。 这就是为什么你的图像不在按钮的右边缘。

正如我在评论中建议的那样,您可以设置

HorizontalContentAlignment = HorizontalAlignment.Right; 

并且您的图片整个网格将移动到右边缘,但这也意味着您的居中标签也会向右移动。

如果您想让第二个Label停留在按钮的中心,请使用Answer提供的mm8并使用 HorizontalContentAlignment = HorizontalAlignment.Stretch;