首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将文本绑定到数组中对象中的嵌套成员

将文本绑定到数组中对象中的嵌套成员
EN

Stack Overflow用户
提问于 2019-03-12 08:14:18
回答 1查看 35关注 0票数 0

我挑战自己让我的应用程序在WPF,我从来没有尝试过:)

也许这是显而易见的,但我不明白我的错误在哪里。

我的RecipePage正确显示part <TextBlock Text="{Binding Name}">,但没有显示<TextBlock Text="{Binding Path=Details[0].PieceName}"> (或1)

我没有错误,只是什么都没有。

是不是由不发送PropertyChanged事件的Recipe.AddDetails()引起的?

提前感谢您的帮助:)

RecipePage.xaml

代码语言:javascript
复制
...
<Grid>
    <Button HorizontalAlignment="Left" Width="112" Height="29" VerticalAlignment="Top" Content="PRECEDENT" Margin="10"/>
    <StackPanel>
        <TextBlock Text="{Binding Name}" Style="{StaticResource h1}"></TextBlock>
        <StackPanel Orientation="Horizontal">
            <StackPanel>
                <TextBlock Text="{Binding Path=Details[0].PieceName}" Style="{StaticResource h2}"></TextBlock>
            ...
            </StackPanel>
            <StackPanel>
                <TextBlock Text="{Binding Path=Details[1].PieceName}" Style="{StaticResource h2}"></TextBlock>
            ...
            </StackPanel>
        </StackPanel>
    </StackPanel>
</Grid>
...

RecipePage.xaml.cs

代码语言:javascript
复制
public partial class RecipePage : Page
{

    public RecipePage(Recipe recipe)
    {
        InitializeComponent();
        DataContext = recipe;
    }
}

页面创建:

代码语言:javascript
复制
    private void OnBourguignonClick(object sender, RoutedEventArgs e)
    {
        var recipe = new Recipe("BOURGUIGNON ou SAUTE", 190, "images/recipes/rotideboeuf.jpg");
        recipe.AddDetails(new RecipeDetails("paleron", 6, 31));
        recipe.AddDetails(new RecipeDetails("sauté maigre", 190, 221));
        ((MainWindow)Application.Current.MainWindow)?.NavigationFrame.NavigationService.Navigate(new RecipePage(recipe));
    }

Recipe.cs

代码语言:javascript
复制
public class Recipe : INotifyPropertyChanged
{
    public string Name { get; set; }

    public int MeatWeightPerPers { get; set; }

    public string ImagePath { get; set; }

    public RecipeDetails[] Details = new RecipeDetails[]{};

    public event PropertyChangedEventHandler PropertyChanged;

    public Recipe(string name, int meatWeight, string image)
    {
        Name = name;
        MeatWeightPerPers = meatWeight;
        ImagePath = image;
    }

    public void AddDetails(RecipeDetails details)
    {
        Details.Append(details);
    }
}

RecipeDetails.cs

代码语言:javascript
复制
public class RecipeDetails : INotifyPropertyChanged
{
    public string PieceName { get; set; }
    public int PieceWeightPerCow { get; set; }
    public int NbPersPerCow { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public RecipeDetails(string pieceName, int pieceWeightPerCow, int nbPersPerCow)
    {
        PieceName = pieceName;
        PieceWeightPerCow= pieceWeightPerCow;
        NbPersPerCow = nbPersPerCow;
    }
}

FodyWeavers.xml (已安装)

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
  <PropertyChanged />
</Weavers>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-12 09:25:32

主要问题是向Details添加元素不会引发CollectionChanged事件。此事件在更改时由ObservableCollection自动引发,因此您需要使用该事件而不是数组:

代码语言:javascript
复制
public class Recipe : INotifyPropertyChanged
{
    public string Name { get; set; }

    public int MeatWeightPerPers { get; set; }

    public string ImagePath { get; set; }

    // Instead of array, use ObservableCollection
    public ObservableCollection<RecipeDetails> Details { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public Recipe(string name, int meatWeight, string image)
    {
        Name = name;
        MeatWeightPerPers = meatWeight;
        ImagePath = image;

        // Initialize Details collection
        Details = new ObservableCollection<RecipeDetails>();
    }

    public void AddDetails(RecipeDetails details)
    {
        Details.Add(details);
    }
}

由于Details是某种类型的列表,因此您可能希望使用ItemsControl而不是单个TextBlock来显示它:

代码语言:javascript
复制
...
<Grid>
    <Button HorizontalAlignment="Left" Width="112" Height="29" VerticalAlignment="Top" Content="PRECEDENT" Margin="10"/>
    <StackPanel>
        <TextBlock Text="{Binding Name}" Style="{StaticResource h1}"></TextBlock>
        <StackPanel Orientation="Horizontal">
            <ItemsControl ItemsSource="{Binding Details}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding PieceName}" Style="{StaticResource h2}" />
                            ...
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
    </StackPanel>
</Grid>
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55112249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档