I've got a ComboBox which looks like this:
<ComboBox x:Name="genreComboBox" ItemsSource="{Binding}" SelectionChanged="genreComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I'm binding it to a list of Genres List<Genre>, which have a Name and a genreId. Whenever the selection changes, I update the DataContext of a GridView based on that id. So basically I need to display the name, and use the id in a DataContext=someDB.getStuffById(int genreId);
I've tried messing around with getStuffById(genreComboBox.SelectedItem) and getStuffById(genreComboBox.SelectedValue), setting DisplayMemeberPath="Name" and SelectedValuePath="GenreId". Most of the time, the named get displayed.
Whatever I try to get that genreId out, I always get a NullReferenceException.
Thank you!
You may try calling getStuffById by the orignal list of genres.
getStuffById(listofGenres[genreComboBox.SelectedIndex].GenreId);
Related
I have a CustomListView that has controls like TextBoxes, HyperLinks and Buttons.
I can't share the exact Code, but please bear with me.
I have a list, say of type person with attributes like personHeight, personWidth, etc.
The binding to the list is done at the list level.
The issue is, that all values are fetched from the database, but only one is populated, the remaining are blank.
Also I didn't get any binding error in the Output Window. What could be the issue?
The general syntax of the XAML (where personHeight is one of the properties of the list object):
<CustomGridViewColumn SortOnProperty="personHeight">
<CustomGridViewColumn.SortableHeader>
<TextBlock Text="Person Height" />
</CustomGridViewColumn.SortableHeader>
<CustomGridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="personHeightText" Text="{Binding personHeight}" />
</DataTemplate>
</CustomGridViewColumn.CellTemplate>
<CustomGridViewColumn>
The binding is done as follows:
<CustomListView ItemsSource="{Binding Path=Persons}"
ItemContainerStyle="{StaticResource MyExpandableListViewItemStyle}"
ExpandedDataTemplate="{StaticResource ExpandedDataTemplate}"
ScrollViewer.CanContentScroll="False"
x:Name="GridList">
I have a collection of Contact objects that I've bound as follows in a WPF form:
<ComboBox Name="Name"
Text="{Binding Path=Contact.FullName}"
ItemsSource="{Binding ContactsCollection}"
SelectedItem="{Binding Path=Contact, Mode=TwoWay}"
IsEditable="true"
IsTextSearchEnabled="True"
TextBoxBase.TextChanged="Name_TextChanged"/>
<TextBox Name="Position" Text="{Binding Path=Contact.Position}"/>
<TextBox Name="Phone" Text="{Binding Path=Contact.PhoneNumber}"/>
I'd like the contact to be selected when the user starts typing in the combo 'IsTextSearchEnabled=true'.
The problem is that I'd like the items in the collection to remain read-only. Once a contact has been selected, any text deletes or additions modify the contact name in the collection.
How can I bind a collection to a combobox, enable search and prevent edits to the collection?
I could be missing something here, but if you don't want an editable ComboBox, try not setting the ComboBox.IsEditable property to True. Using this simple code, I can display items in a ComboBox and make selections by typing (when the ComboBox is focused) without editing anything:
<ComboBox ItemsSource="{Binding Items}" IsTextSearchEnabled="True" Height="25" />
Thanks for your input. It sorted me out. Removing the SelectedItem property and setting Position and PhoneNumber in the PropertyChanged event was what I needed.
i am trying to set selected item through following code but its not working:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Sort by" Margin="10" VerticalAlignment="Center"/>
<ComboBox Width="{StaticResource ComboWidth}" x:Name="sortcombo" ItemsSource="{Binding Path=SortOrder}" SelectionChanged="SearchCombo_SelectionChanged" SelectedItem="{Binding Path=DefaultSortIndex}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Sort}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
however it works fine if i use selectedIndex instead with binding to 0th index. Any thing wrong with declaration?
By the name of your property DefaultSortIndex maybe you are trying to bind an int for SelectedItem.
SeletedItem refers to an element of your collection binded to ItemsSource, so the property binded to SelectedItem must be of type of your collection elements.
If you bind int value to selected item then it will not work, you should bind element for that. For int value you can set it as mentioned in following post :
Set Selected Item of WPF Combobox to User Setting
found out the issue, actually the data source was creating new list everytime I call getData().
I have a list box bound to a collection, PlaylistTracks. PlaylistTracks contains a list id and a track id. All tracks are stored in another collection, Tracks. This collection has the track name.
Can I get the list box to display the track name from the collection its not bound to? Something like this:
<ListBox Name="PlayListTracksBox"
ItemsSource="{Binding Path=PlaylistTracks, Mode=TwoWay}"
DisplayMemberPath="Tracks.Name"
SelectedItem="{Binding Path=SelectedListTrack, Mode=TwoWay}"></ListBox>
I believe overriding ToString() for whatever class PlaylistTracks contains would be the fastest way to get what you want to work. Inside of ToString you'd have to put whatever logic you need to look up the correct track, and return the name therefrom.
If you can get the track name into the top-level object in your ItemsSource, then the more "Silverlighty" way to get this to work is to provide an ItemTemplate and bind to whatever you want. Something like:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Tracks.Name}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
EDIT:
The Answer I was looking for was....
<dataFormToolkit:DataField Label="Business Type:">
<ComboBox x:Name="BusinessType" SelectedItem="{Binding BusinessType, Mode=TwoWay}" >
<ComboBox.Items>
<sys:String>Land</sys:String>
<sys:String>Maritime</sys:String>
</ComboBox.Items>
</ComboBox>
</dataFormToolkit:DataField>
Heres the link to the article
I have a c# silverlight business application that uses the ado.net entity framework and the domain service class to bind to my sql server database and pull data from/ persist data to my database. I have been using the dataformtoolkit namespace to layout textboxes which can be edited/display data, using a TwoWay binding mode in order to allow the read/write functionality.
On some of the fields I want to use a combobox instead of a textbox in order to add a better user experience to my application. The impression i've got from reading around the web is that this isnt as easy as it should be.
My current textbox code looks like:
<dataFormToolkit:DataField>
<TextBox Text="{Binding BusinessType, Mode=TwoWay}" />
</dataFormToolkit:DataField>
my attempt at something similar is as follows but incorrect:
<ComboBox>
<ComboBox.Items>
<ComboBoxItem Content="Maritime" IsSelected="{Binding BusinessType, Mode=TwoWay}" />
<ComboBoxItem Content="Land" IsSelected="{Binding BusinessType, Mode=TwoWay}" />
</ComboBox.Items>
</ComboBox>
NB/ I want the combobox to be populated by a list or an enum etc. (preferably a list). The contents of the combobox should have nothing to do with the database, just that when the user hits submit the selection made in the combobox gets persisted back to the database. It is also important that the combobox can read from the database and display the specific selection that has already been made if this is the case.
****EDIT:
Current setup of dataform bound to datagrid with editable businesstype field as a textbox (I want to replace this textbox with a combobox that has two selectable items).
<!--DataForm Declaration-->
<dataFormToolkit:DataForm x:Name="dataForm1" Height="410" Width="331"
VerticalAlignment="Top"
Header="Job Details"
CurrentItem="{Binding SelectedItem, ElementName=dataGrid1}"
HorizontalAlignment="Left" >
<dataFormToolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataFormToolkit:DataField>
<TextBox Text="{Binding BusinessType, Mode=TwoWay}" />
</dataFormToolkit:DataField>
</StackPanel>
</DataTemplate>
</dataFormToolkit:DataForm.EditTemplate>
</dataFormToolkit:DataForm>
So how do i manipulate this code to use a combobox instead of a textbox?
Any help in doing this would be greatly appreciated.
You should setup your binding to use the ComboBox's SelectedValue property.
<ComboBox SelectedValue="{Binding BusinessType, Mode=TwoWay}">...</ComboBox>
The problem with this is that the ListBox and ComboBox will use the Equals() method on the object in the SelectedItem so if the types do not match then the ComboBox will not set the appropriate item as selected. Therefore, BusinessType will need to be a string since you are using ComboBoxItem's and specifying string content.
If bound the ItemsSource of the ComboBox then you would use SelectedItem and it would actually be an entity type as well, in which case you have more flexability/control around what equals what.