2016-05-02 17 views
0

我在DesignData中苦苦掙扎,應該是一個非常簡單的例子。 我定義在Windows通用項目在Windows Universal Collection中使用DesignData

<UserControl 
... 
xmlns:local="clr-namespace:SimpleDataBind" 
... 
<UserControl.Resources> 
    <DataTemplate x:Key="SimpleListTemplate"> 
     <TextBox Text="{Binding Name}" /> 
    </DataTemplate> 
</UserControl.Resources> 
<Grid> 
    <StackPanel Grid.Row="0" d:DataContext="{d:DesignData Source=./DesignData/Single.xaml}"> 
     <TextBox Text="{Binding Name}"/> 
    </StackPanel> 
    <ListView Grid.Row="1" d:DataContext="{d:DesignData Source=./DesignData/Multiple.xaml}" 
     ItemTemplate="{StaticResource SimpleListTemplate}" 
     ItemsSource="{Binding collection}"> 
    </ListView> 
</Grid> 

的類以下的瑣碎控制的定義如下:

public class Element 
{ 
    public Element() { } 
    public string Name { get; set; } 
} 
public class ElementCollection : ObservableCollection<Element> 
{ 
    public ElementCollection() { } 
} 

型ElementCollection的元素「集合」的定義代碼後面給出一個有效的綁定源。

Single.xaml很簡單:

<local:Element xmlns:local="clr-namespace:SimpleDataBind" Name="Single" /> 

多也同樣如此:

<local:ElementCollection xmlns:local="clr-namespace:SimpleDataBind" > 
    <local:Element Name="Multiple Pete" /> 
    <local:Element Name="Multiple George" /> 
    <local:Element Name="Multiple John" /> 
</local:ElementCollection> 

當設計師認爲,我們應該看到上面的設計數據。實際上,Control在網格的第一行中正確顯示'Single'作爲Name。但是,控件在網格的第二行沒有顯示任何內容。

我明顯錯過了一些明顯的...

回答

0

好的,終於想通了。解決方案是不綁定到XAML中的運行時對象;即,在上面我的源代碼,與元素

ItemsSource="{Binding}" 

然後,你可以明顯地結合於...運行時運行時對象替換元素

ItemsSource="{Binding collection}" 

這很有趣(也許對其他人更熟悉這個工具很明顯),它的工作原理是這樣的,微軟的文檔有點模糊和過時(小夥子想要給你一些Silverlight文檔在環球中根本不工作)。

但無論如何,希望這可以幫助別人!

相關問題