Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Consider this class:
C#
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:
C#
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:
C#
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:
HTML
<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>
            <!-- A hidden Selector for holding the current day (needed for data binding) -->
        
        </Grid>
        <ListBox Name="mySelector" Visibility="Collapsed"
                 ItemsSource="{Binding days}" SelectedIndex="{Binding CurrentDay}" />
    </Grid>
</Window>

Code-behind:
C#
public partial class MainWindow : Window
    {
        //define array of days
        public static Day[] days = new Day[366];
        public int CurrentDay; 
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Test.Prepare(); //inits days array
            CurrentDay = 0;
        }
}
Posted
Updated 4-Apr-13 3:19am
v4

1 solution

One solution to your problem can be using an hidden Selector and bind to its SelectedValue property. Something like:


XML
<Grid>
    <!-- An hidden Selector for holding the current day -->
    <ListBox Name="mySelector"
                Visibility="Collapsed"
                ItemsSource="{Binding Days}"
                SelectedIndex="{Binding CurrentDay}" />
        
    <!-- A Grid with the current day as its DataContext -->
    <Grid DataContext="{Binding SelectedValue, ElementName=mySelector}">
        <ListView ItemsSource="{Binding PreDefinedPlan}">
            <!-- ... -->
        </ListView>

        <!-- ... -->
    </Grid>
</Grid>
 
Share this answer
 
Comments
cs101000 4-Apr-13 8:32am    
Question improved. Would you take a look again?
Shmuel Zang 4-Apr-13 9:04am    
What's the DataContext of your window (or the root Grid)? Try to add a days property in your window's code-behind and, set the window's DataContext to itself in the window's constructor (DataContext = this). (Or the same with your view-model, if you use MVVM.)
cs101000 4-Apr-13 9:22am    
I still have the same problem. Am I missing something in code-behind?
Shmuel Zang 4-Apr-13 10:21am    
Change the declaration of days and CurrentDay to instance properties. (public Day[] days { get; set; } public int CurrentDay { get; set; }).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900