When using ItemsControl ItemsControl.ItemsPanel is set to Canvas, ContenPresenter comes in and break my Canvas properties on the children [WPF]

后端 未结 3 1894
故里飘歌
故里飘歌 2020-12-31 05:07

I am using an ItemsControl where the ItemsPanel is set to Canvas (see this question for more background information). The ItemsControl is performing as I want, and it works

相关标签:
3条回答
  • 2020-12-31 05:22

    It is possible to set the Canvas.Left property using ItemContainerStyle:

    <ItemsControl ItemsSource="{Binding Elements}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="Button Text" />
                </DataTemplate>     
            </ItemsControl.ItemTemplate>
        <ItemsControl.ItemContainerStyle>
            <Style>
                 <Setter Property="Canvas.Left" Value="500" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
    
    0 讨论(0)
  • 2020-12-31 05:22

    Button Doesn't have a "Canvas" Property, so what you are doing is is making a relative call to the hosting control, however because the item and canvas are in different Templates there is no direct link, because of this the Canvas.Left is meaningless before runtime.

    hence your method can't find a left to set so loses the change.

    However Setters are only implemented at runtime so

    <Setter Property="Canvas.Left" Value="500" /> 
    

    will only run after the objects have been generated and hence do have a relative relationship.

    Otherwise you can use the margin which does belong to the button object but again is only interpreted at runtime

    0 讨论(0)
  • 2020-12-31 05:31

    there are several solutions coming to my mind:

    1. use a layout/rendertransform instead of the attached property
    2. use margin instead of the attached property
    3. derive from ItemsControl, and override the behavior how the child containers are generated. (GetContainerForItemOverride, IsItemItsOwnContainerOverride). This article is explaining quite nicely how it works: http://drwpf.com/blog/2008/07/20/itemscontrol-g-is-for-generator/
    0 讨论(0)
提交回复
热议问题