Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在listviewitem UWP中折叠子控件_C#_Uwp_Win Universal App - Fatal编程技术网

C# 如何在listviewitem UWP中折叠子控件

C# 如何在listviewitem UWP中折叠子控件,c#,uwp,win-universal-app,C#,Uwp,Win Universal App,通用Windows平台,C# 如何从代码隐藏折叠/展开item MainListView listitem的子listview?我没有发现任何有效的方法。 我想在SelectionChanged事件上执行此操作 XAML 我会这样做的 我将创建两个数据模板,一个在选中(展开)时显示,另一个在未展开时显示 下面是我的视图模型 public class MyViewModel { public MyViewModel() { myItems = new Observa

通用Windows平台,C#

如何从代码隐藏折叠/展开item MainListView listitem的子listview?我没有发现任何有效的方法。 我想在SelectionChanged事件上执行此操作

XAML


我会这样做的

我将创建两个数据模板,一个在选中(展开)时显示,另一个在未展开时显示

下面是我的视图模型

public class MyViewModel
{
    public MyViewModel()
    {
        myItems = new ObservableCollection<MyItems>();
        for(int i=1;i<=10;i++)
        {
            MyItems item = new MyItems();
            item.Name = "Main Item " + i.ToString();
            ObservableCollection<MySubItems> subItems = new ObservableCollection<MySubItems>();
            for (int j=1;j<=5;j++)
            {
                subItems.Add(new MySubItems() { Title = "Sub Item " + j.ToString() });
            }
            item.Data = subItems;
            myItems.Add(item);
        }
    }

    public ObservableCollection<MyItems> myItems { get; set; }
}

public class MyItems
{
    public string Name { get; set; }
    public ObservableCollection<MySubItems> Data { get; set; }
}

public class MySubItems
{
    public string Title { get; set; }
}
这是输出


标签应该是UWP而不是WPF。这是一个惊人的例子。一个问题是,如何实现“MyViewModel”类。我是来自winforms和webforms的UWP新手。@BangBang我使用了
ViewModel
作为示例。如果您想知道ViewModel是如何工作的,您应该从MVVM开始。谢谢你提供的信息,我一直在研究MVVM。在使用示例代码的同时,是否可以显示xaml的其余部分以连接listview和binding ViewModel?@BangBang使用完整的MainPage.xaml更新了我的答案。
     private void listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //GET THE ITEM
        var selectItem = DestListView.Items[DestListView.SelectedIndex];

        //GET THE CHILD SOMEHOW
        //ListView childListView = (ListView)...not sure what to do here

        //if (childListView != null)
        //{
        //    if (childListView.Visibility == Visibility.Collapsed)
        //    {
        //        //childListView.Visibility = Visibility.Collapsed;
        //    }
        //    else
        //    {
        //        //childListView.Visibility = new Visibility;
        //    }
        //}

    }
public class MyViewModel
{
    public MyViewModel()
    {
        myItems = new ObservableCollection<MyItems>();
        for(int i=1;i<=10;i++)
        {
            MyItems item = new MyItems();
            item.Name = "Main Item " + i.ToString();
            ObservableCollection<MySubItems> subItems = new ObservableCollection<MySubItems>();
            for (int j=1;j<=5;j++)
            {
                subItems.Add(new MySubItems() { Title = "Sub Item " + j.ToString() });
            }
            item.Data = subItems;
            myItems.Add(item);
        }
    }

    public ObservableCollection<MyItems> myItems { get; set; }
}

public class MyItems
{
    public string Name { get; set; }
    public ObservableCollection<MySubItems> Data { get; set; }
}

public class MySubItems
{
    public string Title { get; set; }
}
<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.DataContext>
        <local:MyViewModel/>
    </Page.DataContext>

    <Page.Resources>
        <DataTemplate x:Key="NoSelectDataTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="SelectDataTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Name}" />
                <ListView ItemsSource="{Binding Data}" Grid.Row="1">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Title}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>
        </DataTemplate>
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding myItems}" SelectionChanged="ListView_SelectionChanged" ItemTemplate="{StaticResource NoSelectDataTemplate}">
            <ListView.ItemContainerStyle >
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="BorderThickness" Value="0,.5,0,0" />
                    <Setter Property="BorderBrush" Value="Gainsboro" />
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
    </Grid>
</Page>
private int PreviousIndex;
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListView lv = sender as ListView;

    if (PreviousIndex >=0)
    {
        ListViewItem prevItem = (lv.ContainerFromIndex(PreviousIndex)) as ListViewItem;
        prevItem.ContentTemplate = Resources["NoSelectDataTemplate"] as DataTemplate;
    }

    ListViewItem item = (lv.ContainerFromIndex(lv.SelectedIndex)) as ListViewItem;
    item.ContentTemplate = Resources["SelectDataTemplate"] as DataTemplate;

    PreviousIndex = lv.SelectedIndex;
}