[英]Silverlight Windows Phone 7 ItemsControl Data Binding
我正在為 WP7 開發一個應用程序。 我有一組對象,每個對象都有一些我們需要在屏幕上顯示的屬性。 對不起,但要真正解釋一下,這將是大量代碼。
我的分組 Class:
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 ) );
}
}
我的列表 Class:
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 個按鈕出現,但它們是空的。 我只看到兩個藍色按鈕。 我確定我在那里的代碼中遺漏了一些愚蠢的東西,但根本找不到它。
對此有什么想法嗎? 我對數據綁定仍然很陌生,並且在一些實現細節上遇到了麻煩。 任何幫助都會很棒。 提前致謝。
首先,您沒有提供所有代碼。 在 xaml 代碼中有對EntityGroup
object 的綁定引用,我不知道那是什么。 但是有一些觀察結果可能會對您有所幫助:
為了使您的代碼工作,在中間 class 中創建實例到 class 對象並使用綁定來綁定到它們。 綁定適用於不適用 class 定義的實例對象。 這是您可以使用的 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, 太平紳士
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.