CurrentItem not set when using paging - c#

I have a DataGrid with CurrentItem bound on a property. DataGrid has implemented Paging.
If I select items in the first page, CurrentItem changes and everything is fine. However, when I set another page... I change Observable collection to display another 30 rows, the CurrentItem binding does not sork.
I have:
<DataGrid CurrentItem="{Binding CurrentItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Orders}">

I'm not sure, I'm following your question - but, I'll take a wild guess :-). I'm guessing your problem is that the CurrentItem is no longer set (and no row is selected in the DataGrid) unless you manually select a new item in the DataGrid? If that is the case, you simply need to add a property in your DataGrid:
<DataGrid IsSynchronizedWithCurrentItem="True"/>
This will force the DataGrid to select the first item when data is reloaded.
Another wild guess:
When you set the new source - how do you do that? if you update your property with a new ObservableCollection without calling a PropertyChanged event your binding will no longer work (but then, your DataGrid shouldn't update either).
Hope, either of those help - otherwise, I need more info to help you :-).

Related

How to attach a property in WPF/XAML with NULL as initial value

I have a datagrid and I want to show some details of the currently selected row in some textboxes above the datagrid. I am working with Telerik's grid and have SelectionUnit=Mixed and SelectionMode=Extended. Thus SelectedItem and SelectedItems are always null.
My working solution is that I created an attached property which provides the values of that row if only cells from one row are selected and dummy values if cells from multiple values are selected.
that part of the grid is defined as:
<telerik:RadGridView b:myBehavior.CurrentRow="{Binding Path=Data.SelectedRow, Source={StaticResource DataContextProxy}}" Name="myGridView" ...>
and the values are referenced in the textbox with this code:
<TextBox Text="{Binding Path=(b:myBehavior.CurrentRow).TextValue, ElementName=myGridView}" />
As you can see I had to create a binding do a property in my viewModel. Is there a way to initalize the attached property without using the viewmodel? Either by providing a dummy record or NULL?
If I set b:myBehavior.CurrentRow="" I get an exception (invalid value). what do i need to do here to get it running?
Thanks to #Clemens for pointing me into the right direction. My solution to avoid an unnecessary roundtrip to the viewmodel was to create an object in the Usercontrols resources
<repositories:FahrzeugFlatRecord x:Key="myObj" />
and then bind it like so:
<telerik:RadGridView b:myBehavior.CurrentRow="{Binding Source={StaticResource myObj}}"...>
the TextBox remains unchanged. This solution was necessary as I have a PropertyChangedCallback in my behavior that registers an EventListener. If this was not the case, it would be sufficient to call to bind that behavior:
<telerik:RadGridView b:myBehavior.CurrentRow="{x:Null}"...>

IsAsync =true on ComboBox ItemsSource affects SelectedItem?

I've spent hours trying to figure out what is happening to my selecteditem property of my ComboBoxes and Im pretty sure I know what is causing the problem now, but I just dont understand why this is happening and if there is something I can do about it.
Basically I have a DataGrid with comboboxes in the headers that I use for filtering the DataGrid items. The DataGrid sits in a Tabcontrol and if I select items in the comboxes and switch tab and back the selected items are removed.
This happens only when the ItemsSource of the Comboboxes have the IsAsync property set to True. Otherwise everything works just like I want it to.
To give some more info about the logic:
The comboboxes get their values from a IValueConverter its the same converter for all comboboxes and I pass a ConverterParameter to tell it what to return. The collection sent to the converter is the items shown in the datagrid.
These values are refreshed (each item has selected parameters updated, the collection is not cleared and recreated) when the tab is activated.
This my xaml for one of the comboboxes:
<ComboBox SelectedItem="{Binding MadeBy}" ItemsSource="{Binding IssuesView,Converter={StaticResource DataGridFilterableValueConverter}",ConverterParameter=Madeby, IsAsync=true}" SelectionChanged="FilterComboBox_SelectionChanged"/>
Now, like i described previously, if I set the IsAsync to false the selecteditems stay.
Do I just have to live with this? We work alot with sql-databases and IsAsync really makes the interface alot more snappy. I can live without it in the comboboxes, but I tend to use it as much as I can.
I've seen that ppl have been having problems with the selecteditem and the general solution seems to be placing the SelectedItem before the ItemsSource, but this does not help.
Thanks
Erik
Edit 1:
Here is the xaml of the tabcontrol and tabitem.
<TabControl Grid.Row="1" x:Name="TabControl" SelectionChanged="TabControl_SelectionChanged">
<TabItem Header="Granskningssynpunkter" x:Name="IssueTab">
..... Lots of code here but nothing that concerns TabControl or TabItem

WPF: Bind to SelectedItem of row in DataGrid

