2016-11-17 3 views
2

コンパイル済みのデータバインディングの概念を理解するのに苦労しています。 ListBoxとそのListBoxに使用される1つのデータテンプレート(ItemTemplate)を含む1つのビュー(MainPage)があります。 MainPageには、ItemViewModelsのObservableCollectionを含むMainPageViewModelがあります。 ItemViewModelには1つのプロパティNameが含まれます。x:リソース辞書のバインドが機能しません。

メインページ:

<Page x:Class="TestApp.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:TestApp"> 
    <Page.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="ItemDictionary.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Page.Resources> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <ListBox ItemsSource="{x:Bind ViewModel.Items}" 
       ItemTemplate="{StaticResource ItemTemplate}" /> 
    </Grid> 
</Page> 

リソースディクショナリ含むのDataTemplate:

<ResourceDictionary 
    x:Class="TestApp.ItemDictionary" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:TestApp"> 

    <DataTemplate x:Key="ItemTemplate" x:DataType="local:ItemViewModel"> 
     <TextBlock Text="{x:Bind Name}" /> 
    </DataTemplate>  
</ResourceDictionary> 

このコードはコンパイルが、私はアイテムが生成されますが、それは、プロパティが失敗した名前への結合を実行するとき。私が古典的なバインディングを使用する場合、すべて正常に動作し、MainPageのリソースに直接データテンプレートを配置すると、それも機能します。私は何が欠けていますか?

+4

多分役立ちます:http://igrali.com/2015/06/14/how-to-use-compiled-bindings-xbind- from-a-resource-dictionary/ – Clemens

+0

バインドは、コンパイル時の構造体、IIRCです。しかし、DataTemplateのすべてはランタイムです。ウィンドウにはハードコードされていませんが、必要に応じて実行時に動的にロードされます。私はこれを教えてくれる参考資料はありませんが、x:Bindsは実行時に結びついていないと仮定しています。それを通常のBindingに切り替えます。 StaticResoruceをコンパイル時に利用可能なコンテンツにどのように使用できるかはまあまあですが、実行時設定のリソースを見つけようとするときは、DynamicResourceを使用する必要があります。 – Will

+0

@ウィルしかし、ドキュメントによると、x:DataTypeが宣言されていれば動作するはずです。 [DataTemplateとx:DataType](https://msdn.microsoft.com/windows/uwp/data-binding/data-binding-in-depth#binding_object_xbind)を参照してください –

答えて

2

正しい:

<Page.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <local:ItemDictionary /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Page.Resources> 

誤:

<Page.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="ItemDictionary.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Page.Resources> 
関連する問題