Подтвердить что ты не робот

Время разработки данных для таблицы данных в xaml

Это может быть глупый вопрос, но можно ли определить некоторые образцы данных как DataContext, чтобы увидеть мой DataTemplate в DesignView?

В настоящий момент мне всегда нужно запускать мое приложение, чтобы узнать, работают ли мои изменения.

например. со следующим кодом DesignView просто показывает пустой список:

 <ListBox x:Name="standardLayoutListBox" ItemsSource="{Binding myListboxItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Grid.Column="0" Content="{Binding text1}" />
                <Label Grid.Column="1" Content="{Binding text2}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
4b9b3361

Ответ 1

public class MyMockClass
{
    public MyMockClass()
    {
        MyListBoxItems.Add(new MyDataClass() { text1 = "test text 1", text2 = "test text 2" });
        MyListBoxItems.Add(new MyDataClass() { text1 = "test text 3", text2 = "test text 4" });
    }
    public ObservableCollection<MyDataClass> MyListBoxItems { get; set; }
}

public class MyDataClass
{
    public string text1 { get; set; }
    public string text2 { get; set; }
}

В вашем XAML

Добавить объявление пространства имен

 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

Добавьте контекстный контекст данных в ресурсы окна/управления

<UserControl.Resources> 
    <local:MyMockClass x:Key="DesignViewModel"/> 
</UserControl.Resources>

Затем измените свой список для ссылки на объект времени разработки

<ListBox x:Name="standardLayoutListBox" 
 d:DataContext="{Binding Source={StaticResource DesignViewModel}}"
ItemsSource="{Binding MyListBoxItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Grid.Column="0" Content="{Binding text1}" />
                <Label Grid.Column="1" Content="{Binding text2}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>