I have a ListBox, an IEnumerable is the data source. When a ListBoxItem is clicked, I want access to that object so I can grab some data and show another window.
Here is my ListBox XAML
`<ListBox Name="listBox1" Margin="0" Width="1010" Height="275" BorderThickness="0" BorderBrush="{x:Null}" Cursor="Arrow" HorizontalAlignment="Center" VerticalAlignment="Top" SelectionMode="Single" FontFamily="DIN" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Focusable="False" IsHitTestVisible="False" IsTextSearchEnabled="False" >`
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBox_MouseLeftButtonDown"></EventSetter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:Offer}">
<StackPanel Margin="0" Width="200" Height="275" Background="Black" Name="sp">
<Image Source="{Binding Image}" Width="200" Height="131" Margin="0"></Image>
<TextBlock Padding="5" Background="Black" Text="{Binding Name}" Foreground="White" FontFamily="DIN medium" FontWeight="Bold" FontSize="16" Width="200" Margin="0"></TextBlock>
<TextBlock Padding="5,0,5,0" Background="Black" Text="{Binding Date}" Foreground="White" FontFamily="DIN medium" FontWeight="Bold" FontSize="14" Width="200" Margin="0"></TextBlock>
<TextBlock Padding="5" Background="Black" Text="{Binding Description}" Foreground="White" FontFamily="DIN light" FontSize="16" Width="200" Margin="0" TextWrapping="WrapWithOverflow"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Background="Black" CanHorizontallyScroll="True" CanVerticallyScroll="False" FlowDirection="LeftToRight" Margin="0" Orientation="Horizontal" Width="1010" Height="275"></VirtualizingStackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>`$
other pertinent info
CurrentItems = (from offerCatType in offerRes.OfferCategory
where offerCatType.type == Type
from offers in offerCatType.Offer
where new DateTime(Convert.ToDateTime(offers.startDate).Year,
Convert.ToDateTime(offers.startDate).Month, 1) <= MonthYear && Convert.ToDateTime(offers.endDate) >= MonthYear
select new Offer
{
Name = offers.name,
Description = offers.description,
Date = String.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(offers.startDate)) + " to " + String.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(offers.endDate)),
ClickThruUrl = offers.ChannelInfo.refClickThroughLink,
ReferenceID = offers.ChannelInfo.refId,
Image = offers.ChannelInfo.refLink
}
);
listBox1.ItemsSource = CurrentItems;
protected void ListBox_MouseLeftButtonDown(object sender, RoutedEventArgs e)
{}
Is it possible some of my styling could blow away this event? I had it working earlier today, then was fixing a couple more styling items, then, the click code stopped working.
Set the IsHitTestVisible property to true instead of false for the listbox and you will get mouse events.
Related
I am creating custom phone book which reads phone book contacts and displays inside my application. So I am creating a long list selector and a listbox inside it.
My listbox will contain phone contact name and list of phone number with the check box under the particular name. I wrote an event trigger inside my listbox to make track of the checkbox is clicked or not.
PROBLEM : Event is not firing in the view model. I suspect since the listbox is present inside another list(long list selector), event is not firing.
Here is xaml code:
<phone:LongListSelector Grid.Row="3" LayoutMode="List" ItemsSource="{Binding PhoneBookDataSource}" IsGroupingEnabled="True" HideEmptyGroups="True">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<TextBlock Text="{Binding PhoneContactName}" FontWeight="SemiBold" FontSize="36" Foreground="Green"></TextBlock>
<ListBox ItemsSource="{Binding LstPhoneContactNumber,Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<i:InvokeCommandAction Command="{Binding PhoneNumberCheckedStateChangeCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="480">
<TextBlock Text="{Binding PhoneNumberItem}" FontSize="25" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Gray"></TextBlock>
<CheckBox Foreground="Black" Background="Black" VerticalAlignment="Center" HorizontalAlignment="Right" IsChecked="{Binding IsPhoneNumberItemChecked,Mode=TwoWay}"></CheckBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
<phone:LongListSelector.GroupHeaderTemplate>
<DataTemplate>
<Border Background="Transparent" Padding="5">
<Border Background="{StaticResource PhoneAccentBrush}" BorderBrush="{StaticResource PhoneAccentBrush}" BorderThickness="2" Width="62"
Height="62" Margin="0,0,18,0" HorizontalAlignment="Left">
<TextBlock Text="{Binding Key}" Foreground="{StaticResource PhoneForegroundBrush}" FontSize="48" Padding="6"
FontFamily="{StaticResource PhoneFontFamilySemiLight}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Border>
</Border>
</DataTemplate>
</phone:LongListSelector.GroupHeaderTemplate>
<phone:LongListSelector.JumpListStyle>
<Style TargetType="phone:LongListSelector">
<Setter Property="GridCellSize" Value="113,113"/>
<Setter Property="LayoutMode" Value="Grid" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border Background="{Binding Converter={StaticResource BackgroundConverter}}" Width="113" Height="113" Margin="6" >
<TextBlock Text="{Binding Key}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="48" Padding="6"
Foreground="{Binding Converter={StaticResource ForegroundConverter}}" VerticalAlignment="Center"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:LongListSelector.JumpListStyle>
</phone:LongListSelector>
Here is my view model:
public DelegateCommand PhoneNumberCheckedStateChangeCommand { get; set; }
public DelegateCommand SendSMSCommand { get; set; }
public CustomPhoneBookViewModel(INavigationService nav, IDataService data, IAESEnDecrypt encrypt, IGeoLocationService geoLocation, IMessageBus msgBus, ISmartDispatcher smartDispatcher)
: base(nav, data, encrypt, geoLocation, msgBus, smartDispatcher)
{
IsProgressBarBusy = true;
PhoneContactsList = new ObservableCollection<PhoneBookEntity>();
PhoneBookDataSource = new ObservableCollection<LLSAlphaKeyGroup<PhoneBookEntity>>();
InitializeDelegateCommands();
GetDeviceResolution();
ReadPhoneBook();
}
private void OnPhoneNumberItemCheckedStateChangedCommand()
{
try
{
foreach (var parentItem in PhoneBookDataSource)
{
foreach (var childItem in parentItem)
{
foreach (var item in childItem.LstPhoneContactNumber)
{
if (item.IsPhoneNumberItemChecked)
IsSendSMSButtonEnabled = true;
return;
}
}
IsSendSMSButtonEnabled = false;
}
}
catch { }
finally
{
SendSMSCommand.RaiseCanExecuteChanged();
}
}
Any suggestions is appreciated!!
The easiest way of making the nested ListBox interaction bind to the global ViewModel (instead of to its own, nested DataContext) is to assign a unique name to the outmost LongListSelector:
<phone:LongListSelector x:Name="OuterList" Grid.Row="3" LayoutMode="List" ItemsSource="{Binding PhoneBookDataSource}" IsGroupingEnabled="True" HideEmptyGroups="True">
and explicitly bind the Command to this element's DataContext (which is the global ViewModel):
<i:InvokeCommandAction Command="{Binding ElementName=OuterList, Path=DataContext.PhoneNumberCheckedStateChangeCommand}" />
Requirement:-
Want to show the contact list with alphabets groups.
Using Long list selector displays the contacts list. In xaml, we mention the IsFlatList="false" in long list selector for grouping, but it displays the contacts list without groups like list box.
I herewith attached my code for your comments, please let me know where I did mistake in my code.
<phone:PhoneApplicationPage.Resources>
<!--Heading for the items(a,b,c,d) -->
<DataTemplate x:Key="GroupItemHeaderTemplate">
<Border Background="Transparent" Padding="5">
<Border Background="{StaticResource PhoneAccentBrush}" BorderBrush="{StaticResource PhoneAccentBrush}" BorderThickness="2" Width="480" Height="40" Margin="0,0,18,0" HorizontalAlignment="Left">
<TextBlock Text="{Binding Key}" Foreground="{StaticResource PhoneForegroundBrush}" FontSize="20" Padding="6" FontFamily="{StaticResource PhoneFontFamilySemiLight}" HorizontalAlignment="left" VerticalAlignment="Center"/>
</Border>
</Border>
</DataTemplate>
<!-- Heading For the list -->
<DataTemplate x:Key="ListHeader">
<Border Background="#FF049ED6" Height="40">
<TextBlock Text= "{Binding Key}" />
</Border>
</DataTemplate>
<!--Items to display-->
<DataTemplate x:Key="ItemTemplate" >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ListImage}" Height="100" Width="100" Stretch="Uniform" Margin="10,2,0,0" ImageFailed="Image_ImageFailed" />
<TextBlock Text="{Binding ListFullName}" Width="200" Foreground="Black" Margin="10,10,0,0" FontWeight="SemiBold" FontSize="22" />
<TextBlock Text="{Binding ListBio}" FlowDirection="LeftToRight" Foreground="Black" Margin="-200,50,0,0" FontWeight="ExtraLight" FontSize="20" />
</StackPanel>
</DataTemplate>
<!--Group Intems Panel to show when click on Items Header-->
<ItemsPanelTemplate x:Key="GroupItemsPanel">
<toolkit:WrapPanel ItemHeight="105" ItemWidth="105"></toolkit:WrapPanel>
</ItemsPanelTemplate>
</phone:PhoneApplicationPage.Resources>
<toolkit:LongListSelector x:Name="LongListSelectorListBox" ItemsSource="{Binding Content}"
Margin="0,235,5,104" Width="480" Background="Transparent" IsFlatList="True"
DisplayAllGroups="False" ItemTemplate="{StaticResource ItemTemplate}"
GroupHeaderTemplate="{StaticResource GroupItemHeaderTemplate}"
ListHeaderTemplate="{StaticResource ListHeader}" GroupItemsPanel="{StaticResource GroupItemsPanel}"
SelectionChanged="LongListSelectorListBox_SelectionChanged" />
ViewModel:-
ServiceModel sm = new ServiceModel();
Content = new ObservableCollection<ListContactsModel>();
Content1 = new ObservableCollection<ListContactsModel>();
ServiceConstants.Temp_Response = x;//Save The response temp
ObservableCollection<ListContactsModel> result = ListContactsModel.extract(x, sm, OutGoingInvitation);
ObservableCollection<ListContactsModel> result1 = ListContactsModel.extract(x, sm, IncomeInvitation);
AppGlobalConstants.Temp_Response = result;
if (!((string.IsNullOrEmpty(sm.NetErrorCode)) && (string.IsNullOrEmpty(sm.ProvResErrCode))))
{
string errCode = !string.IsNullOrEmpty(sm.NetErrorCode) ? sm.NetErrorCode : sm.ProvResErrCode;
string errDesc = !string.IsNullOrEmpty(sm.NetErrorDesc) ? sm.NetErrorDesc : sm.ProvResErrDesc;
MessageBox.Show(errCode + "/" + errDesc);
}else if (result.Count > 0)
{
Content.Clear();
Content1.Clear();
Content = result;//Outgoing Invitations
Content1 = result1;//Incoming Invitations
List<AlphaKeyGroup<ListContactsModel>> DataSource = AlphaKeyGroup<ListContactsModel>.CreateGroups(result,
System.Threading.Thread.CurrentThread.CurrentUICulture,
(ListContactsModel s) => { return s.ListFullName; },
true);
Console.WriteLine("Content.Count==>>" + Content.Count);
Console.WriteLine("DataSource.ElementAt(1).Key==>>" + DataSource.ElementAt(1).Key);
Console.WriteLine("DataSource.ElementAt(2).Key==>>" + DataSource.ElementAt(2).Key);
Regards,
Naresh T
I don't see whole code and I don't know what is ListContactsModel. Is it a collection (inherits from IEnumerable/List/...)? Does it have Items propery? Does it have GetEnumerator methods?
As far as I understand AlphaKeyGroup class was taken from here (if not, read it first) http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj244365(v=vs.105).aspx
So maybe you want to bind LongListSelector to some property, that receive data from DataSource and has the same type List<_AlphaKeyGroup<ListContactsModel>_> (not to Content)?
Here is a couple of articles about LongListSelector:
http://www.geekchamp.com/articles/wp7-longlistselector-in-depth--part1-visual-structure-and-api
http://www.geekchamp.com/articles/wp7-longlistselector-in-depth--part2-data-binding-scenarios (this one will be great for you)
First of all, follow the instructions.
In general grouping in LLS means, that you have collection of items, that consist of Key/Title/Header/whatever and inner collection of group elements.
In my Wpf application, I've two datatemplates. DefaultDataTemplate and EditableDataTemplate.
Xaml:
<DataTemplate x:Key="DefaultDataTemplate" >
<StackPanel Orientation="Horizontal" Width="596">
<TextBlock Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="145"/>
<TextBlock Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="90"/>
<TextBlock Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="100"/>
<TextBlock Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="60"/>
<TextBlock Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="71"/>
<TextBlock Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="130"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="EditableDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<!--<ComboBox x:Name="ClientComboBox" SelectionChanged="ClientComboBoxChanged" ItemsSource="{Binding Path=clientList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ClientNameBindingClass, Mode=OneWayToSource}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="145"/>-->
<TextBox Text="{Binding ClientNameBinding,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<xctk:TimePicker Name="StartPicker" Value="{Binding StartValue, ElementName=MainWin, UpdateSourceTrigger=PropertyChanged}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" EndTime="11:59:0"/>
<xctk:TimePicker Name="EndPicker" Value="{Binding EndValue, ElementName=MainWin, UpdateSourceTrigger=PropertyChanged}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" EndTime="11:59:0"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
<ComboBox x:Name="ProjectComboBox" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" Width="130" Background="Yellow" BorderThickness="0"/>
</StackPanel>
</DataTemplate>
In the following code, I'm trying to change datatemplate so that I can edit existing data entries of the listbox. As per below code, when I move from DefaultDataTemplate to EditableDataTemplate, I can edit all the entries because all entries in edit mode. After editing is done when I click on button again,I just want to post that entry which I changed (whose text changed/ updated). But the problem is, on button click all the entries are getting posted to the web account to which I linked.
C#:
private void EditButton_Click(object sender, RoutedEventArgs e)
{
foreach (Harvest_TimeSheetEntry item in listBox1.Items)
{
if (item.isSynced)
{
item.isSynced = false;
listBox1.ItemTemplate = (DataTemplate)this.FindResource("EditableDataTemplate");
this.EditButton.Content = "Done Editing";
}
else
{
listBox1.ItemTemplate = (DataTemplate)this.FindResource("DefaultDataTemplate");
this.EditButton.Content = "Edit";
Globals._globalController.harvestManager.postHarvestEntry(item);
System.Windows.MessageBox.Show("Entry posted");
}
}
}
Below line is used for posting entry to Web account-
Globals._globalController.harvestManager.postHarvestEntry(item);
Please suggest something, how should I proceed?
You can add in the class Harvest_TimeSheetEntry a Property "DirtyFlag" that indicates that at least one value was changed.
public class Harvest_TimeSheetEntry
{
public bool DirtyFlag {get; private set;}
public void Reset()
{
DirtyFlag = false;
}
public DateTime StartValue
{
get { return _startValue; }
set
{
_startValue = value;
// in each of the modifyable Properties add this
DirtyFlag = true;
}
}
(...)
}
In your Button Event Handler you check then for this Flag:
private void EditButton_Click(object sender, RoutedEventArgs e)
{
foreach (Harvest_TimeSheetEntry item in listBox1.Items)
{
if (item.isSynced)
{
item.Reset();
item.isSynced = false;
listBox1.ItemTemplate = (DataTemplate)this.FindResource("EditableDataTemplate");
this.EditButton.Content = "Done Editing";
}
else
{
listBox1.ItemTemplate = (DataTemplate)this.FindResource("DefaultDataTemplate");
this.EditButton.Content = "Edit";
if(item.DirtyFlag)
{
Globals._globalController.harvestManager.postHarvestEntry(item);
item.Reset();
System.Windows.MessageBox.Show("Entry posted");
}
}
}
}
In my wpf application, there is View class where I've ListBox. I wrote the code for double click event of ListBox Item.so when I double click on any list Box Item that item will be posted in my Harvest account.Here is the event:
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
try
{
ListBoxItem item = (ListBoxItem)sender;
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)item.DataContext;
if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project" && entryToPost.ClientNameBinding == "Select Client")
MessageBox.Show("Please select you Project and Client");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
MessageBox.Show("Entry posted");
}
else
{
//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}
}
catch (Exception)
{ }
}
My xaml code:
<DataTemplate x:Key="DefaultDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<TextBox Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
<TextBox Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
</StackPanel>
</DataTemplate>
<!-- Editable DataTemplate -->
<DataTemplate x:Key="EditableDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<ComboBox x:Name="ClientComboBox" SelectionChanged="ProjectComboBoxChanged" ItemsSource="{Binding Path=clientList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ClientNameBindingClass, Mode=OneWayToSource}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding StopTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
<ComboBox x:Name="ProjectComboBox" SelectionChanged="ProjectComboBoxChanged" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" Width="71" Background="Yellow" BorderThickness="0"/>
</StackPanel>
</DataTemplate>
<!-- DataTemplate Selector -->
<l:DayViewListDataTemplateSelector x:Key="templateSelector"
DefaultDataTemplate="{StaticResource DefaultDataTemplate}"
EditableDataTemplate="{StaticResource EditableDataTemplate}"/>
I've timer in my class which generates that EditableDataTemplate with two comboBoxes. My problem is, when I select Client and Project in ComboBoxes and double click on the entry, it's posted in my account but at that time I want it to convert from editableDataTemplate to DefaultDataTemplate (i.e those two comboboxes should become textboxes likewise in DefaultDataTemplate). How should I achieve this result?
I don't think the DataTemplateSelector offers a method of changing the data template on request, it is merely used to choose different templates for different types of data (not data states).
I think probably the best way would be to add a property, lets call it IsInEditMode, to your data model. You could then add both the TextBlock and the Combobox to your data template and toggle their visibility according to the value of IsInEditMode.
By the way: if you use the ListBox.SelectedItem property in your DoubleClick-eventhandler you can directly access the data model element without having to first get the ListBoxItem and then access
its data context.
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
try
{
if(listBox1.SelectedItem is Harvest_TimeSheetEntry)
{
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)listBox1.SelectedItem;
if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project" && entryToPost.ClientNameBinding == "Select Client")
MessageBox.Show("Please select you Project and Client");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
MessageBox.Show("Entry posted");
entryToPost.IsInEditMode = true; //set edit mode!
}
}
else
{
//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}
}
catch (Exception)
{ }
}
I am trying to add an object to an ObservableCollection. As mentioned in a few question on this very site, I even tried to instantiate the collection before adding item. However, I am still getting the error. Here is my observation collection:
//Datacontext for local database
private WordDataContext wordsDB;
//Observable collection for binding
private ObservableCollection<WordItem> _wordItems = new ObservableCollection<WordItem>();
public ObservableCollection<WordItem> WordItems
{
get
{
return _wordItems;
}
set
{
if (_wordItems != value)
{
_wordItems = value;
NotifyPropertyChanged("WordItems");
}
}
}
I have overridden onNavigatedTo
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// Define the query to gather all of the idea items.
var wordItemsInDB = from WordItem word in wordsDB.WordItems
select word;
// Execute the query and place the results into a collection.
WordItems = new ObservableCollection<WordItem>(wordItemsInDB);
// Call the base method.
base.OnNavigatedTo(e);
}
And here is the button to add new item
private void newIdeaAddButton_Click(object sender, RoutedEventArgs e)
{
//// Create a new idea item based on the text box.
//WordItem newIdea = new WordItem { WordName = "TestTest" };
//Debug.WriteLine("I'm here!");
//// Add a idea item to the observable collection.
//WordItems.Add(newIdea);
//// Add a idea item to the local database.
//wordsDB.WordItems.InsertOnSubmit(newIdea);
WordItem newword = new WordItem { WordName = "Bingo" };
if (WordItems == null)
{
Debug.WriteLine("I'm null!");
WordItems = new ObservableCollection<WordItem>();
}
WordItems.Add(newword);
wordsDB.WordItems.InsertOnSubmit(newword);
Debug.WriteLine("Did something!");
}
And here's the XAML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<!--<ListBox Margin="14,0,-12,0" FontSize="{StaticResource PhoneFontSizeExtraLarge}" FontFamily="{StaticResource PhoneFontFamilySemiLight}">
<ListBoxItem Content="About" Tap="GoToAbout"/>
</ListBox>-->
<telerikData:RadJumpList x:Name="TestList" IsStickyHeaderEnabled="True" Margin="14,0,-12,0" ItemsSource="{Binding WordItems}">
<telerikData:RadJumpList.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="74">
<Rectangle x:Name="Bully" Width="20" Fill="Gray" Height="62" VerticalAlignment="Top" HorizontalAlignment="Left" />
<TextBlock Style="{StaticResource PhoneTextExtraLargeStyle}" Text="{Binding WordItem}" VerticalAlignment="Top"/>
</StackPanel>
</DataTemplate>
</telerikData:RadJumpList.ItemTemplate>
<telerikData:RadJumpList.StickyHeaderTemplate>
<DataTemplate>
<Border HorizontalAlignment="Stretch" Background="{StaticResource PhoneBackgroundBrush}" Height="74">
<Border Background="{StaticResource PhoneAccentBrush}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="62" Height="62">
<TextBlock Text="{Binding}" FontFamily="{StaticResource PhoneFontFamilySemiLight}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Padding="7,0,0,0" VerticalAlignment="Bottom" Foreground="White" />
</Border>
</Border>
</DataTemplate>
</telerikData:RadJumpList.StickyHeaderTemplate>
<telerikData:RadJumpList.GroupHeaderTemplate>
<DataTemplate>
<Border HorizontalAlignment="Stretch" Background="{StaticResource PhoneBackgroundBrush}" Height="74">
<Border Background="{StaticResource PhoneAccentBrush}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="62" Height="62">
<TextBlock Text="{Binding}" FontFamily="{StaticResource PhoneFontFamilySemiLight}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Padding="7,0,0,0" VerticalAlignment="Bottom" Foreground="White" />
</Border>
</Border>
</DataTemplate>
</telerikData:RadJumpList.GroupHeaderTemplate>
<telerikData:RadJumpList.GroupPickerItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel HorizontalAlignment="Center" ItemHeight="111" ItemWidth="111"/>
</ItemsPanelTemplate>
</telerikData:RadJumpList.GroupPickerItemsPanel>
<telerikData:RadJumpList.GroupPickerItemTemplate>
<DataTemplate>
<Border Background="{StaticResource PhoneAccentBrush}" Width="99" Height="99" VerticalAlignment="Top" HorizontalAlignment="Left">
<TextBlock Text="{Binding}" Style="{StaticResource PhoneTextExtraLargeStyle}" VerticalAlignment="Bottom" Foreground="White" />
</Border>
</DataTemplate>
</telerikData:RadJumpList.GroupPickerItemTemplate>
</telerikData:RadJumpList>
<Button x:Name="newIdeaAddButton" Click="newIdeaAddButton_Click" Content="Button" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="160" />
</Grid>
Okay, I finally got the solution! The problem in itself is a bit obscure. The thing is that, earlier, I had RadJumplist bound to a List<strings> and it had GroupDescriptor defined accordingly
GenericGroupDescriptor<string, string> testgroup = new GenericGroupDescriptor<string, string>(listitem => listitem.Substring(0, 1).ToLower());
However, the scenario in question is about ObservableCollection<WordItem>. As soon as an item is added to the collection, the RadJumpList is notified about those changes and the GroupDescriptor proves to be invalid in that context. That somehow raises the NullReferenceException. It's a bit unintuitive to relate the error with the cause.
So, the simple solution was to change the descriptor as follows
GenericGroupDescriptor<WordItem, string> gengd = new GenericGroupDescriptor<WordItem, string>();
gengd.KeySelector = (WordItem item) =>
{
char keyhead = item.WordName[0];
return keyhead.ToString().ToLower();
};
This thing is not really well documented!