【问题标题】:Binding nested control using MVVM pattern使用 MVVM 模式绑定嵌套控件
【发布时间】:2011-11-05 09:32:00
【问题描述】:

我在将嵌套控件与我的 MVVM 模式绑定时遇到问题。这是我的 XAML 代码:

 <ItemsControl Grid.Column="1" ItemsSource="{Binding NotificationContacts}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <toolkit:Expander>
                        <toolkit:Expander.Header>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding ContactName}" Grid.Column="0" VerticalAlignment="Center"></TextBlock>
                                <Image Source="Images/answer_ok.png" Grid.Column="1" Margin="15,0,15,0" Width="27" Height="27"></Image>
                            </Grid>
                        </toolkit:Expander.Header>
                        <toolkit:Expander.Content>
                            <ListBox Margin="30,10,0,10" ItemsSource="{Binding NotificationContacts.Messages">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding MessageName}"></TextBlock>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </toolkit:Expander.Content>
                    </toolkit:Expander>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

问题是,位于 ExpanderControl 数据模板中的列表框控件没有被数据绑定。 列表框控件由名为“Messages”的 EntityCollection 填充,该 EntityCollection 包含在 ItemsControl 与之数据绑定的父对象“NotificationContacts”中...

有谁知道如何解决这个问题?

提前致谢!!!

【问题讨论】:

  • 你的视图模型是什么样的?

标签: silverlight data-binding mvvm


【解决方案1】:

你试过了吗:

<ItemsControl Grid.Column="1" ItemsSource="{Binding NotificationContacts}">
  ......
   <ListBox Margin="30,10,0,10" ItemsSource="{Binding Messages}">
  .....
       <TextBlock Text="{Binding MessageName}"></TextBlock>

如果我没记错的话,当您在“内部”ItemContol 时,绑定上下文设置为 NotificationContacts。所以只使用“{Binding Messages}”就可以了。

顺便说一句,你缺少大括号:

<ListBox Margin="30,10,0,10" ItemsSource="{Binding NotificationContacts.Messages">

【讨论】:

    【解决方案2】:

    调用 ItemsControl f.i. "ic" 并在 ListBox 中使用下一个绑定

    <ItemsControl x:Name="ic" Grid.Column="1" ItemsSource="{Binding NotificationContacts}">
         ...
         <ListBox Margin="30,10,0,10" ItemsSource="{Binding ElementName=ic, Path=DataContext.Messages}">
    

    【讨论】:

    • 感谢您的帮助。不幸的是,您的解决方案对我不起作用......列表框仍然没有被数据填充......
    • 哪个类包含 Messages 属性?
    猜你喜欢
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 2011-10-29
    • 2011-07-29
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2011-05-04
    相关资源
    最近更新 更多