I've been trying to make a Custom search field that, on the fly, should add objects to a list when typing.
But for some reason it only shows the list with an item when i hot reload.
CreateHerdPageViewModel
public class CreateHerdPageViewModel : ViewModelBase
{
private IHerdService herdService;
private string searchInput;
public string SearchInput { get => searchInput;
set {
SetProperty(ref searchInput, value);
RaisePropertyChanged(nameof(HerdSearchResults));
}
}
private List<Herd> herdSearchResults;
public List<Herd> HerdSearchResults
{
get => herdSearchResults;
set {
SetProperty(ref herdSearchResults, value);
}
}
private List<Herd> allHerds;
public List<Herd> AllHerds { get => allHerds; set => SetProperty(ref allHerds, value); }
public DelegateCommand SearchChrOrAddressCommand { get; set; }
public CreateHerdPageViewModel(INavigationService navigationService, IHerdService herdService)
: base(navigationService)
{
this.herdService = herdService;
SearchChrOrAddressCommand = new DelegateCommand(SearchChrOrAddress);
}
private void SearchChrOrAddress()
{
Herd herdMatch = new Herd();
for (int i = 0; i < AllHerds.Count; i++)
{
herdMatch = AllHerds[i];
}
if (herdMatch.ChrAddress.Area.Contains(SearchInput))
{
if (HerdSearchResults.Contains(herdMatch) == false)
{
HerdSearchResults.Add(herdMatch);
RaisePropertyChanged(nameof(HerdSearchResults));
}
}
}
public async override void OnNavigatedTo(INavigationParameters parameters)
{
base.OnNavigatedTo(parameters);
AllHerds = await herdService.GetHerds();
HerdSearchResults = new List<Herd>();
}
}
}
CreateHerdPage.Xaml
xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
xmlns:CustomRenderer="clr-namespace:ChrApp.CustomRenderer">
<yummy:PancakeView
Grid.Column="0"
Grid.Row="0"
Grid.ColumnSpan="3"
CornerRadius="10">
<CustomRenderer:NoUnderlineEntry
x:Name="SearchField"
Style="{StaticResource UpdateEntry}"
Margin="0"
TextChanged="RemovceSearchIcon"
Text="{Binding SearchInput}">
<CustomRenderer:NoUnderlineEntry.Behaviors>
<b:EventToCommandBehavior
EventName="TextChanged"
Command="{Binding SearchChrOrAddressCommand}"/>
</CustomRenderer:NoUnderlineEntry.Behaviors>
</CustomRenderer:NoUnderlineEntry>
</yummy:PancakeView>
As you can see, i've tried different approaches to make it recognize changes, but without luck.
Can someone enlighten me on what i'm missing?
Thanks in advance
So the issue was that I used an ordinary List, and not an ObservableCollection. So swithcing to this solved the issue.
Related
I have an employee table. and location field in employee table.
I have to use combobox to filter that. If I choose "A location" in combobox only A location people should come in screen if I choose B location only B location people should come in screen.
It's my xaml Entries and ComboBox.ParticularEntries is my all entries (A and B locations together)
Initialized ParticularEntries like that:
private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
get { return _particularEntries; }
set { Set(ref _particularEntries, value); }
}
And EntryReportParticular Model Class:
public class EntryReportParticular : BindableItem
{
private Employee _employee;
public Employee Employee
{
get { return _employee; }
set { Set(ref _employee, value); }
}
private DateTime _entry;
public DateTime Entry
{
get { return _entry; }
set { Set(ref _entry, value, () => OnPropertyChanged(nameof(Duration))); }
}
private DateTime _exit;
public DateTime Exit
{
get { return _exit; }
set { Set(ref _exit, value, () => OnPropertyChanged(nameof(Duration))); }
}
public TimeSpan Duration { get { return Exit - Entry; } }
private Region _region;
public Region Region
{
get { return _region; }
set { Set(ref _region, value); }
}
}
It's my xaml ParticularEntries
<DataGrid
ItemsSource="{Binding ParticularEntries}"
AutoGenerateColumns="False"
IsReadOnly="True"
RowHeaderWidth="0"
GridLinesVisibility="All"
HorizontalGridLinesBrush="WhiteSmoke"
VerticalGridLinesBrush="WhiteSmoke"
Margin="4">
And It's my combobox with command.
<ComboBox
ItemsSource="{Binding Locations}"
SelectedItem ="{Binding SelectedLocation}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding LocationFilterCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
And It's my related part of ViewModel:
ComboBox:
private string _selectedLocation;
public string SelectedLocation
{
get { return _selectedLocation; }
set
{
_selectedLocation = value;
OnPropertyChanged("SelectedLocation");
Trace.WriteLine(SelectedLocation);
}
}
private ObservableCollection<string> _locations;
public ObservableCollection<string> Locations
{
get { return _locations; }
set
{
_locations = value;
OnPropertyChanged("Locations");
}
}
public EntryReportViewModel()//Constructor
{
Locations = new ObservableCollection<string>()
{
"A Location","B Location"
};
}
LocationFilterCommand(to filtered according to location without button)
#region LocationFilterCommand
private DelegateCommand _locationFilterCommand;
public DelegateCommand LocationFilterCommand
{
get { return _locationFilterCommand ?? (_locationFilterCommand = new DelegateCommand(CanLocationFilter, LocationFilter)); }
}
private bool CanLocationFilter()
{
if (ParticularEntries == null || DailyEntries == null || MonthlyEntries == null)
return false;
return true;
}
private void LocationFilter()
{
ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
MonthlyEntries.Select(pg => pg.Employee.CostCenter.Location == _selectedLocation);
}
#endregion
I did that. I have ComboBox with A and B locations but when I choose A or B location anything changed.How can I fix this and how can I filtered according to location? What should I change in UI or others to do that?
Your code in LocationFilter make no sense at all.
ParticularEntries.Select(pg => pg.Region.Location == _selectedLocation);
It returns an IEnumerable<bool> but it is never assigned.
If you want to filter, you have to use Where.
But even if you change your code to
ParticularEntries = ParticularEntries.Where(pg => pg.Region.Location == _selectedLocation);
you will see a change, but you will face the next problem next time when you select a different location.
Solution
You need a collection with all unfiltered items stored inside a private field and use that for filtering.
private IEnumerable<EntryReportParticular> _allEntries;
private IEnumerable<EntryReportParticular> _particularEntries;
public IEnumerable<EntryReportParticular> ParticularEntries
{
get { return _particularEntries; }
set { Set(ref _particularEntries, value); }
}
private void LocationFilter()
{
ParticularEntries = _allEntries
.Where(pg => pg.Region.Location == _selectedLocation)
.ToList();
}
I have defined the ReactiveCommand like this:
CmdGetTestResult = ReactiveCommand.CreateFromTask<bool>(async detailed =>
{
var result = await Client.FooAsync(detailed);
});
CmdGetTestResult.ToPropertyEx(this, x => x.GetTestResultResponse);
viewmodel
public bool Detailed { get; set; }
public Unit GetTestResultResponse { [ObservableAsProperty]get; }
public ReactiveCommand<bool, Unit> CmdGetTestResult { get; }
In xaml:
<Button
Content="get test result (F4)"
Command="{Binding Path=ViewModel.CmdGetTestResult}"
CommandParameter="{Binding Path=ViewModel.Detailed}"/>
The property detailed is true (checked in debugger), but it is always passed as false. What's wrng with my code?
EDIT:
I have also tried the reactiveui way:
this.BindCommand(ViewModel, model => model.CmdGetTestResult, window => window.ButtonGetTestResult, model => model.Detailed);
without any success and same problem..
just been bumping my head and appears it's like that:
private readonly ObservableAsPropertyHelper<List<LocationData>> _locations;
public ReactiveCommand<LocationData, List<LocationData>> GetLocations { get; }
public ViewModel
{
GetLocations = ReactiveCommand.CreateFromTask<LocationData, List<LocationData>>(GetLocationsTask);
}
private async Task<List<LocationData>> GetLocationsTask(LocationData i)
{
return await LocationService.GetLocationsAsync(i);
}
this is just a tester...
I currently have a ListBox binded to a collection of items. As the collection is big we want to filter the items being shown based on the text entered on a TextBox.
What I'm asking is if this is possible to implement using only XAML, I don't want to modify the collection of items, I would like to modify the Visibility of each of the items based on the filter.
Hope its clear,
thanks!
Like CodeNaked and devdigital told you CollectionViewSource/CollectionView/ICollectionView
are the keys to your goal
It's a MVVM patter but this is a View only related problem so I don't
want this code at the ViewModel.
thats not the right way because the View only shows what she get´s but shouldn´t modifi
so it should/must be your ViewModel who handel changes
so now some code snips:
public class myVM
{
public CollectionViewSource CollViewSource { get; set; }
public string SearchFilter
{
get;
set
{
if(!string.IsNullOrEmpty(SearchFilter))
AddFilter();
CollViewSource.View.Refresh(); // important to refresh your View
}
}
public myVM(YourCollection)
{
CollViewSource = new CollectionViewSource();//onload of your VM class
CollViewSource.Source = YourCollection;//after ini YourCollection
}
}
Xaml Snip:
<StackPanel>
<TextBox Height="23" HorizontalAlignment="Left" Name="tB" VerticalAlignment="Top"
Width="120" Text="{Binding SearchFilter,UpdateSourceTrigger=PropertyChanged}" />
<DataGrid Name="testgrid" ItemsSource="{Binding CollViewSource.View}"/>
</StackPanel>
Edit i forgot the Filter
private void AddFilter()
{
CollViewSource.Filter -= new FilterEventHandler(Filter);
CollViewSource.Filter += new FilterEventHandler(Filter);
}
private void Filter(object sender, FilterEventArgs e)
{
// see Notes on Filter Methods:
var src = e.Item as YourCollectionItemTyp;
if (src == null)
e.Accepted = false;
else if ( src.FirstName !=null && !src.FirstName.Contains(SearchFilter))// here is FirstName a Property in my YourCollectionItem
e.Accepted = false;
}
You can use the CollectionViewSource to apply filtering, another example can be found here and here.
You can do this with a CollectionViewSource. You wouldn't want to do this completely in XAML, as it would be much easier to test this if the filtering code is in your view model (assuming an MVVM design pattern).
There is no way to accomplish this in XAML only. But there are other two ways:
1) using converter
<TextBox x:Name="text"/>
<ListBox Tag="{Binding ElementName=text}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=Tag, Converter={StaticResource filterLogicConverter}}"/>
</Style>
</ListBox.ItemContainerStyle>
<LixtBox/>
2) better and more natural way is to use CollectionView.Filter property. It doesn't modify an underlying collection.
var collectionView = CollectionViewSource.GetDefaultView(your_collection);
collectionView.Filter = filter_predicate
The only thing XAML really does is encapsulating logic in a declarative fashion. Using markup extensions you can do quite a lot, here's an example:
<StackPanel>
<StackPanel.Resources>
<CollectionViewSource x:Key="items" Source="{Binding Data}">
<CollectionViewSource.Filter>
<me:Filter>
<me:PropertyFilter PropertyName="Name"
RegexPattern="{Binding Text, Source={x:Reference filterbox}}" />
</me:Filter>
</CollectionViewSource.Filter>
</CollectionViewSource>
</StackPanel.Resources>
<TextBox Name="filterbox" Text="Skeet">
<TextBox.TextChanged>
<me:ExecuteActionsHandler ThrowOnException="false">
<me:CallMethodAction>
<me:CallMethodActionSettings MethodName="Refresh"
TargetObject="{Binding Source={StaticResource items}}" />
</me:CallMethodAction>
</me:ExecuteActionsHandler>
</TextBox.TextChanged>
</TextBox>
<!-- ListView here -->
</StackPanel>
(Note that this works but it will trip every GUI designer, also there is no IntelliSense for the events as they usually are not set via element syntax.)
There are several markup extensions here of which two create handlers and one creates an action:
FilterExtension
ExecuteActionsHandlerExtension
CallMethodActionExtension
The extensions look like this:
[ContentProperty("Filters")]
class FilterExtension : MarkupExtension
{
private readonly Collection<IFilter> _filters = new Collection<IFilter>();
public ICollection<IFilter> Filters { get { return _filters; } }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new FilterEventHandler((s, e) =>
{
foreach (var filter in Filters)
{
var res = filter.Filter(e.Item);
if (!res)
{
e.Accepted = false;
return;
}
}
e.Accepted = true;
});
}
}
public interface IFilter
{
bool Filter(object item);
}
Quite straightforward, just loops through filters and applies them. Same goes for the ExecuteActionsHandlerExtension:
[ContentProperty("Actions")]
public class ExecuteActionsHandlerExtension : MarkupExtension
{
private readonly Collection<Action> _actions = new Collection<Action>();
public Collection<Action> Actions { get { return _actions; } }
public bool ThrowOnException { get; set; }
public ExecuteActionsHandlerExtension()
{
ThrowOnException = true;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new RoutedEventHandler((s, e) =>
{
try
{
foreach (var action in Actions)
{
action.Invoke();
}
}
catch (Exception)
{
if (ThrowOnException) throw;
}
});
}
}
Now the last extension is a bit more complicated as it actually needs to do something concrete:
[ContentProperty("Settings")]
public class CallMethodActionExtension : MarkupExtension
{
//Needed to provide dependency properties as MarkupExtensions cannot have any
public CallMethodActionSettings Settings { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new Action(() =>
{
bool staticCall = Settings.TargetObject == null;
var argsCast = Settings.MethodArguments.Cast<object>();
var types = argsCast.Select(x => x.GetType()).ToArray();
var args = argsCast.ToArray();
MethodInfo method;
if (staticCall)
{
method = Settings.TargetType.GetMethod(Settings.MethodName, types);
}
else
{
method = Settings.TargetObject.GetType().GetMethod(Settings.MethodName, types);
}
method.Invoke(Settings.TargetObject, args);
});
}
}
public class CallMethodActionSettings : DependencyObject
{
public static readonly DependencyProperty MethodNameProperty =
DependencyProperty.Register("MethodName", typeof(string), typeof(CallMethodActionSettings), new UIPropertyMetadata(null));
public string MethodName
{
get { return (string)GetValue(MethodNameProperty); }
set { SetValue(MethodNameProperty, value); }
}
public static readonly DependencyProperty TargetObjectProperty =
DependencyProperty.Register("TargetObject", typeof(object), typeof(CallMethodActionSettings), new UIPropertyMetadata(null));
public object TargetObject
{
get { return (object)GetValue(TargetObjectProperty); }
set { SetValue(TargetObjectProperty, value); }
}
public static readonly DependencyProperty TargetTypeProperty =
DependencyProperty.Register("TargetType", typeof(Type), typeof(CallMethodActionSettings), new UIPropertyMetadata(null));
public Type TargetType
{
get { return (Type)GetValue(TargetTypeProperty); }
set { SetValue(TargetTypeProperty, value); }
}
public static readonly DependencyProperty MethodArgumentsProperty =
DependencyProperty.Register("MethodArguments", typeof(IList), typeof(CallMethodActionSettings), new UIPropertyMetadata(null));
public IList MethodArguments
{
get { return (IList)GetValue(MethodArgumentsProperty); }
set { SetValue(MethodArgumentsProperty, value); }
}
public CallMethodActionSettings()
{
MethodArguments = new List<object>();
}
}
All of these snippets are just quick drafts to demonstrate how one could approach this. (A draft for the property filter implementation can be found in this answer.)
Use a data trigger on some property of the item in the collectin and you can do it all in xaml.
I have bound a GridView with an ICollectionView in the XAML designer the properties are not known because the entity in the CollectionView have been transformed into type Object and the entity properties can't be accessed, it runs fine no error but the designer shows it as an error, if I bind to the collection I can access the properties fine
Example the entity is a Person with a string Name property I place them in an ObservableCollection<Person> and get the view from it and bind it to the GridView.ItemsSource now when I try to set the column header DataMemberBinding.FirstName property the designer shows it as an error
Cannot Resolve property 'FirstName' in data Context of type object
Is it a bug or is it Resharper playing tricks on me
Sample code:
public class Person
{
public string FirstName{
get { return _firstName; }
set { SetPropertyValue("FirstName", ref _firstName, value); }
}
}
public class DataService
{
public IDataSource DataContext { get; set; }
public ICollectionView PersonCollection{ get; set; }
public DataService()
{
DataContext = new DataSource();
//QueryableCollectionView is from Telerik
//but if i use any other CollectionView same thing
//DataContext Persons is an ObservableCollection<Person> Persons
PersonCollection = new QueryableCollectionView(DataContext.Persons);
}
}
<telerik:RadGridView x:Name="ParentGrid"
ItemsSource="{Binding DataService.PersonCollection}"
AutoGenerateColumns="False">
<telerik:RadGridView.Columns >
<telerik:GridViewDataColumn Header="{lex:Loc Key=FirstName}"
DataMemberBinding="{Binding FirstName}"/>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
The warnings that Resharper is giving you in the XAML view is because the design-time view of the control does not know what type it's data-context is. You can use a d:DesignInstance to help with your bindings.
Add the following (replacing Assembly/Namespace/Binding Target names appropriately)
<UserControl x:Class="MyNamespace.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup‐compatibility/2006"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lcl="clr‐namespace:MyAssembly"
d:DataContext="{d:DesignInstance Type=lcl:ViewModel}">
Your entity has not been transformed in object, it's because the interface ICollectionView is not a generic collection so ReSharper has no way to know that it holds a collection of Person.
You can create a generic version of ICollectionView and use it for your PersonCollection property as demonstrated in this post https://benoitpatra.com/2014/10/12/a-generic-version-of-icollectionview-used-in-a-mvvm-searchable-list/.
First some interfaces:
public interface ICollectionView<T> : IEnumerable<T>, ICollectionView
{
}
public interface IEditableCollectionView<T> : IEditableCollectionView
{
}
The implementation:
public class GenericCollectionView<T> : ICollectionView<T>, IEditableCollectionView<T>
{
readonly ListCollectionView collectionView;
public CultureInfo Culture
{
get => collectionView.Culture;
set => collectionView.Culture = value;
}
public IEnumerable SourceCollection => collectionView.SourceCollection;
public Predicate<object> Filter
{
get => collectionView.Filter;
set => collectionView.Filter = value;
}
public bool CanFilter => collectionView.CanFilter;
public SortDescriptionCollection SortDescriptions => collectionView.SortDescriptions;
public bool CanSort => collectionView.CanSort;
public bool CanGroup => collectionView.CanGroup;
public ObservableCollection<GroupDescription> GroupDescriptions => collectionView.GroupDescriptions;
public ReadOnlyObservableCollection<object> Groups => collectionView.Groups;
public bool IsEmpty => collectionView.IsEmpty;
public object CurrentItem => collectionView.CurrentItem;
public int CurrentPosition => collectionView.CurrentPosition;
public bool IsCurrentAfterLast => collectionView.IsCurrentAfterLast;
public bool IsCurrentBeforeFirst => collectionView.IsCurrentBeforeFirst;
public NewItemPlaceholderPosition NewItemPlaceholderPosition
{
get => collectionView.NewItemPlaceholderPosition;
set => collectionView.NewItemPlaceholderPosition = value;
}
public bool CanAddNew => collectionView.CanAddNew;
public bool IsAddingNew => collectionView.IsAddingNew;
public object CurrentAddItem => collectionView.CurrentAddItem;
public bool CanRemove => collectionView.CanRemove;
public bool CanCancelEdit => collectionView.CanCancelEdit;
public bool IsEditingItem => collectionView.IsEditingItem;
public object CurrentEditItem => collectionView.CurrentEditItem;
public event NotifyCollectionChangedEventHandler CollectionChanged
{
add => ((ICollectionView) collectionView).CollectionChanged += value;
remove => ((ICollectionView) collectionView).CollectionChanged -= value;
}
public event CurrentChangingEventHandler CurrentChanging
{
add => ((ICollectionView) collectionView).CurrentChanging += value;
remove => ((ICollectionView) collectionView).CurrentChanging -= value;
}
public event EventHandler CurrentChanged
{
add => ((ICollectionView) collectionView).CurrentChanged += value;
remove => ((ICollectionView) collectionView).CurrentChanged -= value;
}
public GenericCollectionView([NotNull] ListCollectionView collectionView)
{
this.collectionView = collectionView ?? throw new ArgumentNullException(nameof(collectionView));
}
public IEnumerator<T> GetEnumerator()
{
return (IEnumerator<T>) ((ICollectionView) collectionView).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((ICollectionView) collectionView).GetEnumerator();
}
public bool Contains(object item)
{
return collectionView.Contains(item);
}
public void Refresh()
{
collectionView.Refresh();
}
public IDisposable DeferRefresh()
{
return collectionView.DeferRefresh();
}
public bool MoveCurrentToFirst()
{
return collectionView.MoveCurrentToFirst();
}
public bool MoveCurrentToLast()
{
return collectionView.MoveCurrentToLast();
}
public bool MoveCurrentToNext()
{
return collectionView.MoveCurrentToNext();
}
public bool MoveCurrentToPrevious()
{
return collectionView.MoveCurrentToPrevious();
}
public bool MoveCurrentTo(object item)
{
return collectionView.MoveCurrentTo(item);
}
public bool MoveCurrentToPosition(int position)
{
return collectionView.MoveCurrentToPosition(position);
}
public object AddNew()
{
return collectionView.AddNew();
}
public void CommitNew()
{
collectionView.CommitNew();
}
public void CancelNew()
{
collectionView.CancelNew();
}
public void RemoveAt(int index)
{
collectionView.RemoveAt(index);
}
public void Remove(object item)
{
collectionView.Remove(item);
}
public void EditItem(object item)
{
collectionView.EditItem(item);
}
public void CommitEdit()
{
collectionView.CommitEdit();
}
public void CancelEdit()
{
collectionView.CancelEdit();
}
}
And finally the usage:
ICollectionView<Person> PersonCollectionView { get; }
In the constructor:
var view = (ListCollectionView) CollectionViewSource.GetDefaultView(PersonCollection);
PersonCollectionView = new GenericCollectionView<Person>(view);
Neither
d:DataContext="{d:DesignInstance Type=lcl:ViewModel}">
nor
GenericCollectionView
works directly for a DataGrid with a CollectionViewSource.
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding collectionViewSource.View}"
SelectedItem="{Binding SelectedRow}"
We can't set "d:DataContext"; because, we often need to bind multiple properties to our viewmodel.
The CollectionViewSource creates new ListCollectionView which is runtime instantiated each time you set the Source property. Since setting the Source property is the only reasonable way to refresh a range of rows, we can't keep a GenericCollectionView around.
My solution is perhaps perfectly obvious, but I dumped the CollectionViewSource. By making creating a property
private ObservableCollection<ListingGridRow> _rowDataStoreAsList;
public GenericCollectionView<ListingGridRow> TypedCollectionView
{
get => _typedCollectionView;
set { _typedCollectionView = value; OnPropertyChanged();}
}
public void FullRefresh()
{
var listData = _model.FetchListingGridRows(onlyListingId: -1);
_rowDataStoreAsList = new ObservableCollection<ListingGridRow>(listData);
var oldView = TypedCollectionView;
var saveSortDescriptions = oldView.SortDescriptions.ToArray();
var saveFilter = oldView.Filter;
TypedCollectionView = new GenericCollectionView<ListingGridRow>(new ListCollectionView(_rowDataStoreAsList));
var newView = TypedCollectionView;
foreach (var sortDescription in saveSortDescriptions)
{
newView.SortDescriptions.Add(new SortDescription()
{
Direction = sortDescription.Direction,
PropertyName = sortDescription.PropertyName
});
}
newView.Filter = saveFilter;
}
internal void EditItem(object arg)
{
var view = TypedCollectionView;
var saveCurrentPosition = view.CurrentPosition;
var originalRow = view.TypedCurrentItem;
if (originalRow == null)
return;
var listingId = originalRow.ListingId;
var rawListIndex = _rowDataStoreAsList.IndexOf(originalRow);
// ... ShowDialog ... DialogResult ...
var lstData = _model.FetchListingGridRows(listingId);
_rowDataStoreAsList[rawListIndex] = lstData[0];
view.MoveCurrentToPosition(saveCurrentPosition);
view.Refresh();
}
After adding
public T TypedCurrentItem => (T)collectionView.CurrentItem;
To the GenericCollectionView provided by Maxence.
I can't get why this works (binds a single Car to RadDataForm):
xaml:
<tk:RadDataForm ItemsSource="{Binding Path=Cars}"
AutoGenerateFields="True" DataContext="{Binding}" />
viewmodel:
public void OnNavigatedTo(NavigationContext navigationContext)
{
carId = int.Parse(navigationContext.Parameters["IdRecord"]);
Cars= _carContext.GetCarById(carId);
}
private IEnumerable<Car> cars;
public IEnumerable<Car> Cars
{
get { return this.cars; }
set
{
if (this.cars!= value)
{
this.cars= value;
this.RaisePropertyChanged(() => this.Cars);
}
}
}
and this not:
xaml:
<tk:RadDataForm CurrentItem="{Binding Path=CurrentCar}"
AutoGenerateFields="True" DataContext="{Binding}" />
viewmodel:
public void OnNavigatedTo(NavigationContext navigationContext)
{
carId = int.Parse(navigationContext.Parameters["IdRecord"]);
CurrentCar= _carContext.GetCarById(carId).FirstOrDefault();
}
private Car currentCar;
public Car CurrentCar
{
get { return this.currentCar; }
set
{
if (this.currentCar!= value)
{
this.currentCar= value;
this.RaisePropertyChanged(() => this.CurrentCar);
}
}
}
I don't want a IEnumerable<> because I want to get a single Entity.
And, by the way, I want to understand what is going wrong...
When binding to a single entity you should use CurrentItem instead of ItemsSource.