Xamarin Forms:动态创建Listview项-> BindingContext问题

时间:2018-10-03 12:56:07

标签: xamarin.forms binding-context

正如我在another post here on Stackoverflow中所描述的那样,我试图获得一种不同的布局(一帧跨越多个列表视图项)。现在,我决定尝试以下方法:我的ViewModel是一个列表列表(就像一个分组的列表视图一样)。但是,我没有使用分组的列表视图,而是使用普通的ListView,在该列表中,只要ParentViewCell的bindingContext可用,就会在代码隐藏中创建子列表的单个Items:

private void CommentViewCell_BindingContextChanged(object sender, EventArgs e)
        {
            if (this.BindingContext == null) return;
            var model = this.BindingContext as CommentViewModel;


            DateCommentViewCell dateCell = new DateCommentViewCell
            {
                BindingContext = model
            };

            ParentCommentViewCell parentCell = new ParentCommentViewCell
            {
                BindingContext = model
            };

            ContentStackView.Children.Add(dateCell.View);
            ContentStackView.Children.Add(parentCell.View);

            foreach (CommentBaseViewModel cbvm in model)
            {
                if (cbvm is CommentViewModel)
                {
                    ChildCommentViewCell childCell = new ChildCommentViewCell
                    {
                        BindingContext = cbvm
                    };

                    ContentStackView.Children.Add(childCell.View);
                }
            }
        }

当我运行此命令时,视觉效果实际上还不错,然后看看我打算如何使用它们。

但是BindingContext是错误的:ChildCommentViewCell BindingContext不会引用子项的CommentViewModel,而是在显示时引用父项的CommentViewModel 。我这样检查了ChildCommentViewCell的BindingContext

public ChildCommentViewCell ()
        {
            InitializeComponent ();
            BindingContextChanged += ChildCommentViewCell_BindingContextChanged;

        }

        private void ChildCommentViewCell_BindingContextChanged(object sender, EventArgs e)
        {
            Debug.WriteLine("### ChildCommentViewCell BindingContext Changed");
            test();            
        }

        public void test()
        {
            var context = this.BindingContext as CommentViewModel;
            Debug.WriteLine("### Instance: " + this.GetHashCode());
            Debug.WriteLine("### \tBinding Context: " + context.CommentModel.Text);
            Debug.WriteLine("### \tLabel: " + ChildCommentText.Text);
        }

,控制台上的输出就可以了。但是,在我的手机上运行时,实际内容(如上所述)是ParentCommentViewModel的内容。有什么想法吗?

ChildCommentViewCell元素的XAML代码如下:

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App.View.ViewCell.ChildCommentViewCell">
    <StackLayout Padding="10,0" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
        <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
            <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                <StackLayout Grid.Column="0" VerticalOptions="FillAndExpand" Orientation="Vertical" Spacing="0">
                    <Label Text="{Binding CommentModel.AuthorName}" Style="{StaticResource CommentAuthor}"/>
                </StackLayout>
                <Frame IsClippedToBounds="True" HasShadow="False" Margin="5" Padding="3" BackgroundColor="LightGray" CornerRadius="3.0">
                    <StackLayout Grid.Column="1" VerticalOptions="FillAndExpand" Orientation="Vertical" Spacing="0">
                        <Label x:Name="ChildCommentText" Text="{Binding Path=CommentModel.Text, StringFormat=' {0}'}" Style="{StaticResource CommentContent}"/>
                        <Label Text="{Binding CommentTimeAgo}" Style="{StaticResource CommentTime}" HorizontalOptions="Start"/>
                    </StackLayout>
                </Frame>
            </StackLayout>
        </StackLayout>
    </StackLayout>
</ViewCell>

另一件事:我尝试调试“出现的”事件,但是这个事件甚至没有被调用过一次……?!

非常感谢您!

1 个答案:

答案 0 :(得分:0)

在BindingContextChanged方法中发现了我的问题:我必须将BindingContext显式绑定到视图,而不仅是ViewCell:

foreach (CommentBaseViewModel cbvm in model)
            {
                if (cbvm is CommentViewModel)
                {
                    ChildCommentViewCell childCell = new ChildCommentViewCell
                    {
                        BindingContext = cbvm
                    };
                    childCell.View.BindingContext = cbvm;
                    ContentStackView.Children.Add(childCell.View);
                }
}