When my app starts, there is a ListView populated with data. How do I set it so that the first row is not selected automatically? Ie I don't want any items to be selected until the user clicks one
I have tried:
setting the property bound to the ListView's SelectedItem to null (the property is bound two-way),
setting the FallBackValue of the SelectedItem to null
but neither of these worked. Is there a way to do this, or am i stuck with the first item being selected when the app starts?
If you dont put SelectedItem or SelectedIndex it is not selecting anithing, but you want to set than try this code:
<ListView IsSynchronizedWithCurrentItem="True">
<ListViewItem>Some text1</ListViewItem>
<ListViewItem>Some text1</ListViewItem>
<ListViewItem>Some text1</ListViewItem>
<ListViewItem>Some text1</ListViewItem>
</ListView>
Related
When I use ComboBox or other controls that have ItemsSource and SelectedItem property bindings then each time upon the initial binding during runtime and also each time when the bound collection to ItemsSource changes I experience that the content of bound SelectedItem object is changed.
How can I disable this?
For example:
I have <ComboBox MinWidth="300" ItemsSource="{Binding AvailableMasters}" SelectedItem="{Binding SelectedMaster}">
When I run the application the SelectedMaster property is assigned the first item in AvailableMasters. Also, each time the AvailableMasters collection changes (for example, by assigning a new collection to the property) the SelectedMaster is again adjusted.
The desired behavior is that SelectedItem (SelectedMaster) is only populated/changed when the end-user clicks with the mouse on that item / chooses that item from the ComboBox or other control.
Set a flag/bool property before you update the collection and use it in SelectedMaster property. Or do you need only XAML solution?
I have a WPF 4.0 application and I am using the WPF DataGrid. What I want is to use navigation buttons on my view to change the SelectedItem the DataGrid, rather than letting the user change the SelectedItem by clicking on the DataGrid. I am using an ICollectionView in my ViewModel as the ItemsSource for my DataGrid. Here is what I have so far:
NextCommand (ViewModel):
DefaultView.MoveCurrentToNext(); // DefaultView is an ICollectionView
SelectedItem = DefaultView.CurrentItem as MyProperty;
DataGrid (View):
<DataGrid ItemsSource="{Binding Path=DefaultView}"
SelectedItem="{Binding Path=SelectedItem}"
IsSynchronizedWithCurrentItem="True">
...
</DataGrid>
The navigation buttons work great... however, I do not want to allow the user to click on the DataGrid to change the SelectedItem. Any ideas on how to accomplish this? I've played around with the DataGrid_SelectionChanged event, but the problem is that the binding on the SelectedItem updates the ViewModel before this event even fires. I would prefer that the SelectedItem does not get changed twice (once when the user clicks, and twice when it is set back to the original). I am ok with using the Code behind if needed...
Apparently you have to disable it through a custom DataGrid Template:
http://www.wpfsharp.com/2011/05/how-to-disable-row-selection-in-a-wpf-datagrid/
Out of curiosity, why do you want to do this? I mean why do you want to forbid a user from selecting a row by clicking it, but yet allow the user to select a row indirectly by using some navigation buttons?
How can I maintain selection on a combobox if the currently selected item is replaced in the ItemsSource collection? In this case the collection is an ObservableCollection and of course if the currently selected item is replaced the combobox loses its selection - nothing is selected.
The ComboBox looks like:
<ComboBox
Name="combobox"
SelectedValuePath="Id"
DisplayMemberPath="Description"
SelectedValue="{Binding Source={StaticResource cvs}, Path=Id, Mode=TwoWay}"/>
I cannot simply set the selected item on the combobox manually each time as the collection is manipulated in another generic class I cannot touch!
Thanks!
you bind the selectedVaule to your Id Property. so if you want the new added Item to be the selected one, just set your Id property to the new Item and call OnPropertyChanged("Id")
myCollection.Remove(oldItem);
myCollection.Add(newItem);
Id = newItem;
OnPropertyChanged("Id")
Use this code to select the new item:
combobox.SelectedItem = newItem;
UPDATE:
If the combobox is unknown to the part of the code that replaces the item, you need to do something like the following:
Subscribe to the CollectionChanged event of your collection.
When the event is fired and it says that a new item was added, execute the code as shown above.
I'm facing the following problem:
I have a datagrid on the first tab and combobox on the second. They both are bound to the same property - FieldList (OneWay for datagrid and TwoWay for combobox).
The point is that the user needs to be able to select item from the combobox on the seconds page, but when he does such thing - the SelectedItem in the datagrid on the first page is changing as well.
And my question is how to prevent this? I don't want to change selection in the datagrid.
Sounds like you need OneTime binding for the grid, rather than OneWay
I will try and explain this as concise as possible. I have 2 objects, the first which we will call object A that has an Id property and the second we will call object B, which has a ParentId property. The obvious relationship is that object B's ParentId is set to an object A's Id property. I am using the MVVM pattern, so on the viewmodel I have 2 ObservableCollections, one containing objects A the other objects B. On construction of the viewmodel, I create and fill the ObservableCollection<'A'> named ListItems. My xaml is simple,
<StackPanel>
<ListBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListItems}">
</ListBox>
<ComboBox SelectedValuePath="ParentId" SelectedValue="{Binding Path=ListItems/Id, Mode=OneWay}" ItemsSource="{Binding ComboItems}">
</ComboBox>
<Button Click="Button_Click" Content="Push Me"/>
</StackPanel>
As you can see the combobox's SelectedValue is bound to the ListItems current item's Id property. So essentially the listbox and combobox are in a master details.
If you press the button, it will fill the ObservableCollection<'B'> name ComboItems, which in turn populates the combobox. Now here is where the oddity begins. When I start the program, if the only thing I do is press the button, and then afterwords select an item in the listbox, the combobox will properly select an item due to the SelectedValue binding. But if I start the program and first select an item in the listbox and then press the button, the current combobox item will not change with the current listbox item. The binding appears to be forever broken. Does anyone know why this happens?
Ps. If I set the ItemsSource on the combobox before I set the SelectedValue/SelectedValuePath, the master/detail binding will never work. I know there is order to xaml, but that seems a little fragile. So if anyone has input on that also, I am all ears.
Thanks, Nate
EDIT -
When binding SelectedValue, it is very fragile. If the binding is working, i.e. have not selected anything in the listbox and then filled the combobox, if you choose an item in the combobox, the binding will break. After much time wasted with this, I chose to bind SelectedItem. This binding does not break in any of the conditions I have previously specified. I would however take any answers as to why SelectedValue binding is so ridiculous. Thanks again to all that have answered or will answer.
Yeah this is a problem we stumble upon quite a lot.
The problem is that after the ItemsSource property gets a new value, the SelectedValue binding will be cleared. Sucks, and until today we have not found a proper solution.
Here are a few workarounds:
Reset the SelectedValue binding in code, as soon as the new ItemsSource has been set. You can do this in a converter, or somewhere you'll know which will replace the ItemsSource binding (like the DataContextChanged event).
Instead of using the Binding on ItemsSource, try using a CollectionViewSource and a Filter. Put all your items in the CollectionViewSource object and filter the items when your combobox changes value.
Manually get your item the old fashion way when your listbox throws a SelectionChanged event.
Mind you, all solutions are not the prettiest in the book. I would go for option 2, its the cleanest IMO ;)
Hope this helps!