【发布时间】:2016-11-24 16:42:55
【问题描述】:
我正在 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();
}
}
而我需要的是这个。
提前致谢。
【问题讨论】:
-
我需要以编程方式解决我的问题。我刚刚在 xaml 中使用了流布局面板。并用我在这里发布的代码填充它。
-
看看this是否适合你。
-
对不起。但这根本没有帮助。因为在那里所做的一切都完全不同。