Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# 更改复选框时如何排序ListView_C#_Xamarin.forms_Xamarin.android - Fatal编程技术网

C# 更改复选框时如何排序ListView

C# 更改复选框时如何排序ListView,c#,xamarin.forms,xamarin.android,C#,Xamarin.forms,Xamarin.android,我正在尝试编写一个购物清单应用程序。当您更改复选框时,它应该重新排列列表,使未选中的项位于顶部,选中的项位于底部 当我尝试对CheckedChanged事件进行排序时,它陷入了无限循环,因为在排序时CheckedChange事件似乎会触发 我还尝试使用两个单独的列表,一个保存旧值,另一个保存当前值并绑定到listview。这将导致以下错误并隐藏一些标签: requestLayout() improperly called by md5f92e0daf340890c9667469657ee2ece

我正在尝试编写一个购物清单应用程序。当您更改复选框时,它应该重新排列列表,使未选中的项位于顶部,选中的项位于底部

当我尝试对CheckedChanged事件进行排序时,它陷入了无限循环,因为在排序时CheckedChange事件似乎会触发

我还尝试使用两个单独的列表,一个保存旧值,另一个保存当前值并绑定到listview。这将导致以下错误并隐藏一些标签:

requestLayout() improperly called by md5f92e0daf340890c9667469657ee2ece8.LabelRenderer
我正在使用此代码获取错误:

async void CheckChange(object sender, CheckedChangedEventArgs e)
{
    CheckBox checkbox = (CheckBox)sender;
    var selectedProduct = checkbox.BindingContext as Product;

    if (selectedProduct == null)
    {
        return;
    }

    var oldProduct = _oldProducts.First(x => x.ProductId == selectedProduct.ProductId);
    if (oldProduct.IsChecked == selectedProduct.IsChecked)
    {
        return;
    }


    listView.ItemsSource = _products.OrderBy(x => x.IsChecked);
    listView.ItemsSource = new ObservableCollection<Product>(_products.OrderBy(x => x.ProductId).ToList());
    await _connection.UpdateAsync(selectedProduct);

    var prod = _oldProducts.First(x => x.ProductId == selectedProduct.ProductId);
    prod.IsChecked = selectedProduct.IsChecked;

}
async void CheckChange(对象发送方,CheckedChangedEventArgs e)
{
复选框=(复选框)发件人;
var selectedProduct=checkbox.BindingContext作为产品;
如果(selectedProduct==null)
{
返回;
}
var oldProduct=\u oldProducts.First(x=>x.ProductId==selectedProduct.ProductId);
如果(oldProduct.IsChecked==selectedProduct.IsChecked)
{
返回;
}
listView.ItemsSource=\u products.OrderBy(x=>x.IsChecked);
listView.ItemsSource=新的ObservableCollection(_products.OrderBy(x=>x.ProductId.ToList());
等待连接。更新同步(所选产品);
var prod=\u oldProducts.First(x=>x.ProductId==selectedProduct.ProductId);
prod.IsChecked=selectedProduct.IsChecked;
}

您可以绑定模型的属性IsChecked的值,并在代码隐藏中处理逻辑

在xaml中 在构造函数中,选择contentPage或ViewModel

您可以绑定模型的属性IsChecked的值,并在代码隐藏中处理逻辑

在xaml中 在构造函数中,选择contentPage或ViewModel

使用MessagingCenter会有点过分,你不觉得吗?我的意思是,这正是你不应该使用它的原因!使用MessagingCenter会有点过分,你不觉得吗?我的意思是,这正是你不应该使用它的原因!
<CheckBox IsChecked="{Binding IsCheck , Mode=TwoWay}"  /> 
public class Product : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public int Id { get; set; }

    public string Title { get; set; }

    private bool ischeck;
    public bool IsCheck
    {
        get
        {
            return ischeck;
        }

        set
        {
            if (ischeck != value)
            {
                ischeck = value;
                NotifyPropertyChanged();

            }
        }

    }
}
foreach(var product in MyItems) // MyItems here is ItemsSource of your listview
{
   product.PropertyChanged += Product_PropertyChanged;
}

//...

private void Product_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
  if(e.PropertyName== "IsCheck")
  {
     var newSources = MyItems.OrderBy(x => x.IsCheck);

     listView.ItemsSource = newSources;
  }
}