How to display items in Canvas through Binding

前端 未结 1 1152
盖世英雄少女心
盖世英雄少女心 2020-11-27 17:43

I have list of items that I want to display in Canvas using data binding.

ItemsToShowInCanvas = new ObservableCollection
   {
       new I         


        
相关标签:
1条回答
  • 2020-11-27 18:11

    Set the ItemsPanel to a Canvas and bind the containers instead of the TextBlock in the DataTemplate

    <ItemsControl ItemsSource="{Binding Path=ItemsToShowInCanvas}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="{Binding Left}"/>
                <Setter Property="Canvas.Top" Value="{Binding Top}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Text}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    
    0 讨论(0)
提交回复
热议问题