0

ContentPresenter Content があり、ファイルから bmp をロードしました。bmp を ContentPresenter に表示して、ズーム機能を利用したいと考えています。

私が持っているコード (bmp ファイルのパスのみを表示する) は次のとおりです。

        BitmapImage bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.UriSource = new Uri(selectedFileName);
        bitmap.EndInit();
        Content = bitmap;
4

2 に答える 2

1

イメージをプリロードしたくない場合は、メイン ウィンドウに依存関係プロパティを作成する必要があります。WPF Pack URIも使用する必要があります。XAML と分離コード ファイルは次のとおりです。

XAML :

<Window x:Class="TestWPFApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWPFApp"
        Title="MainWindow" Height="550" Width="725">

    <Grid x:Name="MainGrid">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <Label Content="Select Image" Width="100" Height="30" Margin="10,10,100,10"></Label>
            <ComboBox x:Name="cbImageSelect" Height="20" Width="400" SelectionChanged="ComboBox_SelectionChanged" />
        </StackPanel>
        <ContentPresenter x:Name="contentPresenter" Width="250" Height="250" Grid.Row="1" >
            <ContentPresenter.Content>
                <Image Source="{Binding ImageUri}" Width="220" Height="220">
                </Image>
            </ContentPresenter.Content>
        </ContentPresenter>
    </Grid>
</Window>

XAML.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace TestWPFApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            cbImageSelect.ItemsSource = new List<string>() { "test1.bmp", "test2.bmp" };
        }

        public static readonly DependencyProperty ImageUriProperty = DependencyProperty.Register("ImageUri", typeof(string), typeof(MainWindow));

        public string ImageUri
        {
            get { return (string)GetValue(ImageUriProperty); }
            set { SetValue(ImageUriProperty, value); }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ImageUri = "pack://application:,,,/" + ((sender as ComboBox).SelectedItem as string);
        }
    }
}

画像の場所: test1.bmp および test2.bmp

ここに画像の説明を入力

test1.bmp と test2.bmp のプロパティ
ここに画像の説明を入力 ここに画像の説明を入力

于 2013-05-23T14:35:09.803 に答える
0

AContentPresenterは基本的に 2 つのことを行います: 要素を直接表示するか、 a で定義された方法でデータを表示しますDataTemplate(詳細については、MSDN ページのコメントを参照してください)。BitmapImageは要素ではなく、特定の DataTemplate が関連付けられていないため、ContentPresenter は単純にその要素を表示するようにフォールバックしToStringます。

要素を作成しImageてコンテンツを直接設定するか、DataTemplateContentTemplateDataType.

コンテンツ テンプレート:

<ContentPresenter>
    <ContentPresenter.ContentTemplate>
        <DataTemplate>
            <Image Source="{Binding}" />
        </DataTemplate>
    </ContentPresenter.ContentTemplate>
</ContentPresenter>

リソース:

<Window.Resources>
    <DataTemplate DataType="{x:Type BitmapImage}">
        <Image Source="{Binding}" />
    </DataTemplate>
</Window.Resources>
于 2013-05-23T15:02:44.077 に答える