2012-01-09 4 views
1

次のシナリオがあります。ItemsControlを含むウィンドウがあります。私は、WindowのDataContextのViewModelを指定します。 ItemControlのItemTemplateのDataTemplateを指定します。 DataTemplateではComboBoxを使用し、ComboBoxのItemsSourceにはRelativeSourceバインディングを含むウィンドウのDataContextを使用します。実行時にはすべて正常に動作し、バインディングは正しく解決されますが、Design-Time CiderではItemSourceがバインドされているウィンドウのViewModelを取り出すことができません。ここでWpf RelativeSourceを使用するバインディングブレンドのブレンド性

は(私が一番上にあるXML名前空間宣言を残したが、私のコードでは、それらが含まれている)私のコードです:

<Window d:DataContext="{Binding Source={StaticResource DesignViewModel}}"> 

<Window.Resources> 
    <designviewmodels:GenresEditorDesignViewModel x:Key="DesignViewModel" /> 
</Window.Resources> 

<ItemsControl Grid.Row="0" Margin="3" ItemsSource="{Binding Path=CurrentState}" > 
<ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <Grid DataContext="{Binding}"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"></ColumnDefinition> 
       <ColumnDefinition Width="20"></ColumnDefinition> 
      </Grid.ColumnDefinitions> 

      <ComboBox Grid.Column="0" Margin="3,0,3,0" 
       ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
       AncestorType={x:Type Window}}, Path=DataContext.AvailableGenres, 
       Mode=OneWay}" 
       DisplayMemberPath="Name" 
       SelectedItem="{Binding Path=Genre, Mode=TwoWay}" DataContext=" 
       {Binding}" /> 

       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Window> 

だから、基本的には上記のコード片からのパス= DataContext.AvailableGenresは、設計時には解決できませんが、実行時には正しく解決されます。

私が何か間違っているのか、それとも設計時にRelativeSourcesへのバインディングを解決できないWpf xamlパーサに問題があるのか​​誰かが知っていますか?

+0

...ブレンド可能性? –

+0

デザインタイムデータを作成する機能は、デザイナーが に対抗するものです... – Imri

答えて

1

私はこれが古い質問だと知っていますが、後世のために私には私のために働く解決策があります。

RelativeSourceバインディングをBlendableにすることはできませんでした。ただし、祖先が拘束されていない幸運な場合は、設計時環境にサインポストを提供することができます。

予備の祖先(この場合はグリッド)で、DataContextをPathだけに設定してを除く同じRelativeSource にバインドします。次に、d:DataContextを同じ祖先に適用し、実際の元の要素にバインドする型(または同等の模様)を提供します。最後に、元の要素(ComboBox)をプロパティまたはパスにバインドします。

<Grid 
    DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
      AncestorType={x:Type Window}}, Path=DataContext, 
      Mode=OneWay}" 
    d:DataContext="{Binding Source={StaticResource DesignViewModel}}" > 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"></ColumnDefinition> 
      <ColumnDefinition Width="20"></ColumnDefinition> 
     </Grid.ColumnDefinitions> 

     <ComboBox Grid.Column="0" Margin="3,0,3,0" 
      ItemsSource="{Binding Path=AvailableGenres, Mode=OneWay}" 
      DisplayMemberPath="Name" 
      SelectedItem="{Binding Path=Genre, Mode=TwoWay}" DataContext=" 
      {Binding}" /> 

</Grid> 
関連する問題