以编程方式更改列表框内文本块的绑定属性路径 - windows 8 app

时间:2013-08-05 07:41:16

标签: c# binding windows-8 path app-store

我需要以编程方式更改listbox中textblock的绑定属性路径是否有任何解决方案?属性路径应根据当天的不同而变化。例如,如果是星期一,那么Text =“{Binding SubjectLongNameMonday}”,如果是星期二,则Text =“{Binding SubjectLongNameTuesday}”等...

<ListBox x:Name="lbToday" Loaded="lbToday_Loaded">
                        <ListBox.ItemTemplate>
                            <DataTemplate >
                                <Grid x:Name="gridTodayBtn" Tag="{Binding}" Tapped="gridTodayBtn_Tapped">

 <TextBlock x:Name="tbTodaySubjectNameMonday" Text="{Binding SubjectLongNameMonday}"/>

                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

C#代码

 if (DateTime.Today.DayOfWeek.ToString().Equals("Monday")) 
        {
            Binding myBinding2 = new Binding();
            myBinding2.Source = this;
            myBinding2.Path = new PropertyPath("MyDatasTimetableMonday");
            lbToday.SetBinding(ListBox.ItemsSourceProperty, myBinding2);
            //here need change path in xaml of textblock tbTodaySubjectNameMonday  Text="{Binding SubjectLongNameMonday}" to  Text="{Binding SubjectLongNameTuesday}"
        }

1 个答案:

答案 0 :(得分:0)

一种简单的方法是通过SetValue:

lbToday.SetValue(ListBox.ItemsSourceProperty, this);