2014-09-18 32 views
0

Visual Studio 2013 XAML編輯器不顯示我的虛擬數據。如果我設置makeDummy = true並運行,則會看到相關數據(兩個字段,帶有正確的標籤)。但它沒有在設計師身上顯示。如何在設計模式下查看虛擬數據?設計時數據未顯示?

public partial class InputDialog : Window { 
    public ObservableCollection<KeyValueViewModel> Items { get; set; } 

    public InputDialog(){ 
     bool makeDummy = DesignerProperties.GetIsInDesignMode(this); 
     if (makeDummy) { 
      Items = new ObservableCollection<KeyValueViewModel>()    { 
       new KeyValueViewModel() {Key = "Top:"}, 
       new KeyValueViewModel() {Key = "Middleton:"} 
      }; 
     } 
     InitializeComponent(); 
    } 

    private void CancelButton_Click(object sender, RoutedEventArgs e) { DialogResult = false; } 
    private void OkButton_Click(object sender, RoutedEventArgs e) { DialogResult = true; } 
} 

請忽略的屬性,接口繼承,事件和方法 - 他們只是實施INotifyPropertyChanged所需的行爲:

[NotifyPropertyChangedAspect] 
public class KeyValueViewModel : IRetrievableNotifyPropertyChanged{ 
    public string Value { get; set; } 
    public string Key { get; set; } 
    public event PropertyChangedEventHandler PropertyChanged; 
    public PropertyChangedEventHandler GetPropertyChangedEventHandler() { return PropertyChanged; } 
} 

這是XAML:

<Window x:Class="Dre.InputDialog" 
     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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     d:DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     FocusManager.FocusedElement="{Binding ElementName=FieldsContainerGrid}" 
     Height="165" Width="341">  
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="127*"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <Grid Name="FieldsContainerGrid" Margin="20,20,20,0" Grid.Row="0" MinWidth="100" MinHeight="50"> 
      <ListView ItemsSource="{Binding Items}"> 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <WrapPanel> 
          <Label Content="{Binding Key}" Background="Red"></Label> 
          <TextBox Text="{Binding Value}"></TextBox> 
         </WrapPanel> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 
     </Grid> 

     <StackPanel Margin="5" Grid.Row="1" HorizontalAlignment="Center" Orientation="Horizontal"> 
      <Button Margin="5" Width="100" Height="20" Click="OkButton_Click" IsDefault="True"> 
       Save 
      </Button> 
      <Button Margin="5" Width="100" Height="20" Click="CancelButton_Click" IsCancel="True"> 
       Cancel 
      </Button> 
     </StackPanel> 
    </Grid> 
</Window> 

回答

2

的設計師實例化您的基類(Window),而不是您的代碼隱藏類。

最簡單的解決方案是讓一個靜態假人模型屬性,然後寫

d:DataContext="{x:Static local:DummyModel.Instance}" 
+0

你是什麼意思? 'Window'沒有什麼可顯示的,'InputDialog'永遠不會被設計者創建? – Tar 2014-09-18 16:54:28

+0

確切地說, – SLaks 2014-09-18 16:57:39

+0

讓我明白這一點 - 設計者創建了「純粹的」Window實例,它根據「XAML」('ListView','Button',等等......)?那麼'DesignerProperties.GetIsInDesignMode(this)'有什麼用? – Tar 2014-09-18 17:02:09