2016-05-02 11 views
9

私はXamarinフォームでデータバインディングに苦労しています。私は、次のXAMLの文から起こることを期待するもの、なぜ、ここにあります:Xamarinフォームのデータバインディング "セパレータ

IsVisible="{Binding Path=UserContext.IsLoggedOut}" 

は - プロパティが表示モデルの子オブジェクトにバインドされていること。これはXamarinフォームではサポートされていないか、またはトリックがありません。

これがWPFでサポートされているXamarinではサポートされていない場合は、ネストされたオブジェクトを伝播するために何をする必要がありますか?ビューモデルをフラット化することで、わかりやすいコードのリームやリームを書くことができます。

+0

どうUserContextが見えますか? –

+0

UserContextはviewModelクラスのプロパティです。 UserContextオブジェクトにIsLoggedOutというプロパティがあります –

+0

コードを貼り付けることができますか? –

答えて

13

ネストされたプロパティは、他のかなり複雑な式のように、サポートされていますがあります:

あなたはそれをテストすることができます。

XAML

<StackLayout Spacing="20"> 
    <StackLayout Orientation="Horizontal"> 
    <Label Text="Subproperties: " HorizontalOptions="FillAndExpand" FontSize="15"></Label> 
    <Label Text="{Binding Item.SubItem.Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label> 
    </StackLayout> 
    <StackLayout Orientation="Horizontal"> 
    <Label Text="Indexer: " HorizontalOptions="FillAndExpand" FontSize="15"></Label> 
    <Label Text="{Binding Item.Dictionary[key].Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label> 
    </StackLayout> 
    <StackLayout Orientation="Horizontal"> 
    <Label Text="Array Indexer: " HorizontalOptions="FillAndExpand" FontSize="15"></Label> 
    <Label Text="{Binding Item.Array[1].Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label> 
    </StackLayout> 
</StackLayout> 

ページ

public partial class Page2 : ContentPage 
{ 
    public ItemModel Item { get; } 

    public Page2() 
    { 
     InitializeComponent(); 
     Item = new ItemModel(); 
     BindingContext = this; 

    } 
} 

public class ItemModel 
{ 
    public ItemSubModel SubItem { get; set; } 
    public Dictionary<string, ItemSubModel> Dictionary { get; set; } 
    public ItemSubModel[] Array { get; set; } 

    public ItemModel() 
    { 
     SubItem = new ItemSubModel(); 
     Dictionary = new Dictionary<string, ItemSubModel> 
     { 
      {"key", new ItemSubModel()} 
     }; 
     Array = new [] {new ItemSubModel(), new ItemSubModel() }; 
    } 
} 

public class ItemSubModel 
{ 
    public string Text { get; set; } = "Supported"; 
} 

結果

enter image description here

+0

あなたは私のコードと関係がなければなりません。更新されます。 –

+0

問題を引き起こしていた正確な内容はわかりませんが、すべてをゼロから再実装しました。ありがとうSven-Michael! –

0

私はXamlで試していると仮定しています。 「パス」を削除してみてください。

IsVisible="{Binding UserContext.IsLoggedOut}" 

さらに重要なことは、あなたのBindingContextは何ですか?上記のコードを動作させるにはBindingContextをクラスFooに設定する必要があります。UserContextというプロパティがあり、そのプロパティ自体にはIsLoggedOutというプロパティがあります。

が見here同様

+0

BindingContextは、コード内で[ビューモデル]に設定されています。ビューモデルにはUserContextという名前のプロパティがあり、それ自体にIsLoggedOutというプロパティがあります。 Xamarinフォームでは、この伝播は動作していないようでも、どこにでも書かれているとは思いません。 AllDayerはこのようなことを働かせることができますか? –

関連する問題