【问题标题】:Bind to properties of property in viewmodel xamarin.forms绑定到视图模型 xamarin.forms 中的属性
【发布时间】:2020-07-20 15:38:26
【问题描述】:

我有一个包含多个控件的内容视图,我想将其绑定到内容视图代码后面的属性的属性。模型已正确传递给内容视图,但未更新绑定。

内容视图的 XAML 是:

<ContentView.Content>
      <Grid>
          <Grid.ColumnDefinitions>
              <ColumnDefinition Width="Auto" />
              <ColumnDefinition Width="Auto" />
              <ColumnDefinition Width="Auto" />
              <ColumnDefinition Width="Auto" />
          </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Source="clock" />
            <Label Grid.Column="1" Text="{Binding Summary.MeetingsList}" />
            <StackLayout Grid.Column="2">
                <Image Source="meeting" />
                <Label Text="{Binding Summary.TotalMettings}"/>
            </StackLayout>
            <StackLayout Grid.Column="3">
                <Image Source="people"/>
                <Label Text="{Binding Summary.People}" />
            </StackLayout>
        </Grid>
  </ContentView.Content>

内容视图背后的代码:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SummaryControl: ContentView
{
    public static readonly BindableProperty SummaryProperty = BindableProperty.Create(nameof(Summary), typeof(SummaryModel), typeof(SummaryModel), new SummaryModel(), BindingMode.OneWay,
    propertyChanged: (bindableObject, oldValue, newValue) =>
    {
        var view = bindableObject as SummaryControl: 
        view.Summary = (SummaryModel)newValue;
    });

    public SummaryModel Summary { get { return (SummaryModel)GetValue(SummaryProperty); } set { SetValue(SummaryProperty, value); } }

    public SummaryControl: ()
    {
        InitializeComponent();
    }
}

SummaryModel 类:

public class SummaryModel
{
    public string TotalMettings { get; set; }
    public string People { get; set; }
    public string MeetingsList { get; set; }
}

我需要另一个类作为内容视图的视图模型吗?

【问题讨论】:

    标签: mvvm xamarin.forms binding


    【解决方案1】:

    好像忘记设置绑定路径了

    在内容视图中

    <ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                  x:Name="CustomView"  // set the name of view
                 x:Class="xxx">
    
    <Label Text="{Binding Summary.MeetingsList,Source={x:Reference CustomView}}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 2011-05-22
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多