【问题标题】:XAML GridView template bind to itemXAML GridView 模板绑定到项目
【发布时间】:2013-08-02 04:33:38
【问题描述】:

这似乎是一个棘手的问题。至少,很难描述。

基本上,我将 GridView 的 itemsSource 设置为对象列表,并且我希望 GridView 中的每个项目都可以访问它在自己的代码中生成的对象。

这里有一些sn-ps,我希望能说出我的意思:

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
}

【问题讨论】:

    标签: c# xaml gridview data-binding


    【解决方案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); }
    }
    

    注意ChapterProperty DP 实际上是绑定源,而视图模型属性 (BibleChapter) 是目标。但是,当您设置Mode=TwoWay 时,它会导致属性从目标更新源。

    【讨论】:

    • 哇,这太简单了!看起来它应该都可以工作,但是在 ChapterBox() 的构造函数中,Chapter 是 null (上面输入的代码有问题)。如果它应该在构造函数中为 null,它会在什么时候改变,我怎样才能让 builder.Build() 运行一次?
    • @MoscowModder 你可能可以使用属性更改处理程序 - 请参阅上面的更新。
    【解决方案2】:

    如果 Chapter 是一个 DependencyProperty,那么你可以简单地这样做:

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

    这会将单个项目的实例设置为绑定到Chapter 的任何内容,显然如果类型不匹配,您可以使用转换器。或者,您可能应该看看简单地设置用户控件的DataContext:

    <local:ChapterBox DataContext="{Binding}" ... />
    

    【讨论】:

      猜你喜欢
      • 2014-10-31
      • 2013-06-08
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 2014-08-18
      相关资源
      最近更新 更多