2

これは難しいようです。少なくとも、説明するのは難しいです。

基本的に、GridView の itemsSource をオブジェクトのリストに設定しています。GridView 内の各項目が、独自のコードで生成されたオブジェクトにアクセスできるようにします。

ここに、私が言いたいことを伝えたいと思うスニペットをいくつか示します。

ReadingPage.xaml.cs

zoomedInGrid.ItemsSource = openChapters.chapters; //A List<BibleChapter>

ReadingPage.xaml

<GridView x:Name="zoomedInGrid">
   <GridView.ItemTemplate>
       <DataTemplate>
           <local:ChapterBox Chapter="" />
           <!--I have no idea what to put here^^^-->
       </DataTemplate>
   </GridView.ItemTemplate>
</GridView>

次に、ChapterBox.xaml.cs で、テンプレート化された ChapterBox が作成された BibleChapter にアクセスする必要があります。

誰でもこれを行う方法を知っていますか?

編集

これは私が ChapterBox.xaml.cs に持っているものです:

public static readonly DependencyProperty ChapterProperty =
    DependencyProperty.Register("Chapter",
        typeof(BibleChapter),
        typeof(ChapterBox),
        null);

public BibleChapter Chapter
{
    get { return (BibleChapter)GetValue(ChapterProperty); }
    set { SetValue(ChapterProperty, value); }
}

public ChapterBox()
{
    this.InitializeComponent();
    VerseRichTextBuilder builder = new VerseRichTextBuilder();
    builder.Build(textBlock, Chapter); //<== Chapter is null at this point
}
4

2 に答える 2

1

クラスに依存関係プロパティを追加ChapterBoxし、XAML で双方向バインディングを使用します。

<local:ChapterBox Chapter="{Binding Mode=TwoWay}" />

DP は次のようになります (WPF を使用していると仮定しますが、Silverlight の場合も同様です)。

public static readonly DependencyProperty ChapterProperty = 
    DependencyProperty.Register("Chapter", 
        // property type
        typeof(BibleChapter), 
        // property owner type
        typeof(ChapterBox), 
        new UIPropertyMetadata(new PropertyChangedCallback(OnChapterChanged)));

public static void OnChapterChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    var chapterBox = (ChapterBox)sender;

    VerseRichTextBuilder builder = new VerseRichTextBuilder();
    var newValue = (Chapter)args.NewValue;
    builder.Build(chapterBox.textBlock, newValue); 
}

public BibleChapter Chapter
{
    get { return (BibleChapter)GetValue(ChapterProperty); }
    set { SetValue(ChapterProperty, value); }
}

ChapterPropertyDP は実際にはバインディングソースであり、ビュー モデル プロパティ ( BibleChapter) はターゲットであることに注意してください。ただし、 を設定Mode=TwoWayすると、プロパティによってソースがターゲットから更新されます。

于 2013-08-02T04:45:34.047 に答える
1

が DependencyProperty の場合Chapter、次のように簡単に実行できます。

<local:ChapterBox Chapter="{Binding}" />

これにより、個々のアイテムのインスタンスが何にでもバインドされるように設定されますChapter。明らかに、タイプが一致しない場合は、コンバーターを使用できます。DataContext別の方法として、ユーザー コントロールのを単純に設定する方法を検討する必要があります。

<local:ChapterBox DataContext="{Binding}" ... />
于 2013-08-02T04:39:54.630 に答える