簡體   English   中英

WPF-使用ContentControl中父DataTemplate中的DataType

[英]WPF - Use DataType from parent DataTemplate inside ContentControl

所以我在UserControl.Resources中定義了一個DataTemplate,如下所示:

    <DataTemplate x:Key="Impact">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0"
                       Text="Impact:" />
            <ComboBox Grid.Column="1"
                      ItemsSource="{Binding Impacts}">
            </ComboBox>
        </Grid>
    </DataTemplate>

然后,在可視樹中有一個ItemsControl定義,如下所示:

<ItemsControl ItemsSource="{Binding Path=ViewModelsList}">
        <ItemsControl.Resources>
            <DataTemplate DataType="{x:Type ui:FirstViewModel}">
                    <ContentControl ContentTemplate="{StaticResource Impact}"></ContentControl>
            </DataTemplate>
            <DataTemplate DataType="{x:Type ui:SecondViewModel}">
                <ContentControl ContentTemplate="{StaticResource Impact}"></ContentControl>
                ...
            </DataTemplate>
        </ItemsControl.Resources>
    </ItemsControl>

有沒有一種方法可以將資源中的{Binding Impacts}與DataTemplate的DataType中定義的各個ViewModel類型相關聯?

也許有一種方法可以將DataType傳遞給ContentControl?

將此添加到您的ContentControls:

Content="{Binding}"

暫無
暫無

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

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