How can I position ViewModels in an ItemsControl

后端 未结 1 1768
醉话见心
醉话见心 2021-01-16 05:43

My mainwindow ViewModel has an ObservableCollection of ViewModels, called ViewModels.

The Mainwindow XAML has an ItemsControl with ItemsSource bound to ViewModels.

相关标签:
1条回答
  • 2021-01-16 06:09

    Make your ItemsControl.ItemsPanel into a Canvas, and set your Top/Left/Height/Width in the ItemTemplate.

    <ItemsControl ItemsSource="{Binding ViewModels}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <!-- Height, Width, and Background are required to render size correctly -->
                <Canvas Height="600" Width="800" Background="White" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="{x:Type FrameworkElement}">
                <Setter Property="Canvas.Top" Value="{Binding Top}" />
                <Setter Property="Canvas.Left" Value="{Binding Left}" />
                <Setter Property="Height" Value="{Binding Height}" />
                <Setter Property="Width" Value="{Binding Width}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ContentControl>
                    <ContentPresenter ClipToBounds="True" Content="{TemplateBinding Content}"/>
                </ContentControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    
    0 讨论(0)
提交回复
热议问题