Adding image to shortcut in wpf?

I am currently developing an application in C # using WPF. What I need to do is make the shortcut to the left of the shortcut text a small X or a small checkmark, as the case may be. I have images included in a project in an image folder.

How can I assign images to be placed to the left of the label programmatically in code, rather than using XAML code.

+5
source share
3 answers

Since you want it to be in code, not in XAML, I suggest disabling Labeland using it StackPanelin combination with Imageand TextBlock, as shown below, where it MyGridcan be any container ...

        <Grid Name="MyGrid"/>

... then in your code behind ...

        StackPanel myStackPanel = new StackPanel();
        myStackPanel.Orientation = Orientation.Horizontal;

        Image myImage = new Image();
        BitmapImage myImageSource = new BitmapImage(); 
        myImageSource.BeginInit();
        myImageSource.UriSource = new Uri("Images/MyImage.png");
        myImageSource.EndInit();
        myImage.Source = myImageSource;

        TextBlock myTextBlock = new TextBlock();
        myTextBlock.Text = "This is my image";

        myStackPanel.Children.Add(myImage);
        myStackPanel.Children.Add(myTextBlock);

        MyGrid.Children.Add(myStackPanel);
+2
source

You can group this inside the grid:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>

  <Image Grid.Column="0" Source="{Binding ImageSourceProperty}" />
  <Label Grid.Column="1" Content="{Binding LabelTextProperty}" />
</Grid>

Or, since the label is a content control, you can simply place the image control inside the label control:

<Label>
  <Image Source="{Binding ImageSourceProperty}" />
  My Text
</Label>

Once you know what xaml should look like, it is very easy to create the same elements with code.

+4
source

. . .

xaml , .

<StackPanel Name="myStack" Orientation="Horizontal"></StackPanel>

, ,

Image coolPic = new Image() { 
    Name="pic", 
    Source = new BitmapImage(new Uri("pack://application:,,,/images/cool.png")), 
    Stretch = Stretch.None  // this preserves the original size, fill would fill
}; 

TextBlock text = new TextBlock() { 
    Name = "myText", 
    Text = "This is my cool Pic" 
};

myStack.Children.Add(coolPic); // adding the pic first places it on the left
myStack.Children.Add(text);    // the text would show up to the right

, , .

, , .

, .

, , ,

((TextBlock)FindName("myText")).Text = "my other cool pic";

, , ?

Object reference not set to an instance of an object.

Drats, .

// register the new control
RegisterName(text.Name, text);

, . , . , .

Image coolPic = new Image() { 
    Name="pic", 
    Source = new BitmapImage(new Uri("pack://application:,,,/images/cool.png")), 
    Stretch = Stretch.None // this preserves the original size, fill would fill
};

// register the new control
RegisterName(coolPic.Name, coolPic);

TextBlock text = new TextBlock() {
    Name = "myText",
    Text = "This is my cool Pic" 
};

// register the new control
RegisterName(text.Name, text);

myStack.Children.Add(coolPic);
myStack.Children.Add(text);
+1

All Articles