在ListView中添加内容时,垂直展开列

时间:2017-05-02 17:21:01

标签: c# wpf listview visual-studio-2015

我正在为音乐目录的XML API创建一个Windows WPF界面。

因此,在创建专辑,歌曲,播放列表等列表时,我创建了带列的列表框(使用visual studio,因为我没有Windows编程经验):

<Window x:Class="AmpacheWPF.Programacion"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:AmpacheWPF"
    mc:Ignorable="d"
    Title="Programacion" Height="917.833" Width="1206.666">
<Grid>
    <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="26,39,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox_SelectionChanged"/>
    <ListView x:Name="albums" HorizontalAlignment="Left" Height="146" Margin="26,99,0,0" VerticalAlignment="Top" Width="549" SelectionChanged="albums_SelectionChanged">

        <ListView.View >
            <GridView AllowsColumnReorder="False">
                <GridView.ColumnHeaderContextMenu>
                    <ContextMenu/>
                </GridView.ColumnHeaderContextMenu>
                <GridViewColumn Header="Album"
                                DisplayMemberBinding="{Binding Album}" Width="120"/>
                <GridViewColumn Header="Artist"
                                DisplayMemberBinding="{Binding Artist}"/>
                <GridViewColumn Header="Year"
                                DisplayMemberBinding="{Binding Year}"/>
            </GridView>
        </ListView.View>
    </ListView>
    <Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="160,54,0,0" VerticalAlignment="Top"/>
    <ListView x:Name="Songs" HorizontalAlignment="Left" Height="257" Margin="26,286,0,0" VerticalAlignment="Top" Width="549" SelectionChanged="Songs_SelectionChanged">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="#"
                                DisplayMemberBinding="{Binding Pista}"/>
                <GridViewColumn Header="Cancion"
                                DisplayMemberBinding="{Binding Titulo}"/>
                <GridViewColumn Header="Compositor"
                                DisplayMemberBinding="{Binding Compositor}"/>
                <GridViewColumn Header="Artista"
                                DisplayMemberBinding="{Binding Artist}"/>
                <GridViewColumn Header="Tiempo"
                                DisplayMemberBinding="{Binding Tiempo}"/>
                <GridViewColumn Header="Genero"
                                DisplayMemberBinding="{Binding Genero}"/>
            </GridView>
        </ListView.View>
    </ListView>

</Grid>

它有专栏(专辑,艺术家,年) 所有信息都使用我的音乐服务器的XML API自动填充。

我有一个带有流派(拉丁语,萨尔萨语等)的组合框

每次点击一个类型时,它都会加载ListView专辑中的信息,当我点击一个专辑时,它会填充ListView歌曲。

我没有成功的尝试是修复列的大小(这是我唯一能用“宽度”做的事情)并希望列在文本不适合时垂直展开列宽(就像Microsoft excel一样)(所以我有2行或3行),但请告诉我在代码中的位置,因为我没有在Windows上编程的经验。

以下是我的代码的一些示例,以防它有用:

public Programacion()
    {
        InitializeComponent();
        serverurl = Loginwindow.serverurl;
        string ampuser = Loginwindow.ampuser;
        token = Loginwindow.token;

        //obteniendo lista de tags
        string tagurl = "http://" + serverurl + "/server/xml.server.php?action=tags&auth=" + token;
        string[] tagarray = { tagurl, "1", "tag", "name", "albums" };//Creando array con datos a utilizar
        tagsinfo = xmlcs2.Xmlparser(tagarray);//Llamando afuncion que lee XML

        //Obteniendo total de espacios musicales contando el array
        totalgenres = tagsinfo.GetUpperBound(0);

        //Lennado ComboBox1
        int g = Programacion.totalgenres;
        for (int y = 0; y < g; y++)
        {
            comboBox.Items.Add(tagsinfo[y][1]); //
        }

    }

    private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int index = comboBox.SelectedIndex;
        id = tagsinfo[index][0];
        string totalalbums = tagsinfo[index][2];


        //obteniendo lista de albums
        string tagurl = "http://" + serverurl + "/server/xml.server.php?action=tag_albums&auth=" + token + "&filter=" + id;
        string[] tagarray = { tagurl, totalalbums, "album", "name", "tracks", "artist", "year" };//Creando array con datos a utilizar
        albumsinfo = xmlcs2.Xmlparser(tagarray);//Llamando afuncion que lee XML

        //ordenando alfabeticamente
        Sort(albumsinfo, 1);

        //Lennado ListView Albums
        albums.ItemsSource = null;
        int g = Convert.ToInt32(totalalbums);

        List<Albumlist> items = new List<Albumlist>();
        for (int y = 0; y < g; y++)
        {

            if (albumsinfo[y][4] == "0")
            {

                items.Add(new Albumlist() { Album = albumsinfo[y][1], Artist = albumsinfo[y][3], Year = "" });


            }
            else
            {
                items.Add(new Albumlist() { Album = albumsinfo[y][1], Artist = albumsinfo[y][3], Year = albumsinfo[y][4], Id = albumsinfo[y][0] });



            }



        }
        albums.ItemsSource = items;


    }

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

看起来你需要做的是按照this answer更改GridView for CellTemplate中的DisplayMemberBinding,这样你就可以将presenter对象自定义为包装的自定义TextBlock(这也会做关于让网格布局知道需要更多空间的正确方法。)

您可能无法立即获得Excel行为,并且您可能需要使用其他TextBlock设置来使其按照您想要的方式工作。