【发布时间】:2018-09-03 11:33:50
【问题描述】:
我想动态绑定 ViewModel 类的 ScrollView 内容。
我有 xaml 代码:
<ScrollView
Grid.Column="1"
Content="{Binding UserGrid}"
HorizontalScrollBarVisibility="Never"
IsEnabled="False"
Orientation="Horizontal">
</ScrollView>
和 ViewModel 代码
class ViewModel : BaseViewModel{
private Grid _userGrid { get; set; }
public Grid UserGrid
{
get { return _userGrid; }
set
{
_userGrid = value;
OnPropertyChanged();
}
}
public override async Task InitAsync()
{
await Task.Factory.StartNew(() =>
{
_userGrid = new Grid
{
ColumnSpacing = 1,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
_userGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(GridHeightSize * 4) });
// and adding some content to This grid
}
}
当然,我已经在 View 类中初始化了 Binding 上下文
我的问题是是否可以使用此内容绑定动态绑定此内容。 目前,我收到一个错误:
,, 未找到“内容”的属性、可绑定属性或事件,或者值和属性之间的类型不匹配”
如果它不是动态视图绑定的正确方式,那是什么?
【问题讨论】:
-
我觉得内容一定是
Xamarin.Forms.View见:docs.microsoft.com/en-us/dotnet/api/… -
我已将其更改为 Xamarin.Forms.View。仍然遇到同样的错误
-
噢,嗯……嗯,通常方法有点不同。您到底想在滚动视图中显示什么?
-
为什么不在
ScrollViewxaml 中创建一个嵌套的Grid然后直接引用它? -
@Stefan。它是更复杂视图的一部分:i.imgur.com/4t5LXDP.jpg。我已经在 xaml 和 cs 代码中创建了它,它应该根据用户或时间等参数动态创建。我想将它转换为 MVVM 只是为了知道如何去做。这就是为什么我要绑定这个视图,因为它的所有依赖项都应该从服务中获取或在 ViewModel 中计算
标签: c# xamarin xamarin.forms