[UWP]当ListView的数据模板是另一个ListView时,那么如何更改内部ListView的选择模式? [英] [UWP]When data template of ListView is an another ListView, then how to change Selection mode of inner ListView?

查看:62
本文介绍了[UWP]当ListView的数据模板是另一个ListView时,那么如何更改内部ListView的选择模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,


我们使用ListView作为另一个listview的数据模板来扩展和折叠功能。我们需要改变内部列表视图的选择模式。但内部listView的选择模式无法在运行时
中更改,也无法绑定内部列表视图的选择模式


请帮助解决问题。

解决方案

< blockquote>

Hello Ramseem,


似乎可以使用Binding直接更改ListView.SelectionMode,如下所示。不幸的是,不清楚为什么你的代码不能很好地工作。 


Body of MainPage.xaml

< ; Page.Resources> 
< local:SelectionModeConverter x:Key =" SelectionModeConverter" />
< /Page.Resources>
< Grid Background =" {ThemeResource ApplicationPageBackgroundThemeBrush}">
< ListView ItemsSource =" {x:Bind Data}">
< ListView.ItemTemplate>
< DataTemplate x:DataType =" local:TreeItem">
< ListView ItemsSource =" {x:Bind Items}"的DisplayMemberPath = QUOT;姓名" SelectionMode =" {x:Bind SelectionMode,Mode = OneWay}">
< ListView.Header>
< StackPanel Orientation =" Horizo​​ntal">
< TextBlock Text =" {x:Bind Name}" VerticalAlignment = QUOT;中心" />
< ComboBox SelectedIndex =" {x:Bind SelectionMode,Mode = TwoWay,Converter = {StaticResource SelectionModeConverter}}"余量= QUOT; 8,0,0,0">
< ComboBoxItem>无< / ComboBoxItem>
< ComboBoxItem> Single< / ComboBoxItem>
< ComboBoxItem> Multiple< / ComboBoxItem>
< ComboBoxItem> Extended< / ComboBoxItem>
< / ComboBox>
< / StackPanel>
< /ListView.Header>
< / ListView>
< / DataTemplate>
< /ListView.ItemTemplate>
< / ListView>
< / Grid>

和C#代码。

 public sealed partial class MainPage:Page 
{
public MainPage()
{
this.InitializeComponent();
}
public ObservableCollection< TreeItem>数据{得到;私人集; } = new ObservableCollection< TreeItem>()
{
new TreeItem(" Group1")
{
Items =
{
new TreeItem(" ; Item1"),
new TreeItem(" Item2"),
new TreeItem(" Item3"),
}
},
new TreeItem(" ; Group2")
{
Items =
{
new TreeItem(" Item1"),
new TreeItem(" Item2"),
new TreeItem(" Item3"),
}
}
};
}


公共类TreeItem:INotifyPropertyChanged
{
公共事件PropertyChangedEventHandler PropertyChanged;

public TreeItem(string name)
{
this.Name = name;
}
public string Name {get;私人集; }

private ListViewSelectionMode _selectionMode = ListViewSelectionMode.Single;
public ListViewSelectionMode SelectionMode
{
get {return _selectionMode; }
set
{
if(value!= _selectionMode)
{
_selectionMode = value;
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(" SelectionMode"));
}
}
}
public ObservableCollection< TreeItem>物品{get;私人集; } = new ObservableCollection< TreeItem>();
}


公共类SelectionModeConverter:IValueConverter
{
公共对象转换(对象值,类型targetType,对象参数,字符串语言)
{
switch((ListViewSelectionMode)value)
{
case ListViewSelectionMode.None:
return 0;
case ListViewSelectionMode.Single:
默认值:
返回1;
case ListViewSelectionMode.Multiple:
return 2;
case ListViewSelectionMode.Extended:
return 3;
}
}
公共对象ConvertBack(对象值,类型targetType,对象参数,字符串语言)
{
switch((int)value)
{
case 0:
返回ListViewSelectionMode.None;
case 1:
默认值:
返回ListViewSelectionMode.Single;
case 2:
返回ListViewSelectionMode.Multiple;
case 3:
返回ListViewSelectionMode.Extended;
}
}
}


