C# 将文本绑定到数组中对象中的嵌套成员
我挑战自己在WPF中提出申请,我以前从未尝试过:) 也许这很明显,但我不明白我的错误在哪里 MyRecipePage正确显示零件C# 将文本绑定到数组中对象中的嵌套成员,c#,wpf,xaml,data-binding,nested,C#,Wpf,Xaml,Data Binding,Nested,我挑战自己在WPF中提出申请,我以前从未尝试过:) 也许这很明显,但我不明白我的错误在哪里 MyRecipePage正确显示零件,但对于(或1)不显示任何内容 我没有错,什么都没有 它是由Recipe.AddDetails()不发送任何PropertyChanged事件引起的吗 提前感谢您的帮助:) RecipePage.xaml ... <Grid> <Button HorizontalAlignment="Left" Width="112" Height="29"
,但对于
(或1)不显示任何内容
我没有错,什么都没有
它是由Recipe.AddDetails()
不发送任何PropertyChanged
事件引起的吗
提前感谢您的帮助:)
RecipePage.xaml
...
<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>
...
页面创建:
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
public partial class RecipePage : Page
{
public RecipePage(Recipe recipe)
{
InitializeComponent();
DataContext = recipe;
}
}
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);
}
}
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;
}
}
RecipedDetails.cs
public partial class RecipePage : Page
{
public RecipePage(Recipe recipe)
{
InitializeComponent();
DataContext = recipe;
}
}
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);
}
}
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(已安装)
主要问题是向
详细信息添加元素不会引发CollectionChanged
事件。此事件在更改时由ObservableCollection
自动引发,因此您希望使用该事件而不是数组:
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);
}
}
你知道为什么我的详细信息项目是垂直堆叠而不是水平堆叠的,而我的项目控件父面板是
?你的堆栈面板
只定向其内容,而不是项目面板的内容。但是有一个解决办法:看哦,好的!我认为ItemsControl
只在父容器中生成项。非常感谢。