zoukankan      html  css  js  c++  java
  • 2019-1-27-WPF-使用-ItemsPanel-修改方向

    title author date CreateTime categories
    WPF 使用 ItemsPanel 修改方向
    lindexi
    2019-1-27 21:8:9 +0800
    2019-01-11 19:53:56 +0800
    WPF

    在 WPF 很多时候都需要使用 ListView 但是默认的列表是垂直的,如果需要使用水平的,就需要使用 ItemsPanel 设置使用的类

    先添加一些代码到资源,下面就可以使用这里的资源

          <Grid.Resources>
                <XmlDataProvider x:Key="InventoryData" XPath="Books">
                    <x:XData>
                        <Books xmlns="">
                            <Book ISBN="0-7356-0562-9" Stock="in" Number="9">
                                <Title>XML in Action</Title>
                                <Summary>XML Web Technology</Summary>
                            </Book>
                            <Book ISBN="0-7356-1370-2" Stock="in" Number="8">
                                <Title>Programming Microsoft Windows With C#</Title>
                                <Summary>C# Programming using the .NET Framework</Summary>
                            </Book>
                            <Book ISBN="0-7356-1288-9" Stock="out" Number="7">
                                <Title>Inside C#</Title>
                                <Summary>C# Language Programming</Summary>
                            </Book>
                            <Book ISBN="0-7356-1377-X" Stock="in" Number="5">
                                <Title>Introducing Microsoft .NET</Title>
                                <Summary>Overview of .NET Technology</Summary>
                            </Book>
                            <Book ISBN="0-7356-1448-2" Stock="out" Number="4">
                                <Title>Microsoft C# Language Specifications</Title>
                                <Summary>The C# language definition</Summary>
                            </Book>
                        </Books>
                    </x:XData>
                </XmlDataProvider>
            </Grid.Resources>
    

    添加一个 ListView 然后通过 ItemTemplate 设置界面

           <ListView ItemsSource="{Binding Source={StaticResource InventoryData}, XPath=Book}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid Background="#5a5a5a" Margin="10,10,10,10">
                            <StackPanel Margin="2,2,2,2">
                                <TextBlock Text="{Binding XPath=Title}" />
                                <TextBlock Text="{Binding XPath=@ISBN}" />
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

    这里的 DataTemplate 传入的 DataContext 就是 ItemsSource 绑定的列表的每一项

    如绑定了 List 那么这里的 DataContext 就是 Foo 类

    于是在这里就可以通过绑定 DataContext 的属性绑定界面

    上面代码运行可以看到列表是垂直的

    如果需要修改为水平的,可以通过 ItemsPanel 修改

                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
    

    代码全部都在 xaml 写,因为神树说在后台写不好

        <Grid>
            <Grid.Resources>
                <XmlDataProvider x:Key="InventoryData" XPath="Books">
                    <x:XData>
                        <Books xmlns="">
                            <Book ISBN="0-7356-0562-9" Stock="in" Number="9">
                                <Title>XML in Action</Title>
                                <Summary>XML Web Technology</Summary>
                            </Book>
                            <Book ISBN="0-7356-1370-2" Stock="in" Number="8">
                                <Title>Programming Microsoft Windows With C#</Title>
                                <Summary>C# Programming using the .NET Framework</Summary>
                            </Book>
                            <Book ISBN="0-7356-1288-9" Stock="out" Number="7">
                                <Title>Inside C#</Title>
                                <Summary>C# Language Programming</Summary>
                            </Book>
                            <Book ISBN="0-7356-1377-X" Stock="in" Number="5">
                                <Title>Introducing Microsoft .NET</Title>
                                <Summary>Overview of .NET Technology</Summary>
                            </Book>
                            <Book ISBN="0-7356-1448-2" Stock="out" Number="4">
                                <Title>Microsoft C# Language Specifications</Title>
                                <Summary>The C# language definition</Summary>
                            </Book>
                        </Books>
                    </x:XData>
                </XmlDataProvider>
            </Grid.Resources>
    
            <ListView ItemsSource="{Binding Source={StaticResource InventoryData}, XPath=Book}">
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
    
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid Background="#5a5a5a" Margin="10,10,10,10">
                            <StackPanel Margin="2,2,2,2">
                                <TextBlock Text="{Binding XPath=Title}" />
                                <TextBlock Text="{Binding XPath=@ISBN}" />
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    

    代码 https://gitee.com/lindexi/lindexi_gd/blob/68cefabd097bf2f4fc35e3384f34e1dc622a67ad/PotrallTiscawMouger/PotrallTiscawMouger/MainWindow.xaml

  • 相关阅读:
    HashMap 和HashTable
    两种方式获得键盘录入
    打印流 printStream
    对象操作流--存储对象
    内存输出流
    序列流
    装饰设计模式
    递归
    IO流(使用指定的码表读写字符)
    IO-字符流 练习
  • 原文地址:https://www.cnblogs.com/lindexi/p/12086352.html
Copyright © 2011-2022 走看看