[UWP]在UWP App中的GridViewItem IsSelected绑定 [英] [UWP]GridViewItem IsSelected binding in UWP App

查看:59
本文介绍了[UWP]在UWP App中的GridViewItem IsSelected绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我们要求显示图像 在GridView中逐步增加。因此,要在GridView中查找所选项,GridView项的IsSelected属性已与CLR对象的相应绑定对象属性绑定(GridView
ItemSource类型的属性)。由于,UWP不支持RelativeSouce和setter绑定风格,在互联网上进行一些搜索后我们发现了以下代码。

We have a requirement to display the images  in a GridView incrementally. So to find the selected items in GridView , IsSelected property of the GridView item has bind with corresponding binding objects property of CLR object (property of the GridView ItemSource type). Since, UWP does not support RelativeSouce and setter binding in style , after doing some search on internet we found the below code.

 

public
class
GridViewEx :GridView

{

        
受保护
< span style ="color:blue; font-family:Consolas"> override
void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject元素,
object item)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

           
base .PrepareContainerForItemOverride(element,item);

           
var gridItem = element
as GridViewItem;

           
var binding =
new Binding {Mode = BindingMode.TwoWay,Source = item,Path =
new PropertyPath( " IsSelected" )};

            gridItem.SetBinding(SelectorItem.IsSelectedProperty,binding);

       &NBSP; }

}

但似乎上述方法存在缺陷。每当我们向下滚动并加载下一组图像时,之前选择的项目就会失去选择。

But it seems that there is a flaw with the above approach. Whenever we are scrolling down and loading next set of images the previous selected items are losing their selection.

推荐答案

Hell Venkata,

Hell Venkata,

看来你已经得到了以下SO线程的回答:

It seems you've already got an answer from the following SO thread:

http://stackoverflow.com/questions/ 36547056 / gridviewitem-isselected-binding-in-uwp-app

你试过试试吗?

或者您有一个简单的代码示例,我们可以根据您现有的代码帮助您修改。

Or do you have a simple code sample that we can help you modify based on your exist code.

例如,对于上面的usercontrol部分stackoverflow线程,它可以是以下代码:

For example, for usercontrol part on above stackoverflow thread, it can be the following code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace GridViewSelectItem
{
    public sealed partial class MyUserControl1 : UserControl
    {   

        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(MyUserControl1), new PropertyMetadata(false));



        public MyUserControl1()
        {
            this.InitializeComponent();
            this.DataContext = this;
        }

    }
    public class myconverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value is Visibility && (Visibility)value == Visibility.Visible;

        }
    }
}

致以最诚挚的问候,

Barry


这篇关于[UWP]在UWP App中的GridViewItem IsSelected绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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