TOC

This article has been localized into Danish by the community.

Kontroller til tekstformattering:

Avanceret FlowDocument indhold

Som jeg allerede har nævnt, er mulighederne for tekstpræsentation i WPF og FlowDocument meget avanceret - du kan næsten gøre alting, og dette omfatter ting som lister, billeder og endda tabeller. Indtil nu har vi brugt meget simple eksemplere på FlowDocument indhold, men i denne artikel, vil vi endelig lave et mere omfattende eksempel.

XAML koden til det næste eksempel kan virke en smule overvældende, men bemærk hvor simpelt, det faktisk er - ganske som HTML kan du formattere tekst simpelthen ved at placere dem i stykker med typografier. Kig nu på XAML. Et skærmbillede af resultatet vil følge umiddelbart efter:

<Window x:Class="WpfTutorialSamples.Rich_text_controls.ExtendedFlowDocumentSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ExtendedFlowDocumentSample" Height="550" Width="500">
    <Grid>
        <FlowDocumentScrollViewer>
            <FlowDocument>
                <Paragraph>
                    <Image Source="http://www.wpf-tutorial.com/images/logo.png" Width="90" Height="90" Margin="0,0,30,0" />
                    <Run FontSize="120">WPF</Run>
                </Paragraph>

                <Paragraph>
                    WPF, which stands for
                    <Bold>Windows Presentation Foundation</Bold>,
                    is Microsoft's latest approach to a GUI framework, used with the .NET framework.
                    Some advantages include:
                </Paragraph>

                <List>
                    <ListItem>
                        <Paragraph>
                            It's newer and thereby more in tune with current standards
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            Microsoft is using it for a lot of new applications, e.g. Visual Studio
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            It's more flexible, so you can do more things without having to write or buy new controls
                        </Paragraph>
                    </ListItem>
                </List>

                <Table CellSpacing="0">
                    <TableRowGroup>
                        <TableRow Background="Gainsboro" FontWeight="Bold">
                            <TableCell></TableCell>
                            <TableCell>
                                <Paragraph TextAlignment="Right">WinForms</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph TextAlignment="Right">WPF</Paragraph>
                            </TableCell>
                        </TableRow>
                    </TableRowGroup>

                    <TableRowGroup>
                        <TableRow>
                            <TableCell Background="Gainsboro" FontWeight="Bold">
                                <Paragraph>Lines of code</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph TextAlignment="Right">1.718.000</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph TextAlignment="Right">1.542.000</Paragraph>
                            </TableCell>
                        </TableRow>
                    </TableRowGroup>
                    <TableRowGroup>
                        <TableRow>
                            <TableCell Background="Gainsboro" FontWeight="Bold">
                                <Paragraph>Developers</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph TextAlignment="Right">633.000</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph TextAlignment="Right">981.000</Paragraph>
                            </TableCell>
                        </TableRow>
                    </TableRowGroup>
                </Table>
                <Paragraph Foreground="Silver" FontStyle="Italic">A table of made up WinForms/WPF numbers</Paragraph>
            </FlowDocument>
        </FlowDocumentScrollViewer>
    </Grid>
</Window>

Jeg vil ikke gå for meget i detaljer med hver enkelt af taggene - forhåbentlig giver de mening, som de er.

Som du kan se, er anvendelse af lister, billeder og tabeller ret nemt, men faktisk kan du bruge en hvilken som helst WPF kontrol inde i FlowDocument. Ved at bruge BlockUIContainer elementet får du adgang til alle kontroller, der ellers kun ville være tilgængelige inde i et vindue. Her er et eksempel:

<Window x:Class="WpfTutorialSamples.Rich_text_controls.BlockUIContainerSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:self="clr-namespace:WpfTutorialSamples.Rich_text_controls"
        Title="BlockUIContainerSample" Height="275" Width="300">
    <Window.Resources>
        <x:Array x:Key="UserArray" Type="{x:Type self:User}">
            <self:User Name="John Doe" Age="42"/>
            <self:User Name="Jane Doe" Age="36"/>
        </x:Array>
    </Window.Resources>
    <Grid>
        <FlowDocumentScrollViewer>
            <FlowDocument>
                <Paragraph FontSize="36" Margin="0">Users</Paragraph>
                <Paragraph FontStyle="Italic" TextAlignment="Left" FontSize="14" Foreground="Gray">Here's a list of our users, inside our FlowDocument, in a completely interactive ListView control!</Paragraph>
                <BlockUIContainer>
                    <ListView BorderThickness="0" ItemsSource="{StaticResource UserArray}">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="150" />
                                <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="75" />
                            </GridView>
                        </ListView.View>
                    </ListView>
                </BlockUIContainer>
                <Paragraph FontStyle="Italic" TextAlignment="Left" FontSize="14" Foreground="Gray">More content can go here...</Paragraph>
            </FlowDocument>
        </FlowDocumentScrollViewer>
    </Grid>
</Window>

Nu har vi et FlowDocument med et ListView inden i det, og som du kan se på skærmbilledet, virker ListView helt, som det normalt ville, inklusive markeringer osv. Ret sejt!

Resume

Ved at bruge teknikkerne beskrevet i de to eksempler i denne artikel, er stort set alt muligt ved oprettelse af FlowDocument dokumenter. Det er fremragende til præsentation af visuel information til slutbrugeren, som man ser det i mange af de dyre rapportingssuiter.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!