Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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>
            <!-- 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:
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 3-Apr-13 22:17pm
Edited 4-Apr-13 4:19am
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

<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>
  Permalink  
Comments
cs101000 at 4-Apr-13 8:32am
   
Question improved. Would you take a look again?
Shmuel Zang at 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 at 4-Apr-13 9:22am
   
I still have the same problem. Am I missing something in code-behind?
Shmuel Zang at 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)

  Print Answers RSS
0 OriginalGriff 270
1 Sergey Alexandrovich Kryukov 185
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100