【问题标题】:Data binding to a list inside a data bound object数据绑定到数据绑定对象内的列表
【发布时间】:2008-12-07 07:20:18
【问题描述】:

我是 WPF 和数据绑定的新手,所以希望我能用足够的细节来解释我遇到的问题以获得一些帮助。

我有一个数据绑定到窗口的对象列表。假设它是一个食谱列表。我设法让应用程序在列表框中显示每个配方的一些详细信息,并在各种文本框中显示所选配方的更多详细信息。我的问题是,我在选择配方时,我有一份我想要在另一个列表框中显示的成分列表,但我无法解决如何获取数据绑定到工作。

我的数据类看起来像:

public class Recipes : ObservableCollection<RecipeDetails>
{
}

public class RecipeDetails
{
    public string Name { get; set; }
    public string Description { get; set; }
    public List<RecipeIngredient> Ingredients;
}

public class RecipeIngredient
{
    public string IngredientName { get; set; }
}


public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        m_recipes = new Recipes();
        // m_recipes initialisation
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DataContext = m_recipes;
    }

    private Recipes m_recipes;
}

我的数据绑定尝试(在 XAML 中)看起来像:

<Window x:Class="RecipeWindow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Recipe Window" Height="402.687" Width="532.674" Loaded="Window_Loaded">
    <Grid>
        <ListBox Margin="12,58.176,0,16.362" Name="recipeListBox" Width="209.07" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" FontSize="14" Padding="5" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <StackPanel Margin="245.43,58.176,12,47.268" Name="detailsPanel">
            <TextBox Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}" Width="140" />
            <TextBox Text="{Binding Description,UpdateSourceTrigger=PropertyChanged}" Width="140" />
            <ListBox Name="ingredientListBox" Width="209.07" ItemsSource="{Binding Ingredients}" Height="118.17">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding IngredientName}" Padding="5" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Grid>
</Window>

但是当我尝试运行这段代码时,成分列表框总是空的。谁能告诉我我做错了什么?

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:

    问题在于您声明和初始化成分列表的方式,使其成为 ObservableCollection 并仅在构造函数中初始化成分集合,如下所示

     public class RecipeDetails
    {
        public RecipeDetails()
        {
            _ingredients = new ObservableCollection<RecipeIngredient>();
        }
    
        public string Name { get; set; }
        public string Description { get; set; }
    
        private ObservableCollection<RecipeIngredient> _ingredients;
    
        private ObservableCollection<RecipeIngredient> Ingredients
        {
            get { return _ingredients; }
        }
    }
    

    还有一点要添加到您的方法中,建议在此类上使用INotifyPropertyChanged,这样当您在 TextBox 中键入内容时,在这里可以轻松实现双向绑定。

    【讨论】:

    • 工作就像一个魅力,除了我不需要在构造函数中初始化它。使其成为公共属性,例如: public ObservableCollection Ingredients { get;放; } 似乎工作正常。
    • Setter on ObservableCollection 可能会给您更新问题,如果您稍后在 DatBinding 之后更改它。
    猜你喜欢
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多