mardi 7 avril 2015

Changing ListBox item depending on DateTime


Vote count:

0




I have a ListBox with some items bound to it. Those items are read from files like so:



private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
bindList();
}

private void bindList()
{
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();

string[] fileList = appStorage.GetFileNames();

List<Activity> activities = new List<Activity>();


foreach (string file in fileList)
{

string fileName = file;

string cYear = file.Substring(0, 4);
string cMonth = file.Substring(5, 2);
string cDay = file.Substring(8, 2);
string cHour = file.Substring(11, 2);
string cMinute = file.Substring(14, 2);
string cSeconds = file.Substring(17, 2);

DateTime dateCreated = new DateTime(int.Parse(cYear), int.Parse(cMonth), int.Parse(cDay), int.Parse(cHour), int.Parse(cMinute), int.Parse(cSeconds));

string dYear = file.Substring(20, 4);
string dMonth = file.Substring(25, 2);
string dDay = file.Substring(28, 2);

DateTime dateDeadline = new DateTime(int.Parse(dYear), int.Parse(dMonth), int.Parse(dDay));

string aTitle = file.Substring(31);
aTitle = aTitle.Substring(0, aTitle.Length - 4);
activities.Add(new Activity() { Title = aTitle, DateCreated = dateCreated.ToLongDateString(), Deadline = dateDeadline.ToLongDateString(), FileName = fileName });

}

activityListBox.ItemsSource = activities;

}


As you can see I'm reading dates and a title from the file name. Afterwards I bind them to the ListBox. What I want to do is change ListBox item (2 textbox and a hyperlink) color each time the dateDeadline is past the current date.


Here is how my ListBox looks like:



<ListBox HorizontalAlignment="Stretch"
Name="activityListBox"
VerticalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<HyperlinkButton Name="activityTitle"
FontSize="40"
Content="{Binding Title}"
HorizontalContentAlignment="Left"
Tag="{Binding FileName}"
Click="activityTitle_Click"
/>

<TextBlock Name="activityDateCreated"
Text="{Binding DateCreated, StringFormat='Stworzono: {0}'}"
Margin="10" />

<TextBlock Name="activityDeadline"
Text="{Binding Deadline, StringFormat='Deadline: {0}'}"
Margin="10" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>


Every guide I found was dealing with particular ListBox item (like changing 3rd item, 4th item etc.) and it does not solve my problem. I want to be able to check if the Deadline is past the current date each time files are loaded to the app and change it accordingly.


I'll greatly appreciate your help.



asked 2 mins ago







Changing ListBox item depending on DateTime

Aucun commentaire:

Enregistrer un commentaire