هناك ListView مرتبطة بسلسلة ObservableCollection. عرض القائمة يحتوي على تصنيف واحد و ContentView واحد (لا يحتوي إلا على تسمية). كلاهما مرتبط بنفس المجموعة.
أيضًا ، هناك زر يقوم بإنشاء بعض البيانات العشوائية للمجموعة.
المشكلة هي عندما أقوم بتشغيل التطبيق والنقر على زر إنشاء بيانات ، يتم تحديث التسمية ولكن ليس ContentView.
يجب أيضًا تغيير التسمية داخل طريقة عرض المحتوى.
تظل التسمية داخل طريقة عرض المحتوى كما هي
حزم نوجيت:
Xamarin. الأشكال
NETStandard.Library
الأجهزة المتأثرة:
تم طرح سؤال على Stackoverflow قبل أسبوع - https://stackoverflow.com/questions/50211197/xamarin-forms-user-control-binding-inside-listview
عمل
this.BindingContext = this;
في MagicBox.xaml.cs يفرض BindingContext على الكائن الحالي. هذا يعني أيضًا أن BindingContext من الأصل لم يعد موروثًا.
لجعله يعمل ، قم بتغيير الكود الخاص بك خلف إلى
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MagicBox : ContentView
{
public static readonly BindableProperty TextProperty =
BindableProperty.Create("Text", typeof(string), typeof(MagicBox), default(string));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public MagicBox ()
{
InitializeComponent ();
}
}
و xaml الخاص بك
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestSample.Controls.MagicBox"
x:Name="box">
<ContentView.Content>
<Grid>
<Label Text="{Binding Text, Source={x:Reference box}}" />
</Grid>
</ContentView.Content>
</ContentView>
شكرًا StephaneDelcroix ، لقد
التعليق الأكثر فائدة
عمل
في MagicBox.xaml.cs يفرض BindingContext على الكائن الحالي. هذا يعني أيضًا أن BindingContext من الأصل لم يعد موروثًا.
لجعله يعمل ، قم بتغيير الكود الخاص بك خلف إلى
و xaml الخاص بك