如果您需要进一步的帮助,请详细说明您的尝试。或者,如果此帖子足以解决您的问题,请点击"标记为答案";下面关闭你的问题。谢谢。


Sir,

We use ListView as data template of another listview for expanding and collapsing feature. We need to change theselection mode of inner listview. But Selection mode of inner listView is not able to change in Run time and also not able to bind the Selection mode of inner listview

Please help to solve the scenario.

解决方案

Hello Ramseem,

It seems ListView.SelectionMode can be changed fairly straightforwardly with Binding as follows. So it's not clear why your code doesn't work well, unfortunately. 

Body of MainPage.xaml

    <Page.Resources>
        <local:SelectionModeConverter x:Key="SelectionModeConverter"/>
    </Page.Resources>
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{x:Bind Data}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:TreeItem">
                    <ListView ItemsSource="{x:Bind Items}" DisplayMemberPath="Name" SelectionMode="{x:Bind SelectionMode, Mode=OneWay}">
                        <ListView.Header>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{x:Bind Name}"  VerticalAlignment="Center"/>
                                <ComboBox SelectedIndex="{x:Bind SelectionMode, Mode=TwoWay, Converter={StaticResource SelectionModeConverter}}" Margin="8,0,0,0">
                                    <ComboBoxItem>None</ComboBoxItem>
                                    <ComboBoxItem>Single</ComboBoxItem>
                                    <ComboBoxItem>Multiple</ComboBoxItem>
                                    <ComboBoxItem>Extended</ComboBoxItem>
                                </ComboBox>
                            </StackPanel>
                        </ListView.Header>
                    </ListView>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>

And C# code.

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        public ObservableCollection<TreeItem> Data { get; private set; } = new ObservableCollection<TreeItem>()
        {
            new TreeItem("Group1")
            {
                Items =
                {
                    new TreeItem("Item1"),
                    new TreeItem("Item2"),
                    new TreeItem("Item3"),
                }
            },
            new TreeItem("Group2")
            {
                Items =
                {
                    new TreeItem("Item1"),
                    new TreeItem("Item2"),
                    new TreeItem("Item3"),
                }
            }
        };
    }


    public class TreeItem : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public TreeItem(string name)
        {
            this.Name = name;
        }
        public string Name { get; private set; }

        private ListViewSelectionMode _selectionMode = ListViewSelectionMode.Single;
        public ListViewSelectionMode SelectionMode
        {
            get { return _selectionMode; }
            set
            {
                if (value != _selectionMode)
                {
                    _selectionMode = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectionMode"));
                }
            }
        }
        public ObservableCollection<TreeItem> Items { get; private set; } = new ObservableCollection<TreeItem>();
    }


    public class SelectionModeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            switch ((ListViewSelectionMode)value)
            {
                case ListViewSelectionMode.None:
                    return 0;
                case ListViewSelectionMode.Single:
                default:
                    return 1;
                case ListViewSelectionMode.Multiple:
                    return 2;
                case ListViewSelectionMode.Extended:
                    return 3;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            switch ((int)value)
            {
                case 0:
                    return ListViewSelectionMode.None;
                case 1:
                default:
                    return ListViewSelectionMode.Single;
                case 2:
                    return ListViewSelectionMode.Multiple;
                case 3:
                    return ListViewSelectionMode.Extended;
            }
        }
    }

If you need further help, please describe what you'd tried in detail. Or otherwise, if this post is enough to solve your problem, click "Mark as answer" below to close your question. Thank you.


这篇关于[UWP]当ListView的数据模板是另一个ListView时,那么如何更改内部ListView的选择模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