Xamarin.forms: Xamarinフォームコンテンツビュー(ユーザーコントロール)がListView内でバインドされていません

作成日 2018年05月15日  ·  3コメント  ·  ソース: xamarin/Xamarin.Forms

説明

文字列のObservableCollectionにバインドされたListViewがあります。 Listviewには1つのラベルと1つのContentView(ラベルのみを含む)があります。 両方とも同じコレクションにバインドされています。

また、コレクションのランダムデータを生成するボタンがあります。

問題は、アプリを実行して[データの生成]ボタンをクリックすると、ラベルは更新されますが、ContentViewは更新されないことです。

再現する手順

  1. VS 2017を開き、新しいXamarin Formsブランクプロジェクトを作成します(標準)
  2. MainPage.xamlにボタンとリストビューを含むStackLayoutを配置します
  3. ListviewのDataTemplateを、1つのLabelと1つのContentViewを含むViewCellに設定します(以下で作成します)。 TextプロパティをObservableCollectionアイテムにバインドします。
  4. MainPage.xaml.csでObservalbeを作成します財産
  5. コンストラクターで、Observableコレクションをいくつかのランダムな値で初期化し、バインディングコンテキストをこれに設定します。
  6. ボタンのクリックをサブスクライブし、ボタンが押されるたびに、いくつかのランダムな値をObservableコレクションに割り当てます。
  7. 上で使用した新しいContentView(xaml)を作成し、Textプロパティにバインドされたラベルをその中に配置します
  8. xaml.csで、バインド可能なプロパティテキストを作成します。
  9. または、VS 2017(15.7.1)で添付のソリューションを解凍して開くこともできます
    TestSample.zip
  10. Androidで実行し、生成ボタンをクリックします。
  11. ラベルにバインドされたテキストは変更されますが、ContentViewにバインドされたテキストは変更されないことに注意してください。

予想される行動

コンテンツビュー内のラベルも変更する必要があります。

実際の動作

コンテンツビュー内のラベルは同じままです

基本情報

  • 問題のあるバージョン:4.10.0.442
  • 最後に知られている良いバージョン:
  • IDE:VS 2017(15.7.1)
  • プラットフォームターゲットフレームワーク:

    • iOS:

    • Android:8.3.0.19

    • UWP:

  • Androidサポートライブラリバージョン:
  • Nugetパッケージ:
    Xamarin.Forms
    NETStandard.Library

  • 影響を受けるデバイス:

スクリーンショット

複製リンク

https://github.com/AppGrate/ListViewBindingIssue

question

最も参考になるコメント

やって

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>

全てのコメント3件

やって

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に感謝しうまくいきました:)

このページは役に立ちましたか?
0 / 5 - 0 評価