【问题标题】:Silverlight Windows Phone 7 ItemsControl Data BindingSilverlight Windows Phone 7 ItemsControl 数据绑定
【发布时间】:2011-07-14 06:13:24
【问题描述】:

我正在为 WP7 开发一个应用程序。我有一组对象,每个对象都有一些我们需要在屏幕上显示的属性。很抱歉,但要真正解释一下,这将是大量代码。

MyGrouping 类:

public class MyListGrouping : INotifyPropertyChanged
{
    public MyListGrouping( )
    {
        _Title = "";
        _Group = new ObservableCollection<MyList>( );
    }

    private string _Title;
    public string Title
    {
        get { return _Title; }
        set
        {
            _Title = value;
            NotifyPropertyChanged( "Title" );
        }
    }

    private ObservableCollection<MyList> _Group;
    public ObservableCollection<MyList> Group
    {
        get { return _Group; }
        set
        {
            _Group = value;
            NotifyPropertyChanged( "Group" );
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged( string propertyName )
    {
        if ( PropertyChanged != null )
            PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
    }
}

MyList 类:

public class MyList : INotifyPropertyChanged
{
    public MyList( ){}

    private string _DisplayName;
    public string DisplayName
    {
        get{return _DisplayName;}
        set
        {
            _DisplayName = value;
            NotifyPropertyChanged( "DisplayName" );
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged( string propertyName )
    {
        if ( PropertyChanged != null )
            PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
    }
}

现在介绍 Silverlight 中的 2 个单独的用户控件。

    <ScrollViewer VerticalScrollBarVisibility="Auto">
    <ItemsControl x:Name="ItemContainer" ItemsSource="{Binding Path=ListGroups}" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical" Margin="0,5,0,0">
                    <!--Only works if we don't bind here for some reason-->
                    <base:MyListView ListGroup="{Binding Group}" Title="{Binding Title}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

    public partial class MyListGroupingView : UserControl
{
    public static readonly DependencyProperty ListGroupsProperty = DependencyProperty.Register( "ListGroups", typeof( ObservableCollection<MyListGrouping> ), typeof( MyListGroupingView ), new PropertyMetadata( null ) );

    public MyListGroupingView( )
    {
        InitializeComponent( );
        this.DataContext = this;
    }

    public ObservableCollection<MyListGrouping> ListGroups
    {
        get { return (ObservableCollection<MyListGrouping>)GetValue( ListGroupsProperty ); }
        set { SetValue( ListGroupsProperty, value ); }
    }

    private void UserControl_Loaded( object sender, RoutedEventArgs e )
    {
        MyListGrouping aList = new MyListGrouping( ) { Title = "A" };
        aList.Group.Add( new MyList( ) { DisplayName = "Ant" } );
        aList.Group.Add( new MyList( ) { DisplayName = "Art" } );

        MyListGrouping bList = new MyListGrouping( ) { Title = "B" };
        bList.Group.Add( new MyList( ) { DisplayName = "Bob" } );
        bList.Group.Add( new MyList( ) { DisplayName = "Billy" } );

        ObservableCollection<MyListGrouping> collection = new ObservableCollection<MyListGrouping>( );
        collection.Add( aList );
        collection.Add( bList );

        ListGroups = collection;
    }
}

以及它使用的 UI 元素:

    <Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="80*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30*"/>
        <ColumnDefinition Width="70*"/>
    </Grid.ColumnDefinitions>

    <Button x:Name="SectionLetterBtn" Content="{Binding Path=EntityGroup.StartingText}" Background="{StaticResource PhoneAccentBrush}" Foreground="{StaticResource PhoneForegroundBrush}" BorderBrush="Transparent" Grid.Column="0" Grid.Row="0" />

    <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
        <ItemsControl x:Name="ItemContainer" ItemsSource="{Binding Path=EntityGroup.Group}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20*" />
                            <ColumnDefinition Width="80*" />
                        </Grid.ColumnDefinitions>

                        <TextBlock Text="{Binding DisplayName}" Grid.Column="1" />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>

    public partial class MyListView : UserControl
{
    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register( "Title", typeof( string ), typeof( MyListView ), new PropertyMetadata( null ) );
    public static readonly DependencyProperty ListGroupProperty = DependencyProperty.Register( "ListGroup", typeof( ObservableCollection<MyList> ), typeof( MyListView ), new PropertyMetadata( null ) );

    public MyListView( )
    {
        InitializeComponent( );
        this.DataContext = this;
    }

    public ObservableCollection<MyList> ListGroup
    {
        get { return (ObservableCollection<MyList>)GetValue( ListGroupProperty ); }
        set { SetValue( ListGroupProperty, value ); }
    }

    public string Title
    {
        get { return (string)GetValue( TitleProperty ); }
        set { SetValue( TitleProperty, value ); }
    }
}

我遇到的问题是我会看到 2 个按钮出现,但它们是空的。我只看到两个蓝色按钮。我确定我在那里的代码中遗漏了一些愚蠢的东西,但根本找不到。

对此有什么想法吗?我对数据绑定仍然很陌生,并且在一些实现细节上遇到了麻烦。任何帮助都会很棒。提前致谢。

【问题讨论】:

  • 我没有看到任何 DataContext-assignment?您是在代码中遗漏了它还是我失明了?以防万一:您需要在代码或 XAML 中为网格提供指向类实例的 DataContext。
  • sprinter252 是对的。您发布的代码的 xaml 部分不包含 DataContext 分配。这通常在 标记的开头指定。请在单独的代码 sn-p 中发布整个 xaml 文件内容,因为这非常重要,因为您在 XAML 文件中使用绑定

标签: c# silverlight data-binding windows-phone-7


【解决方案1】:

首先,您没有提供所有代码。在 xaml 代码中有对 EntityGroup 对象的绑定引用,我不知道那是什么。但有一些观察结果可能会对您有所帮助:

  • 您正在实施某种 MVVM,但我没有看到该 VM。前两个代码 sn-ps 是属于模型的类,后两个是视图
  • 为了使您的代码正常工作,请在中间类中为类对象创建实例,并使用绑定来绑定它们。绑定适用于实例对象,而不适用于类定义。这是您可以使用的 ViewModel:

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace **YourNamespace**
    {
        public class ViewModel
        {
            public MyList TheList {get; set;}
            public MyListGrouping ListGrouping {get; set;}
    
            public ViewModel()
            {
                TheList = new MyList();
                ListGrouping = new MyListGrouping();
                PopulateLists();
            }
    
            private void PopulateLists()
            {
                TheList.DisplayName = "Testing display name";
    
                MyList lList = new MyList();
                lList.DisplayName = "List0";
                MyList lList1 = new MyList();
                lList1.DisplayName = "List1";
                MyList lList2 = new MyList();
                lList2.DisplayName = "List2";
    
                ListGrouping.Title = "Testing title";
                ListGrouping.Group.Add(mList);
                ListGrouping.Group.Add(lList);
                ListGrouping.Group.Add(lList1);
                ListGrouping.Group.Add(lList2);
            }
        }
    }
    
  • 您的 xaml 文件非常混乱且不完整。但是,如果您想进行正确的绑定,它们的标题应该包含如下行:

    ...
    xmlns:base="clr-namespace:**YourNamespace**" 
    ...
    
    <UserControl.Resources>
        <base:ViewModel   x:Key="ViewModel"/>
    </UserControl.Resources>
    
  • 那么你应该在 xaml 文件中指定DataContext:

    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModel}}">
    
  • 然后你可以绑定到ViewModel的属性:

    <TextBlock Text="{Binding Path=TheList.DisplayName}" Grid.Column="1" />  
    

你有它。绑定效果很好。

HTH, JP

【讨论】:

    猜你喜欢
    • 2011-03-23
    • 1970-01-01
    • 2012-09-11
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    相关资源
    最近更新 更多