简体   繁体   中英

How to programatically add an image to a textblock?

I'm trying to set an icon to be attached to a textblock when it is saved but I'm not sure how I would do this programatically.What I'm thinking is you could set the value to the left of the textbox to an image.I have tried this but I'm not sure how I would set it to an image icon:

NoteGridView.SetValue(Canvas.LeftProperty(double)block.GetValue(Canvas.LeftProperty));

This is how I'm adding the notes to the layout:

// Creates a new textblock that for this note.
TextBlock block = new TextBlock();
//block.SetValue
notepadTxtBox.SetValue(Canvas.LeftProperty, (double)block.GetValue(Canvas.LeftProperty));
block.Width = 250;
block.Height = 125;
block.Text = notepadTxtBox.Text;


// Add that note to the grid.
NoteGridView.Items.Add(block);

Does anyone have any idea how I would acheive this or is it possible to do in the xaml code?

This might give a better understanding of what I'm trying to do:

它目前的样子:尝试在此图片中添加图标左侧的图标:

You could put a StackPanel inside of the TextBlock , containing an Image , and another TextBlock , like so:

<TextBlock>
    <StackPanel Orientation="Horizontal">
        <Image Name="YourImage" Source="image/Under-Construction.png" />
        <TextBlock Text="Your Text Block Text" />
    </StackPanel>
</TextBlock>

You would then be able to set the Image programmatically like so:

YourImage.Source = "path.to.image.file"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM