I'm currently working on a music player app and I'm getting stuck on keeping the playing track highlighted in all ListViews in the app.
For example, there is a page with a list of albums, each of which contains a DataTemplate with a list of songs. All is well and good when you first select it and the selected item is bound to the viewmodel. But on navigating away from and the returning to that page, the viewmodel retains the information about which sing was playing, but the binding doesn't cause the item to be highlighted.
Here is my XAML:
<ListView x:Name="lstSongs"
ItemsSource="{Binding attachments}"
SelectionChanged="lstSongs_SelectionChanged"
ScrollViewer.VerticalScrollMode="Disabled"
SelectedValue="{Binding DataContext.selectedSong, ElementName=Group_Page}"
SelectedItem="{Binding DataContext.selectedSong, ElementName=Group_Page}"
ItemContainerStyle="{StaticResource ListViewTextHighlight}"
Grid.Row="1">
Binding the SelectedItem and SelectedValue was just to see if either one would work.
I'm probably missing something obvious, but nevertheless I hope someone can help me out.
Thanks!
I think the SelectedItem binding needs to be Mode=TwoWay
SelectedItem="{Binding DataContext.selectedSong, ElementName=Group_Page, Mode=TwoWay }"
Related
I have a screen in WPF that needs to list data from same database table, but separately by status. In the print each column is a different status and each is a ListBox, the SelectedItem binding for all ListBox is the same property in ViewModel. The problem is when an item is selected in some ListBox it still selected even after I select an item from other ListBox and when the focus return to the first ListBox (with Tab key for example) the item gets the selected style. Any idea how to resolve this? I can change the ListBoxes for other solution too, at this moment i can't see other solution.
Sorry for my bad English, and I don't know if the explanation is much clear this feature is a bit complicated to explain, but any doubts I'll provide the answers.
Thanks for all.
do you want to have the same item selected in each listbox? Try adding IsSynchronizedWithCurrentItem="true" to every listbox in xaml:
<StackPanel>
<!--both comboboxes are synchronized with each other-->
<ComboBox ItemsSource="{Binding Products}" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Name"/>
<ComboBox ItemsSource="{Binding Products}" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Name"/>
</StackPanel>
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
I have ListView with dependency property for selecting more than one item. ListView sample code is here:
<ListView ItemsSource="{Binding Test}"
ItemTemplate="{StaticResource SomeTemplate}"
SelectionMode="Multiple"
multipleBind:SelectionChangedCommand.Command="{Binding SelectionChangedCommand}">
</ListView>
It works properly but what I want is pre-select default items and I don't know How do it. Is it a possible? Please give me some hit.
Thanks
I'm new to WPF and I have this ListBox which I want to instantiate with a specific ListBoxItem, so the user knows what to do with the ListBox.
<ListBox Name="DbListBox"
Grid.Column="3"
HorizontalAlignment="Left"
Height="246"
Margin="0,99,0,0"
Grid.Row="1"
VerticalAlignment="Top"
Width="211"
SelectionMode="Single"
SelectedItem="{Binding Path=selectedDB,Mode=TwoWay}"
AllowDrop="True"
Drop="DbListBox_Drop">
<ListBoxItem Name="ListBoxItem" FontStyle="Italic">Drag .db file here or add below</ListBoxItem>
</ListBox>
Then I have some code which adds a collection of items to the ItemsSource of this ListBox, but I can't do that since the ItemsSource is not empty
DbListBox.ItemsSource = DbCollection;
My question is, how can I start up the ListBox with the item inserted first, and then when DbCollection is added to it, it simply overwrites the first ListBoxItem?
When using WPF properly, we'd normally have something like this, where a collection property would be data bound to the ListBox.ItemsSource property:
<ListBox ItemsSource="{Binding SomeCollectionProperty}" />
Once we have this XAML, we don't need to touch the ListBox again, as we can add or remove items from the data bound collection and they will magically appear (or disappear) from the ListBox:
SomeCollectionProperty.Add(new SomeDataType());
SomeCollectionProperty.Remove(someItemFromCollection);
The SomeDataType used here is up to you... it depends on what you want to display in your items. If it was just a plain string for example, then you could simply do this to add your initial item into the collection:
SomeCollectionProperty.Add("Drag .db file here or add below");
If you wanted to make that item look different to the others then you'd need to data bind a custom class that has a Text property and FontStyle property for example. You could data bind these properties in a DataTemplate to design each item to be exactly as you want it. However, that's a completely different question.
To find out more about these things, you can read the Data Binding Overview and Data Templating Overview page on MSDN.
Im cleaning up this question and going to put in an example that makes sense to me..hopefully for you too...
so lets say i have an Items control. This control is bound to an observable collection of NBA Basketball teams (Lakers, Heat, Knicks...ect). In this observable collection of NBATeams I have a property of FavoritePlayer. I have a datatemplate that lays out how this is going to look inside the Items Control. I have a textbox which displays the team name. (this could be/should be read only..its not now) but it displays the team name.. LA Lakers. the second item in my items control is a combobox. this combo box will ultimately display my favorite player on each team.
The itemssource on the combobox is from a lookup value i have. It displays all the people on the team and the displaymemberpath on this combobox is "DisplayText"...I need my selected item on this combobox to be shown. so if my favorite player is Kobe Bryant the combo box should display this.
<telerik:RadComboBox Grid.Row="0" Grid.Column="9" Width="150" EmptyText="--Select Player--"
ItemsSource="{Binding PlayerList}"
SelectedItem="{Binding FavoritePlayer, Mode=TwoWay}"
DisplayMemberPath="DisplayText" HorizontalAlignment="Left"></telerik:RadComboBox>
I am used to just having textblocks in a listbox and then when an item is selected from within that listbox i use that selected item to bind my comboboxes that live outside of my listbox. Now i actually have the combobox in the listbox(ItemsControl now). So Im wondering the best way to bind the combobox from within an ItemsControl
I initially was just having issues getting the players to show up in the combobox..but I solved that by doing the code below.
I found my first issue of just binding the items source to the combo box from within an items control.
I had to set the bindings to look like this.
ItemsSource="{Binding DataContext.PlayerList, ElementName=ItemsControlNBATeams}"
so i had to set the name of the items control and use that in the element name and set the path = to DataContext.PlayerList. this now brings up the items in my combo box..what it doesnt do is set the selecteditem of my combo box.
Not very clear about your you are asking but does this answer your question
<ListBox ItemsSource="{Binding}" x:Name="ListBox1"></ListBox>
<TextBox Text="{Binding SelectedItem, ElementName=ListBox1}"> </TextBox>