/ / Zoradiť pozorovateľné kolekcie podľa svojej vlastnosti - wpf, triedenie, pozorovateľné kolekcie

Zoradiť Obsaditeľný výber podľa jeho vlastnosti - wpf, triedenie, pozorovateľná kolekcia

Viem, že to už bolo požiadané, ale napriek tomuneviem zabaliť hlavu do akéhokoľvek riešenia, ktoré bude fungovať pre môj prípad. Chcem byť schopný zoradiť pozorovateľnú kolekciu v tomto projekte podľa ItemName. Tu je celý projekt https://gist.github.com/NewCoderNotInTown/322274bd7d2fd57bf2ae7784e1315b73.

cieľom je mať oba zoznamy, pôvodný aj kopírovaný, abecedne zoradené.

oceniť riešenie pre tento prípad. čo najjednoduchšie pre začiatočníkov.

I Upraviť otázku s kompletným kódom podľa odporúčania.

Kód pre MainWindow.xaml

<Window x:Class="TwoWindows.MainWindow"
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:WB="clr-namespace:TwoWindows"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Button Grid.Row="1" Content="Click Me" Width="80" Height="25"  Click="ClickMe_Click"/>
<TabControl Grid.Row="0" Margin="10">
<TabItem>
<TabItem.Header>
<StackPanel>
<TextBlock Text="WindowB"/>
</StackPanel>
</TabItem.Header>
<WB:WindowB x:Name="_windowB"/>
</TabItem>
</TabControl>
</Grid>

Kód pre MainWindow.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TwoWindows
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void ClickMe_Click(object sender, RoutedEventArgs e)
{
var windowBViewModel = new WindowBViewModel("WindowB");
var windowB = new WindowB();

var windowAViewModel = new WindowAViewModel("WindowA", windowBViewModel);
var windowA = new WindowA();

windowA.DataContext = windowAViewModel;
windowA.Show();

_windowB.DataContext = windowBViewModel;
}
}
}

Kód pre WindowA.xaml

<Window x:Class="TwoWindows.WindowA"
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:TwoWindows"
mc:Ignorable="d"
Title="WindowA" Height="300" Width="600">
<Grid>
<Label Content="{Binding PageTitle}" />
<ListBox ItemsSource="{Binding MyItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding ItemName}" IsChecked="{Binding IsChecked}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>

Kód pre WindowA.xaml.cs

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TwoWindows
{
public partial class WindowA : Window
{
public WindowA()
{
InitializeComponent();
}
}
}

Kód pre WindowB.xaml

<UserControl x:Class="TwoWindows.WindowB"
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:TwoWindows"
mc:Ignorable="d">
<Grid>
<Label Content="{Binding PageTitle}" />
<ListBox ItemsSource="{Binding MyItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ItemName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>

Kód pre WindowB.xaml.cs

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TwoWindows
{
public partial class WindowB : UserControl
{
public WindowB()
{
InitializeComponent();
}
}
}

Kód pre WindowAViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TwoWindows
{
public class WindowAViewModel : BaseWindowViewModel
{

public WindowAViewModel(string pageTitle, WindowBViewModel windowBViewModel)
{
PageTitle = pageTitle;

MyItems.Add(new MyCustomItemViewModel("Apple", windowBViewModel));
MyItems.Add(new MyCustomItemViewModel("Orange", windowBViewModel));
MyItems.Add(new MyCustomItemViewModel("Banana", windowBViewModel));
}
}
}

Kód pre WindowBViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TwoWindows
{
public class WindowBViewModel : BaseWindowViewModel
{
public WindowBViewModel(string pageTitle)
{
PageTitle = pageTitle;
}
}
}

Kód pre MyCustomItemViewModel.cs

