簡體   English   中英

ItemsControl在ControlTemplate和ItemTemplate之間共享網格

[英]Itemscontrol sharing grid between controltemplate and itemtemplate

我有一個ItemsControl ,其中有很多列和一個靜態標題行。

我知道HeaderedItemsControl但不確定它能否實現我想要的功能。

我目前擁有的是ControlTemplate用作標題和ItemsPresenter的容器,然后使用ItemTemplate來組織記錄。

目前,我在內容模板和項目模板中都有一個重復的網格,並且我想找到一個優雅的解決方案,在這兩個模板之間可以共享相同的網格,因此在編輯時不必更改兩件事。 模擬下面的樣子,實際控件還有更多列。 我已經讀過IsSharedSizeScopeSharedSizeGroup但是我認為這是行不通的,因為它僅在有兩個共享父元素的網格時才起作用(它們都必須在ControlTemplate內部)

<ItemsControl ItemsSource="{Binding SomeSource}">
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <StackPanel>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="Col1" />
                    <TextBlock Grid.Column="1" Text="Col2" />
                    <TextBlock Grid.Column="2" Text="Col3" />
                </Grid>
                <ItemsPresenter/>
            </StackPanel>
        </ControlTemplate>
    </ItemsControl.Template>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="ItemCol1" />
                <TextBlock Grid.Column="1" Text="ItemCol2" />
                <TextBlock Grid.Column="2" Text="ItemCol3" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我可能還做了一些不好的做法,所以請隨時給我指點。

我覺得您將ItemsSource的值設置為完全錯誤。

它看起來應該像這樣:

<ItemsControl ItemsSource="{Binding SomeSource}">

當我以您的示例運行時,對我來說一切正常。

如果您希望共享列大小,則可以使用SharedSizeGroup。 您必須將范圍設置為ItemsControl。

您對SharedSizeGroup的定義有誤。 無論網格放置在哪個位置,只要它們共享同一個父母就可以使用。 這是您的情況,因為ItemsControl是最頂層的控件,因此它是內部每個控件的父級。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM