2016-11-24 2 views
0

Ich mache eine Anwendung in Wpf. Ich bin kürzlich auf ein Problem gestoßen. Ich möchte die Größe des Bildes ändern, das als Hintergrundbild einer Schaltfläche angezeigt wird, die während der Laufzeit programmgesteuert erstellt wird. Mehr Presizely möchte ich die Höhe des Bildes verringern. Was ich bisher getan haben, ist:Wie man das Hintergrundbild eines Knopfes in wpf ändert

 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(); 

      } 
     } 

, was ich habe ist: this Image

Und das, was ich brauche ist.

Required Image.

Vielen Dank im Voraus.

+0

ich brauche mein Problem programmatisch zu lösen. Ich habe gerade ein Flussdiagramm in xaml verwendet. und bevölkere es mit dem Code, den ich hier gepostet habe. –

+0

Sehen Sie, ob [this] (http://stackoverflow.com/questions/15779564/resize-image-in-wpf) für Sie arbeitet. –

+0

Entschuldigung. Aber das war überhaupt nicht hilfreich. Weil alles, was dort gemacht wird, völlig anders ist. –

Antwort

2

Eigentlich müssen Sie das Bild hier nicht programmgesteuert skalieren. Sie können Ihre Button ein Style und dann definieren zuweisen, wenn Sie es schaffen, und kümmern sich um die Größenänderung in der ControlTemplate des Style:

<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> 

dann die Code-Behind:

 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; 

Ergebnis :

enter image description here

Verwandte Themen