Consider this class:
public class Day
{
public DateTime DT { get; set; }
public List<Plan> PreDefinedPlan { get; set; }
public List<Routine> DoneRoutine { get; set; }
public List<Task> DoneTask { get; set; }
}
I have this array:
public static Day[] days = new Day[366];
and 3 listviews in my UI. (lslvPlans, lslvRoutines, lslvTasks) My problem is with the data binding technique that I should use. I want to bind each list to current day's members. (whenever CurrentDay changes all the lists are updated) I tried this:
lslvPlans.ItemsSource = days[CurrentDay].PreDefinedPlan;
lslvRoutines.ItemsSource = days[CurrentDay].DoneRoutine;
lslvTasks.ItemsSource = days[CurrentDay].DoneTask;
in which currentday is index for specifying current day. (I know it is a mess! And it doesnt reflect 2-sided changes) Any ideas for a professional data binding?
---------------------------------------------------------------
I tried solution 1 but it seems to have some logical errors since lists are empty when I run the program:
<Window x:Class="Diary.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
<Grid>
<Grid Name="MainGrid" Margin="10"
DataContext="{Binding SelectedValue, ElementName=mySelector}">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListView Name="lslvPlans" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding PreDefinedPlan}">
<ListView.View>
<GridView>
<GridViewColumn Header="name" Width="auto" DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="hour" Width="auto" DisplayMemberBinding="{Binding Path=t}"/>
</GridView>
</ListView.View>
</ListView>
<ListView Name="lslvRoutines" Grid.Column="1" Grid.Row="1" ItemsSource="{Binding DoneRoutine}">
<ListView.View>
<GridView>
<GridViewColumn Header="name" Width="auto" DisplayMemberBinding="{Binding Path =Name}"/>
<GridViewColumn Header="hour" Width="auto" DisplayMemberBinding="{Binding Path=t}"/>
</GridView>
</ListView.View>
</ListView>
<ListView Name="lslvTasks" Grid.Column="2" Grid.Row="1" ItemsSource="{Binding DoneTask}">
<ListView.View>
<GridView>
<GridViewColumn Header="period" Width="auto" DisplayMemberBinding="{Binding Path =p}"/>
<GridViewColumn Header="name" Width="auto" DisplayMemberBinding="{Binding Path=Name}"/>
</GridView>
</ListView.View>
</ListView>
<StackPanel>
<Button Width="100" Height="30" Click="Button_Click_1" >++CurrentDay</Button>
<Button Width="100" Height="30" Click="Button_Click_2">--CurrentDay</Button>
<Button Width="100" Height="30" Click="Button_Click_3" >Update Plans List</Button>
<Button Width="100" Height="30" >Update Tasks List</Button>
</StackPanel>
<!--
</Grid>
<ListBox Name="mySelector" Visibility="Collapsed"
ItemsSource="{Binding days}" SelectedIndex="{Binding CurrentDay}" />
</Grid>
</Window>
Code-behind:
public partial class MainWindow : Window
{
public static Day[] days = new Day[366];
public int CurrentDay;
public MainWindow()
{
InitializeComponent();
DataContext = this;
Test.Prepare();
CurrentDay = 0;
}
}