Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在wpf中调整按钮背景图像的大小_C#_Wpf - Fatal编程技术网

C# 如何在wpf中调整按钮背景图像的大小

C# 如何在wpf中调整按钮背景图像的大小,c#,wpf,C#,Wpf,我正在申请Wpf。我最近遇到了一个问题。我想调整显示为在运行时以编程方式创建的按钮背景图像的图像大小。 我更希望降低图像的高度。 到目前为止,我所做的是: SqlDataAdapter adp = new SqlDataAdapter("Load_inventory_in_Pos", conn); adp.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure; adp

我正在申请Wpf。我最近遇到了一个问题。我想调整显示为在运行时以编程方式创建的按钮背景图像的图像大小。 我更希望降低图像的高度。 到目前为止,我所做的是:

        SqlDataAdapter adp = new SqlDataAdapter("Load_inventory_in_Pos", conn);
        adp.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        adp.SelectCommand.Parameters.Add("@CategorID", System.Data.SqlDbType.VarChar, 50).Value = id;
        adp.SelectCommand.Parameters.Add("@Operation", System.Data.SqlDbType.VarChar, 50).Value = "LoadItems";
        System.Data.DataTable dt = new System.Data.DataTable();
        adp.Fill(dt);
        ProductsWrapPanel.Children.Clear();
        foreach (System.Data.DataRow rowvar in dt.Rows)
        {
            //Here Creating Button on Runtime.

            Button button = new Button();
            string quantity = rowvar["Current_Stock"].ToString();
            if (quantity != "" && quantity != null)
            {
                button.Content = Environment.NewLine + "Quantity:[" + rowvar["Current_Stock"].ToString() + "]" + Environment.NewLine + "." + rowvar[2].ToString() + ".";
                button.Height = 150;
                button.Width = 150;
                ProductsWrapPanel.Children.Add(button);
                button.Margin = new Thickness(10, 10, 0, 0);
                new SolidColorBrush(Color.FromArgb(0, 166, 161, 16));
                button.BorderThickness = new Thickness(5);
                button.VerticalContentAlignment = VerticalAlignment.Bottom;
                button.Click += (sender, EventArgs) => { btnNew_Click(sender, EventArgs, rowvar["Product_Code"].ToString()); };

                byte[] getImg = new byte[0];
                getImg = (byte[])rowvar["Image"];
                Image image = new Image();
                using (MemoryStream stream = new MemoryStream(getImg))
                {
                    image.Source = BitmapFrame.Create(stream,  BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                }
                var brush = new ImageBrush();
                brush.ImageSource = image.Source;
                button.Background = brush;
                data.Product_Name = rowvar[2].ToString();
                data.Quantity = quantity.ToString();

            }
        }
我得到的是:

我需要的是这个


提前感谢。

实际上,您根本不需要通过编程来调整图像的大小。您可以定义
样式
,然后在创建时将其分配给
按钮
,并在
样式的
控制模板
中调整大小:

<Application.Resources>
        <Style x:Key="style_MyButton" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" >
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Button HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
                            <Grid >
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                            <Border Background="{TemplateBinding Background}" />
                            <TextBlock Grid.Row="1"
                                           Text="{TemplateBinding Content}"
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                           Margin="{TemplateBinding Padding}" 
                                           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Grid>
                        </Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
结果:


实际上,您根本不需要通过编程来调整图像的大小。您可以定义
样式
,然后在创建时将其分配给
按钮
,并在
样式的
控制模板
中调整大小:

<Application.Resources>
        <Style x:Key="style_MyButton" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" >
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Button HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
                            <Grid >
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                            <Border Background="{TemplateBinding Background}" />
                            <TextBlock Grid.Row="1"
                                           Text="{TemplateBinding Content}"
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                           Margin="{TemplateBinding Padding}" 
                                           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Grid>
                        </Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
结果:


我需要以编程方式解决问题。我刚刚在xaml中使用了一个流布局面板。并用我在这里发布的代码填充它。看看是否适合你。对不起。但这毫无帮助。因为在那里做的每件事都是以完全不同的方式完成的。我需要以编程的方式解决我的问题。我刚刚在xaml中使用了一个流布局面板。并用我在这里发布的代码填充它。看看是否适合你。对不起。但这毫无帮助。因为在那里做的每件事都是完全不同的。