简体   繁体   English

以编程方式将图像在按钮内右对齐WPF

[英]align image to the right inside a button programmatically WPF

I'm trying to align a image inside a button to the right. 我正在尝试将按钮内的图像右对齐。 however it does not seem to do what i want it to do. 但是它似乎并没有按照我的意愿去做。 i have been trying for several hours now, so i think it is time to ask here. 我已经尝试了几个小时,所以我认为是时候在这里问了。 this is my code atm. 这是我的代码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 按钮图片

this makes the image in the button appear but it is not to the right. 这会使按钮中的图像出现,但不在右侧。 i cannot really do anything in the xaml since everything is created programmaticaly. 由于所有内容都是以编程方式创建的,因此我无法真正在xaml中执行任何操作。

Regards, 问候,

Bjorn 比约恩

Set the HorizontalContentAlignment to Stretch : 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;
    }

This should make the UniformGrid stretch to fill the entire Button and the Image will then end up to the right. 这将使UniformGrid拉伸以填充整个Button ,然后Image将在右侧结束。

As I tried to explain in the comments above. 正如我在上面的评论中试图解释的那样。

The code you've provide us works like it should. 您提供给我们的代码可以正常工作。

You create an UniformGrid with three cells and add three childs to the grid. 您创建具有三个单元格的UniformGrid并将三个子级添加到网格。 Each cell of the grid will have the same size. 网格的每个单元将具有相同的大小。

Your empty label will got to the left one. 您的空标签将位于左侧。 The next label to the center. 下一个标签到中心。 You will add your image to the right cell and set HorizontalAlignment = HorizontalAlignment.Right . 您将图像添加到右侧的单元格中,并设置HorizontalAlignment = HorizontalAlignment.Right This will align your image within his cell at the right edge. 这将使图像在其单元格的右边缘对齐。 And also to the right edge of your grid. 并位于网格的右边缘。

Now you add your UniformGrid as Content to your button. 现在,将UniformGrid作为Content添加到按钮。 With the following line of code 用下面的代码行

HorizontalContentAlignment = HorizontalAlignment.Center; 

you will center your grid within the button. 您将使网格居中于按钮内。 That's why your image is not at the right edge of your button. 这就是为什么图像不在按钮的右边缘。


As already suggested in my comment you can set 正如我的评论中已经建议的那样,您可以设置

HorizontalContentAlignment = HorizontalAlignment.Right; 

and your Image and the whole grid will move to the right edge, but this means also that your centered Label will also move to the right. 并且您的图片整个网格将移至右侧,但这也意味着居中的Label也将移至右侧。


If you want to let the second Label stay within the center of the Button take the Answer provided by mm8 and use HorizontalContentAlignment = HorizontalAlignment.Stretch; 如果你想让第二个Label按钮的中心内逗留拿答案提供MM8和使用HorizontalContentAlignment = HorizontalAlignment.Stretch;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM