Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XAML中进行数据绑定以便在设计器视图中工作?_C#_Wpf_Visual Studio 2010_Visual Studio_Xaml - Fatal编程技术网

C# 在XAML中进行数据绑定以便在设计器视图中工作?

C# 在XAML中进行数据绑定以便在设计器视图中工作?,c#,wpf,visual-studio-2010,visual-studio,xaml,C#,Wpf,Visual Studio 2010,Visual Studio,Xaml,我很难理解如何将我的歌曲列表数据绑定到列表框,而无需在代码隐藏中设置项资源。 虽然它可以工作,但我真的希望看到列表在liveview设计器中工作 namespace App5 { class SongsData { public string Title { get; set; } public string Lyrics { get; set; } } } 名称空间App5 { 宋斯达级 { 公共字符串标题{get;set;} 公共字符串

我很难理解如何将我的歌曲
列表
数据绑定到
列表框
,而无需在代码隐藏中设置
项资源
。 虽然它可以工作,但我真的希望看到列表在liveview设计器中工作

namespace App5 { class SongsData { public string Title { get; set; } public string Lyrics { get; set; } } } 名称空间App5 { 宋斯达级 { 公共字符串标题{get;set;} 公共字符串歌词{get;set;} } } 在我的MainPage.xaml.cs中:

public MainPage() { this.InitializeComponent(); List Songs = new List(); Songs.Add(new SongsData() { Title = "Your Song", Lyrics = "It's a little bit funny.." }); Songs.Add(new SongsData() { Title = "Rocket Man", Lyrics = "I'm the Rocket Maaaan.." }); SongsListBox.ItemsSource = Songs; } 公共主页() { this.InitializeComponent(); 列表歌曲=新列表(); somes.Add(new SongsData(){Title=“你的歌”,歌词=“有点滑稽…”); Songs.Add(新歌曲Data(){Title=“Rocket Man”,歌词=“我是Rocket Maaan…”); SongsListBox.ItemsSource=歌曲; } 在XAML中,我有一个基本列表框:

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

请一位友好的人帮助我了解要更改什么,以及为什么要更改,以便在Visual Studio的liveview Designer中的
列表框中显示歌曲标题

有了以上内容,我必须调试程序才能在
列表框中查看歌曲标题


非常感谢advanced。

您基本上需要对数据文件应用DesignData构建时操作。可以在中找到非常全面的演练。

您基本上需要将DesignData构建时操作应用于数据文件。可以在中找到一个非常全面的演练。

一个快速而简单的解决方案是将
列表框
移动到一个新的
用户控件
,将列表初始化放在
用户控件
的构造函数中,然后将
用户控件
的实例添加到主窗体中

例如:

SongListControl.cs:

namespace App5
{
    public parital class SongListControl : userControl
    {
        this.InitializeComponent();

        List Songs = new List();
        Songs.Add(new SongsData() { Title = "Your Song", Lyrics = "It's a little bit funny.." });
        Songs.Add(new SongsData() { Title = "Rocket Man", Lyrics = "I'm the Rocket Maaaan.." });

        SongsListBox.ItemsSource = Songs;
    }
}
SongListControl.xaml:

<UserControl x:Class="App5.SongListControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}" />
            </DataTemplate>
        </ListBox.ItemTemplate>        
    </ListBox>
</UserControl>

然后在主窗口中:

<Window x:Class="App5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:app="clr-namespace:App5"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <app:SongListControl />
    </Grid>
</Window>


构建项目时,构造函数初始化将在主窗口预览中进行。

一个快速而简单的解决方案是将
列表框
移动到新的
用户控件
,将列表初始化放在
用户控件
的构造函数中,然后将
UserControl
的实例添加到主窗体中

例如:

SongListControl.cs:

namespace App5
{
    public parital class SongListControl : userControl
    {
        this.InitializeComponent();

        List Songs = new List();
        Songs.Add(new SongsData() { Title = "Your Song", Lyrics = "It's a little bit funny.." });
        Songs.Add(new SongsData() { Title = "Rocket Man", Lyrics = "I'm the Rocket Maaaan.." });

        SongsListBox.ItemsSource = Songs;
    }
}
SongListControl.xaml:

<UserControl x:Class="App5.SongListControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}" />
            </DataTemplate>
        </ListBox.ItemTemplate>        
    </ListBox>
</UserControl>

然后在主窗口中:

<Window x:Class="App5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:app="clr-namespace:App5"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <app:SongListControl />
    </Grid>
</Window>


构建项目时,构造函数初始化将在主窗口预览中进行。

非常感谢。我一定很笨,因为我不懂演练。这是我必须做的每一步吗?我不想在XAML中声明数据(歌曲标题、歌词等),而是在codebehind中。不要被窃听:)数据声明仍将保留在codebehind中。您只需要更新xaml以添加必要的数据绑定来控制属性。本演练很好地解释了如何进行此操作,所以我在设计器中看到的可能不是我在代码背后声明的数据,而是我制作的一个示例?明白了吗?非常感谢。我一定很笨,因为我不懂演练。这是我必须做的每一步吗?我不想在XAML中声明数据(歌曲标题、歌词等),而是在codebehind中。不要被窃听:)数据声明仍将保留在codebehind中。您只需要更新xaml以添加必要的数据绑定来控制属性。本演练很好地解释了如何进行此操作,所以我在设计器中看到的可能不是我在代码背后声明的数据,而是我制作的一个示例?这是正确的吗?