我正在Wpf中创建一个应用程序。我最近遇到了一个问题。我想调整显示为在运行时以编程方式创建的按钮的背景图像的图像。 更准确地说我想降低Image的高度。 到目前为止我所做的是:
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();
}
}
我需要的是这个。
先谢谢。
答案 0 :(得分:2)
实际上,您根本不必在此处以编程方式调整图像大小。您可以定义Style
,然后在创建Button
时将其分配给ControlTemplate
,并在Style
的{{1}}中处理调整大小:
<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>
然后,代码隐藏:
Button button = new Button();
button.Content = "SomeTetx Some text";
button.Height = 150;
button.Width = 150;
grid.Children.Add(button);
button.Margin = new Thickness(10, 10, 0, 0);
button.BorderThickness = new Thickness(5);
// here is the important line:
button.Style = (Style)Application.Current.FindResource("style_MyButton");
Image image = new Image();
var source=new BitmapImage( new Uri (imagepath));
source.Freeze();
image.Source = source;
var brush = new ImageBrush();
brush.ImageSource = image.Source;
// this is how you can manipulate the stretch mode:
brush.Stretch = Stretch.UniformToFill;
button.Background = brush;
结果: