代码之家  ›  专栏  ›  技术社区  ›  Raumornie

我可以在Silverlight中声明性地将ItemsContol.ItemsSource绑定到ObservableCollection吗?

  •  0
  • Raumornie  · 技术社区  · 17 年前

    我一辈子都搞不懂为什么这样不行。我得到了一个简化的Xaml,如下所示:

    <UserControl x:Class="Foo.MainPage" 
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
        <Grid x:Name="LayoutRoot">
            <ScrollViewer VerticalAlignment="Stretch" 
                          Background="Black" 
                          HorizontalScrollBarVisibility="Auto" 
                          VerticalScrollBarVisibility="Auto" >
                <ItemsControl x:Name="PictureItemsControl"
                              ItemsSource="{Binding Pics}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" 
                                        VerticalAlignment="Center" 
                                        HorizontalAlignment="Center" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Image Source="{Binding Location}" 
                                       Height="200" 
                                       Width="200" 
                                       Stretch="UniformToFill" />
                                <TextBlock FontFamily="Verdana" 
                                           FontSize="16" 
                                           Foreground="White" 
                                           Text="{Binding Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </Grid>
    </UserControl>
    

    在MainPage.xaml的代码隐藏中,我有以下内容:

    namespace Foo
    {
        public partial class MainPage : UserControl
        {
            public FooViewModel viewModel;
    
            public MainPage()
            {
                InitializeComponent();
                viewModel = new FooViewModel();
                this.DataContext = viewModel;
            }
    
        }
    }
    

    我的ViewModel如下所示:

    namespace Foo
    {
        public class FooViewModel:INotifyPropertyChanged
        {
            public ObservableCollection<Pic> Pics;
            public FooViewModel()
            {
                Pics = new ObservableCollection<Pic>();
                Pic.GetPics(Pics);
            }
    
            //More code here... 
        }
    }
    

    Pic只是一个简单的类,它有一些公共属性和一个静态方法,用测试数据填充一个可观察的集合。问题是,我没有看到ItemsControl中发生任何绑定,除非我将这一行添加到我的主页构造函数:

    PictureItemsControl.ItemsSource = viewModel.Pics;
    

    2 回复  |  直到 17 年前
        1
  •  3
  •   Reed Copsey    17 年前

    public ObservableCollection<Pic> Pics { get; private set; }
    

    工作案例之所以有效,是因为您绑定到ViewModel,并间接绑定到图片。

        2
  •  0
  •   thomasmartinsen    17 年前