I'm very new to WPF. I'm trying to bind to a property a row in a DataGrid so that when the row's clicked the property is set. The ItemsSource that's bound to the DataGrid is an ObservableCollection of objects of type Field.
I've tried to bind to the SelectedItem attribute on the DataGrid, but the property is not being called. I'm using almost identical code to bind to the SelectedItem of a ComboBox and this is working fine. Is there a difference that I don't know about?
<ComboBox ItemsSource="{Binding RecordTypes}" SelectedItem="{Binding SelectedRecordType}" ...
<DataGrid ItemsSource="{Binding Fields}" SelectedItem="{Binding SelectedField}" ...
In my ViewModel:
private Field SelectedField
{
get
{
return _selectedField;
}
set
{
_selectedField = value;
}
}
(I will use auto properties later, it's just currently set up like this so that I could break when the property was set).
I'm not sure if it makes a difference, but the DataGrid is composed of 2 DataGridTextColumns and a DataGridTemplateColumn, which contains a checkbox.
Does anyone have any ideas? I'd really appreciate any suggestions.
To confirm, the reason that I want to listen to the click of a row is so that I can have the checkbox be checked whenever a row is clicked. If there's a better solution for this then please let me know.
You need to make it a two-way binding:
SelectedItem="{Binding SelectedField,Mode=TwoWay}"
That propagates changes in the view (user selects an item, SelectedItem changes) back to the viewmodel ("SelectedField" property).
Also, as #KevinDiTraglia pointed out, you need to make sure that the viewmodel property SelectedField is public, not private, otherwise the binding will not be able to access the getter/setter.

Changing Data Context for Data Template clears Combo Box selection

I have a TreeView that permits the user to select different items. The display for each item is determined using Data Templates with the DataType set to the appropriate ViewModel type. The DataContext is automatically set based on the selected item in the tree view to the appropriate ViewModel.
Here's the problem:
One of the DataTemplates has a ComboBox bound to an ObservableCollection to get the list of items and a property to get/set the SelectedValue in the ViewModel.
When I select one item of this type, and then select another item of the same type, the ComboBox displays blank instead of the correct selected item. It appears that the Combo Box is setting the SelectedValue property to NULL immediately after the transition to the new item, and then never updating.
<ComboBox Margin="1,0"
ItemsSource="{Binding ItemsToSelect}"
SelectedValue="{Binding SelectedValue}"
SelectedValuePath="ValuePath" DisplayMemberPath="DisplayPath"
IsEnabled="{Binding CanSelectItem}">
</ComboBox>
The really strange part is if I select an item of a different type between selecting items of the same type, it always displays correctly.
I've tried ignoring the NULL value in the SelectedValue setter, and that didn't work regardless of whether I also raised the PropertyChanged event or not.
private MyObject selectedValue;
public MyObject SelectedValue
{
get
{
return selectedValue;
}
set
{
if (value != null)
{
this.selectedValue = value;
}
this.OnPropertyChanged("SelectedValue");
}
}
Looking at the similar questions while writing this lead me to an interesting attribute that I hadn't found yet - IsSynchronizedWithCurrentItem from this question. At first, I thought this solved the problem, but alas it just changes the behavior somewhat.
With this attribute set to True, the combo doesn't entirely clear its selection, but instead just marks the first item as selected. So, instead of being set to NULL, now the SelectedValue property is being set to the first item in the list.
Anyone have any ideas for a solution?
If I understand your problem correctly, a couple of things are going on here.
First you do not have Mode=TwoWay, UpdateSourceTrigger=PropertyChanged} set in your bindings.
Second, the property binding will only update if there is a change in the value itself.
Check this out Data Binding
I figured out a work around, but it isn't quite what I wanted.
I changed the binding to use SelectedItem instead of SelectedValue, and the issue doesn't occur.
I got the idea from this question.
So I changed
SelectedValue="{Binding SelectedValue}"
To (and renamed my ViewModel Property):
SelectedItem="{Binding SelectedItem}"
After updating the uses of this property in the ViewModel, everything worked.

How to select an item in a listbox programatically using its index using c#

I have a listbox which can be edited. Once an item in that listbox is edited and saved, the newly edited item is displayed in the list however its position changes. I want to be able to have that item selected or highlighted even afer its position changes. I can get its new index however I couldnt manage to find a way to have re-selected programitcally after its psoiton changes in the listbox!
Any help here would be greatly appreciated
Thanks
ListBox.SelectedIndex = newPosition;
or
ListBox.SelectedIndices.Add(newPosition);
with an optional SelectedIndices.Clear() before if you want just your item to be selected.
i assume that the itemssource is some kind of collection. why you dont use the SelectedItem property with Mode=TwoWay to select the item you want from your c# code?
<ListBox ItemsSource="{Binding Path=YourItemsCollection}"
SelectedItem="{Binding Path=MySelectedItem, Mode=TwoWay}" />
another way is to use the ICollectionView MoveCurrentTo method. all you have to do is to create a ICollectionView with CollectionViewSource.GetDefaultView(this.YourItemsCollection). if you take this way you do not need binding to SelectedItem but you have to set IsSynchronizedWithCurrentItem=true for your listbox.

Categories

Resources