2017-12-18 16 views
1

以下は、グリッドビューでのアイテムテンプレートです。私が達成しようとしています何グリッドの観測可能なコレクション内のグリッドアイテムの位置番号を表示

     </Grid.ColumnDefinitions> 
         <StackPanel> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding SerialNumber}" VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" Margin="10" FontSize="25"/> 
           <TextBlock Text="." VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" FontSize="25" /> 
          </StackPanel> 
          <Image Grid.Column="0" Margin="20" Height="100" Width="150" HorizontalAlignment="Center" Source="{Binding ImageUri,Mode=TwoWay}" VerticalAlignment="Center"/> 
         </StackPanel> 
       </Border> 
      </DataTemplate> 
     </GridView.ItemTemplate> 

のTextBlockテキストでコレクション内の項目の位置を表示することです= "{のSerialNumberバインディング}(これは、リストビューであるとしたら、それは希望。。、どのように私はこれを達成するためにしてください)行番号で

答えて

0

あなただけの特定のプロパティを持つクラスを定義し、XAMLでそれをバインドする必要があり

私はあなたの参照のためのシンプルなコードサンプルを作っ:

ザビエル謝@
<GridView ItemsSource="{Binding oc}"> 
     <GridView.ItemTemplate> 
      <DataTemplate> 
       <Border> 
        <StackPanel> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock Text="{Binding SerialNumber}" VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" Margin="10" FontSize="25"/> 
          <TextBlock Text="." VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" FontSize="25" /> 
         </StackPanel> 
         <Image Grid.Column="0" Margin="20" Height="100" Width="150" HorizontalAlignment="Center" Source="{Binding source,Mode=TwoWay}" VerticalAlignment="Center"/> 
        </StackPanel> 
       </Border> 
      </DataTemplate> 
     </GridView.ItemTemplate> 
</GridView> 
public sealed partial class MainPage : Page 
{ 
    public ObservableCollection<Test> oc { get; set;} 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     oc = new ObservableCollection<Test>(); 
     oc.CollectionChanged += Oc_CollectionChanged; 
     for (int i=0;i<10;i++) 
     { 
      oc.Add(new Test() {SerialNumber=i,source=new BitmapImage(new Uri("ms-appx:///Assets/test.png")) }); 
     } 
     this.DataContext = this; 
    } 

    private void Oc_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
     if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) 
     { 
      for (int i =e.OldStartingIndex; i<oc.Count;i++) 
      { 
       oc[i].SerialNumber = i; 
      } 
     } 
    } 
} 


public class Test:INotifyPropertyChanged 
{ 
    private int _SerialNumber; 
    public int SerialNumber 
    { 
     get { return _SerialNumber; } 
     set 
     { 
      _SerialNumber = value; 
      RaisePropertyChanged("SerialNumber"); 
     } 
    } 

    private BitmapImage _source; 
    public BitmapImage source 
    { 
     get { return _source; } 
     set 
     { 
      _source = value; 
      RaisePropertyChanged("source"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void RaisePropertyChanged(string PropertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this,new PropertyChangedEventArgs(PropertyName)); 
     } 
    } 
} 
+0

、ないuはヘルプアップデートにアイテムが観測コレクションから削除され、これまでのObservableCollectionの行番号を提供するソリューション?そうでなければ、どのようにしてそれを作るかについてのアイデア。 –

+0

アイテムを削除した場合は、自分で 'SerialNumber'を更新する必要があります。更新された返信をご覧ください。 SerialNumberを更新するために 'CollectionChanged'イベントを登録しました。そして' Test'クラスを[INotifyPropertyChanged]から継承させます(https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v = vs。 110).aspx)。これは、SerialNumberが更新されたことをUIに通知するために使用されます。 –

+0

は完全に動作します –

関連する問題