文字列のObservableCollectionにバインドされたListViewがあります。 Listviewには1つのラベルと1つのContentView(ラベルのみを含む)があります。 両方とも同じコレクションにバインドされています。
また、コレクションのランダムデータを生成するボタンがあります。
問題は、アプリを実行して[データの生成]ボタンをクリックすると、ラベルは更新されますが、ContentViewは更新されないことです。
コンテンツビュー内のラベルも変更する必要があります。
コンテンツビュー内のラベルは同じままです
Nugetパッケージ:
Xamarin.Forms
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を