【问题标题】:Databinding in XAML so it works in the Designer-view?XAML 中的数据绑定,以便它在设计器视图中工作?
【发布时间】:2013-01-08 19:52:51
【问题描述】:

我无法理解如何将我的歌曲List<> 数据绑定到ListBox,而无需在后面的代码中设置ItemsSource。 虽然它可以工作,但我真的很想在 liveview Designer 中看到 List 工作。

命名空间 App5 { 类 SongsData { 公共字符串标题 { 获取;放; } 公共字符串歌词 { 获取;放; } } }

在我的 MainPage.xaml.cs 中:

公共主页() { this.InitializeComponent(); 列出歌曲 = new List(); Songs.Add(new SongsData() { Title = "你的歌", Lyrics = "有点好笑.." }); Songs.Add(new SongsData() { Title = "Rocket Man", Lyrics = "I'm the Rocket Maaaan.." }); SongsListBox.ItemsSource = 歌曲; }

在 XAML 中我有一个基本的 ListBox:

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

请友好的人帮助我了解要更改哪些内容 - 希望是为什么 - 让歌曲标题显示在 Visual Studio 的实时视图设计器中的 ListBox 中?

有了上面我必须调试程序才能看到ListBox中的歌名。

非常感谢。

【问题讨论】:

    标签: c# wpf visual-studio-2010 visual-studio xaml


    【解决方案1】:

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

    【讨论】:

    • 非常感谢。我一定很愚蠢,因为我不理解演练。这是我必须做的每一步吗?我不想在 XAML 中声明数据(歌曲标题、歌词等),而是在代码隐藏中声明。
    • 不要被窃听 :) 数据的声明仍将保留在代码后面。您只需要更新 xaml 以添加必要的数据绑定来控制属性。该演练很好地解释了如何执行此操作
    • 好吧,我最终在 Designer 中看到的实际上并不是我在代码隐藏中声明的数据,而只是我制作的一个示例?这理解正确吗?
    【解决方案2】:

    一个快速简单的解决方案是将您的ListBox 移动到一个新的UserControl,将列表初始化放入UserControl 的构造函数中,然后将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>
    

    当您构建项目时,构造函数初始化将在 MainWindow 预览中进行。

    【讨论】:

      猜你喜欢
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2016-04-05
      相关资源
      最近更新 更多