0

DataTemplates.

这个答案中,EisenbergEffect 建议将数据模板提取到用户控件中。

如何做到这一点?

在我的用户控件中,我遇到了很多DataTemplates并遇到了这些问题。Conventions不适用,我必须使用 "classical" Binding

我只能想象DataTemplateDataTemplate.

这是一个示例 XAML

<Grid>
    <Grid.Resources />
    <Grid.RowDefinitions>
        <RowDefinition Height="10*" />
        <RowDefinition Height="2*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*" />
    </Grid.ColumnDefinitions>

    <Border HorizontalAlignment="Stretch"
            BorderBrush="Transparent"
            BorderThickness="0">
        <ScrollViewer HorizontalContentAlignment="Stretch"
                      Background="Yellow"
                      BorderBrush="Transparent"
                      BorderThickness="0"
                      CanContentScroll="True"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto">
              <!-- Conventions work here -->
            <ListView x:Name="Computers" 
                      HorizontalContentAlignment="Stretch"
                      Background="Red"
                      BorderThickness="0">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Border Background="Transparent"
                                BorderBrush="Transparent"
                                BorderThickness="0">
                            <ListView HorizontalContentAlignment="Stretch"
                                      Background="Black"
                                      ItemsSource="{Binding HardwareComponents}">  <!-- Conventions to not work here -->
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <Border Background="Aquamarine"
                                                BorderBrush="DarkGray"
                                                BorderThickness="1">
                                            <Grid Background="Transparent" cal:Message.Attach="[Event MouseDown] = [Action Expand($dataContext)]" >
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="20" />
                                                </Grid.RowDefinitions>
                                                <Border BorderBrush="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <!-- Even more DataTemplates come here -->
                                                </Border>
                                            </Grid>
                                        </Border>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </Border>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ScrollViewer>
    </Border>
</Grid>
4

0 回答 0