コントロール テンプレートで Web から画像を読み込む際の問題
データベースからhtmlコンテンツを読み取ってページに表示しようとしています:
このコントロールのコントロール テンプレートを設計すると、generic.xaml は次のようになります。
<Style TargetType="q:ChoiceQuestion"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="q:ChoiceQuestion"> <RichTextBlock x:Name="QuestionTitleTextBlock" VerticalAlignment="Center"> <Paragraph x:Name="QuestionTitleParagraph"> <Run Text="{Binding OrderNumber}"></Run> </Paragraph> </RichTextBlock> </ControlTemplate> </Setter.Value> </Setter> </Style>
これらの画像を html コンテンツから取得して QuestionTitleParagraph に追加するので、次のコードを記述して OnApplyTemplate に画像を表示します:</p>
public override void OnApplyTemplate() { base.OnApplyTemplate(); // Get the image url from database, the following image is just for test ImageSource imageSource = new BitmapImage(new Uri("http://localhost:8081/uploadfile/2012/07/19/temp1.png",
UriKind.Absolute)); 画像 img = 新しい画像(); img.Source = imageSource; img.Width = 100; img.Height = 100;
// Add the image to QuestionTitleParagraph QuestionTitleParagraph = (Paragraph)base.GetTemplateChild("QuestionTitleParagraph"); InlineUIContainer container = new InlineUIContainer(); container.Child = img; QuestionTitleParagraph.Inlines.Add(container); }
私の問題は、ページに空白の画像しか表示されず、サイズが設計どおり (100*100) であることです。コントロール テンプレートに画像を動的に表示するにはどうすればよいですか? 画像の URL が有効であることは確かです。コントロール テンプレートではなく、richtextblock に表示できます。そして、画像の幅と高さを設定しないと、リッチテキストブロックにも表示できないことがわかりました。