对象数组中的数据绑定问题 [英] data binding problem in an array of objects

查看:84
本文介绍了对象数组中的数据绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个课程:

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];



和我的用户界面中的3个列表视图。 (lslvPlans,lslvRoutines,lslvTasks)我的问题是我应该使用的数据绑定技术。我想将每个列表绑定到当天的成员。 (每当CurrentDay更改所有列表都更新)我试过这个:


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;



其中currentday是指定当天的索引。 (我知道这是一团糟!它并不反映双面变化)任何专业数据绑定的想法?

----------------- ----------------------------------------------

我尝试了解决方案1,但它似乎有一些逻辑错误,因为当我运行程序时列表为空:


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;
        }
}

推荐答案

您的问题的一个解决方案是使用隐藏的 Selector 并绑定到其 SelectedValue 属性。类似于:

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>


这篇关于对象数组中的数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