繁体   English   中英

没有显示设计时数据?

[英]Design-time data isn't being shown?

Visual Studio 2013 XAML编辑器不显示我的虚拟数据。 如果将makeDummy = true设置并运行, 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>

设计器实例化您的基类( Window ),而不是您的代码隐藏类。

最简单的解决方案是制作一个静态虚拟模型属性,然后编写

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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