我有以下情況...我有一個包含ItemsControl的窗口。我爲Window的DataContext指定了一個ViewModel。我爲ItemControl的ItemTemplate指定了一個DataTemplate。在DataTemplate中,我使用ComboBox,對於ComboBox的ItemsSource,我使用RelativeSource綁定到其包含的Window的DataContext。在運行期間一切正常,並且Binding已正確解析,但在Design-Time Cider無法獲取包含Window的ViewModel的ItemSource綁定的視圖模型。從上面的代碼路徑= DataContext.AvailableGenres使用Wpf RelativeSource綁定中斷可混合性
<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>
所以基本上:
這裏是我的代碼(我離開了在頂部的XML命名空間的聲明,但在我的代碼,它們被包括在內)在設計時間內無法解析,但在運行時間內,它已正確解析。
有沒有人知道如果我做錯了什麼,或者它是Wpf xaml解析器的問題,它無法在設計時解析綁定到RelativeSources?
...可混合性? – 2012-01-09 15:29:33
創建設計時間數據的能力,以便設計師有 設計對... – Imri 2012-01-10 06:58:00