2014-01-10 27 views
1

爲用戶控件資源添加實體虛擬,但Visual Studio抱怨它無法找到資源「虛擬」。這樣可以添加設計數據嗎?我究竟做錯了什麼?如何將設計模式虛擬數據添加到WPF控件

<UserControl x:Class="MovieScraper.Media" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:entity="clr-namespace:Processor.Entity;assembly=Processor" 
     mc:Ignorable="d" 
     d:DataContext="{StaticResource ResourceKey=Dummy}" 
     d:DesignHeight="300" d:DesignWidth="300" Background="White"> 
    <UserControl.Resources> 
     <entity:Media x:Key="Dummy" Title="Akira"></entity:Media> 
    </UserControl.Resources> 
    <Grid> 
      <TextBlock Text="{Binding Title}"></TextBlock> 
    </Grid> 
</UserControl> 
+0

看看MVVMLight以及它如何解決這個問題。 http://mvvmlight.codeplex.com/ – Alex

+0

我使用Galasoft的MVVM Light Toolkit,你能更具體嗎? –

+0

在這種情況下,您應該仔細檢查您是否正確遵循綁定示例。 – Alex

回答

1

您試圖綁定d:DataContext的資源虛擬的,但你缺少綁定關鍵字。請將此行更改爲

d:DataContext="{Binding Source={StaticResource Dummy}} 
+0

我知道它一定是件小事,謝謝。 –

3

我會創建一個MockDataContext模型。這工作相當不錯。這是我有的代碼片段:

<UserControl x:Class="Modules.Core.Views.HeaderView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:viewModels="clr-namespace:Modules.Core.ViewModels" 
      mc:Ignorable="d" 
      d:DesignHeight="100" d:DesignWidth="1024"> 
    <Grid d:DataContext="{d:DesignInstance Type=viewModels:MockHeaderViewModel, IsDesignTimeCreatable=True}"> 

    </Grid> 
</UserControl> 

最大的好處是你可以實際運行一些代碼。例如,我用它在設計時更新標題中的時間,並改變一些字段。你可以立即看到你的綁定工作和你的佈局是否給你足夠的空間。

+0

我有一個非常簡單的控件,主要是顯示數據,但這是好東西。將在我使用的更復雜的控件上試用。 –

+0

我喜歡這個。能夠爲xaml設置一個只有設計時間的視圖模型非常棒。謝謝! – Corgalore

相關問題