简体   繁体   中英

Design-time data isn't being shown?

Visual Studio 2013 XAML Editor doesn't show my dummy data. If I set makeDummy = true and run, I see the corrent data (two fields, with right labels). But it is not shown in the designer. How to see the dummy data in design-mode?

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; }
}

Please ignore the attribute, the interface inheritence, the event and method - they just implement the behavior required for 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; }
}

This is the 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>

The designer instantiates your base class ( Window ), not your codebehind class.

The simplest solution is to make a static dummy model property, then write

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM