1

私はビューを持っています:

 <Grid>
    <Canvas Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch" x:Name="ImageHolder">
        <!-- there is something to do here !!! -->
        <!-- like 
             <ImageCollection>
             <DataTemplate For One Image>
                <Image Canvas.Left="{Binding Path=posX}" 
                       Canvas.Top="{Binding Path=posY}" 
                       Source="{Binding Path=fileName}"
                       x:Name="{Binding Path=fileName}"
                       MouseDown="Img_MouseDown" 
                       MouseUp="Img_MouseUp" /> 
             </DataTemplate For One Image>
             </ImageCollection> -->
     </Canvas>
 </Grid>

そして.csです

public partial class WindowBoard : Window
{

    protected MyCollectionVM _myCollection; // this class inherits of INotifyPropertyChanged

    public WindowBoard()
    {
        InitializeComponent();
        _myCollection = new MyCollectionVM();
    }
}

ViewModelClass で dataBinding を使用するために、この XAML に動的に画像を追加します。

つまり、1 つの dataTemplate イメージで userControl を作成する方法を知っていますが、多くのイメージでは動的に追加されます。

リストビューでそれを行う方法は知っていますが、キャンバスでそれを行う方法がわかりません.gridView/gridviewCellTemplateなどはありません...

4

2 に答える 2

1

ItemsControlは、にItemsPanel設定された状態で使用できます。Canvas

これにより、親コントロール(この場合はCanvas)が作成され、オブジェクトのコレクションをループして、各オブジェクトが親パネルに追加されます。ItemTemplateプロパティを使用してオブジェクトの描画方法を指定できます

ItemsControlラップは各アイテムをでラップするため、キャンバス自体ではなく、のキャンバスに<ContentPresenter>配置する必要があることに注意してください。ContentPresenterItemContainerStyleImage

終了コードは次のようになります。

<ItemsControl ItemsSource="{Binding MyCollectionOfImages}">
    <!-- ItemsPanelTemplate -->
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <!-- ItemContainerStyle -->
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Canvas.Left" Value="{Binding posX}" />
            <Setter Property="Canvas.Top" Value="{Binding posY}" />
        </Style>
    </ItemsControl.ItemContainerStyle>

    <!-- ItemTemplate -->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Image x:Name="{Binding Path=fileName}"
                   Source="{Binding Path=fileName}"
                   MouseDown="Img_MouseDown" 
                   MouseUp="Img_MouseUp" /> 
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
于 2013-03-15T14:45:09.570 に答える
0

この質問を参照してください

 <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Width="750" Height="350" 
            FontSize="16">
        <Grid>
            <ComboBox x:Name="cb" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" VerticalAlignment="Top"
                      DisplayMemberPath="Title" SelectedValuePath="Image">
            </ComboBox>
            <Canvas Margin="0,50,0,0" Width="100" Height="100" >
                <Canvas.Background>
                    <ImageBrush ImageSource="{Binding ElementName=cb, Path=SelectedValue}"/>
                </Canvas.Background>
            </Canvas>
        </Grid>
    </Window>

そしてc#で

using System.Collections.Generic;
using System.Windows;
using System.Linq;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            cb.DataContext = new[] 
            {
                new { Title="title1", Image=@"C:\img001.jpg" },
                new { Title="title2", Image=@"C:\img002.jpg" }
            };
        }
    }
}
于 2013-03-15T14:36:57.037 に答える