using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TwoWindows
{
[ImplementPropertyChanged]
public class MyCustomItemViewModel
{
public string ItemName { get; set; }
public bool IsChecked { get; set; }
public WindowBViewModel WindowBViewModelObj { get; set; }

public MyCustomItemViewModel(string itemName, WindowBViewModel windowBViewModel)
{
ItemName = itemName;
WindowBViewModelObj = windowBViewModel;
}

private void OnIsCheckedChanged()
{
if (IsChecked)
WindowBViewModelObj.MyItems.Add(this);
else
WindowBViewModelObj.MyItems.Remove(this);
}
}
}

Kód pre BaseWindowViewModel.cs

 using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TwoWindows
{
public class BaseWindowViewModel
{
public string PageTitle { get; set; }

public ObservableCollection<MyCustomItemViewModel> MyItems { get; } = new ObservableCollection<MyCustomItemViewModel>();

}
}

odpovede:

1 pre odpoveď č. 1

Tu sú tri bežné spôsoby triedenia z vášho kódu.

  1. Ručné použitie zoradenia kedykoľvek pridáte položky. Navrhoval by som linq pre skutočné triedenie, aj keď si môžete vytvoriť svoj vlastný. V konkrétnom prípade by to vyzeralo takto:

    MyItems.Add(new MyCustomItemViewModel("Apple", windowBViewModel));
    MyItems.Add(new MyCustomItemViewModel("Orange", windowBViewModel));
    MyItems.Add(new MyCustomItemViewModel("Banana", windowBViewModel));
    
    MyItems = MyItems.Sort(p => p.ItemName);
    

    To predpokladá, že povolíte nastavovač tak MyItems nie je iba na čítanie.

  2. V getter pre MyItems, vráťte zbierku zoradenú (a použite podložkumajetok za to). To nie je v skutočnosti ideálne, pretože zostavujete triedenú kolekciu pre každé volanie, ktoré sa uskutoční getterovi. Pre váš kód by to vyzeralo takto:

    public class BaseWindowViewModel
    {
    public string PageTitle { get; set; }
    
    private ObservableCollection<MyCustomItemViewModel> _myItems = new ObservableCollection<MyCustomItemViewModel>()
    public ObservableCollection<MyCustomItemViewModel> MyItems
    {
    get { return _myItems.Sort(p => p.ItemName); }
    }
    }
    
  3. (Odporúča sa) Pracujte so zdrojom CollectionViewSource, ktorý je umiestnený na vrchu vašej zbierky a používa operácie, ktoré sú priateľské k používateľskému rozhraniu, napríklad triedenie a filtrovanie.

    public class BaseWindowViewModel
    {
    public string PageTitle { get; set; }
    
    private ObservableCollection<MyCustomItemViewModel> _myItems;
    public ObservableCollection<MyCustomItemViewModel> MyItems
    {
    get
    {
    if (_myItems == null)
    {
    _myItems = new ObservableCollection<MyCustomItemViewModel>();
    _myItemsSorted = CollectionViewSource.GetDefaultView(_myItems)
    _myItemsSorted.SortDescriptions.Add(new SortDescription() { PropertyName = "ItemName" });
    }
    return _myItems;
    }
    }
    
    private ICollectionView _myItemsSorted;
    public ICollectionView MyItemsSorted { get { return _myItemsSorted; }}
    

    }

    A potom sa len naviažte MyItemsSorted radšej než MyItems


0 pre odpoveď č. 2

Môžete to urobiť pomocou LINQ, ale musítekeď skončíte s pridávaním alebo odstraňovaním položiek, zavolajte Sort (). Ak sa to stane veľa s veľkými zoznamami, možno budete chcieť pozrieť do ObservableCollection.Insert a použiť svoj vlastný porovnávač.

public class BaseWindowViewModel
{
public string PageTitle { get; set; }

public ObservableCollection<MyCustomItemViewModel> MyItems
{
get
{
return _MyItems;
}
}
private ObservableCollection<MyCustomItemViewModel> _MyItems = new ObservableCollection<MyCustomItemViewModel>();

public void Sort()
{
_MyItems = new ObservableCollection<MyCustomItemViewModel>(from i in _MyItems orderby i.ItemName select i);
}
}