2014-01-30 75 views
1

我想设置一个图标附加到一个文本块,当它被保存,但我不知道我会如何编程这样做。我在想的是你可以设置值的文本框的左侧到image.I都试过,但我不知道我会如何将其设置为一个图像图标:如何以编程方式将图像添加到文本块?

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

这是我如何加入注释,布局:

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

有没有人有任何想法我会如何实现这一目标还是有可能在XAML代码中做?

这可能会给一个更好的理解我想要做的事:

How it currently looks: Trying to add an icon to the left of the note like in this picture:

回答

3

你可以把一个StackPanelTextBlock内,包含Image,另有TextBlock,像这样:

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

你然后能以编程方式设置图像像这样:

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

你确定'StackPanel'可以是'TextBlock'的子元素吗? –

+0

@MichaelPerrenoud,是的。我在发布代码之前对其进行了测试,以避免提供虚假信息的尴尬:) StackPanel绝对可以是“TextBlock”的子代。 –

相关问题