1

我将 listview 与 headerTemplate 一起使用。我需要创建一个可绑定属性来将文本添加到我的模板中。

所以我在我的标题模板“MyMenuHeader”中创建了一个可绑定属性:

public static readonly BindableProperty TitleTextproperty =
BindableProperty.Create("TitleText", typeof(String), typeof(MyMenuHeader), null);

public String TitleText
{
   get { return (String)GetValue(TitleTextproperty); }
   set {
         SetValue(TitleTextproperty, value);
         OnPropertyChanged("TitleText");
       }
}

var nomGroup = new Label
            {
                TextColor = Color.FromHex(ConstColor.PrimaryPrastel),
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                FontSize = 15,
                HorizontalTextAlignment = TextAlignment.Start,
                VerticalOptions = LayoutOptions.Center,
            };
            nomGroup.SetBinding(Label.TextProperty, "TitleText");

BindingContext = this;

在我的列表视图中,我尝试使用“设置值”设置此属性:

 //Design des cellules de groupe 
 var cellGroup = new DataTemplate(typeof(MyMenuHeader));
 cellGroup.CreateContent();
 cellGroup.SetValue(MyMenuHeader.TitleTextproperty, _dataMenu._nomMenu);
 HeaderTemplate = cellGroup;

但我的属性已设置但从未显示在标签中。

此致,

编辑:我有一个 bindableproperty.create 的新参数

 public static readonly BindableProperty TitleTextProperty =
 BindableProperty.Create("TitleText", typeof(String), typeof(MyRelaiTabCell), null, propertyChanged: OnTitleTextChanged);

        public String TitleText
        {
            get { return (String)GetValue(TitleTextProperty); }
            set { SetValue(TitleTextProperty, value); }
        }

这是这个函数,它在属性更改时修改标签的值:

static void OnTitleTextChanged(BindableObject bindable, object oldValue, object newValue)
{
    var menu = (bindable as MyRelaiTabCell);
    // Property changed implementation goes here
    menu._titleText.SetValue(Label.TextProperty, (newValue as String));
}

这是工作,但只有一种方式。视图模型-> 视图单元。所以当我设置值时。当我试图在改变时获得价值时,我没有信息。

4

0 回答 0