【发布时间】: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