从DataTemplate进行WPF数据绑定-多对多关系
我有一个WPF控件用于布置项目,并将其设置为从DataTemplate进行WPF数据绑定-多对多关系,wpf,many-to-many,datatemplate,Wpf,Many To Many,Datatemplate,我有一个WPF控件用于布置项目,并将其设置为 ItemsSource="{Binding item_portfolio}" DataContext="{Binding SelectedItem}" 在布局控件的资源中,我为其项设置了模板: <Style> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate>
ItemsSource="{Binding item_portfolio}" DataContext="{Binding SelectedItem}"
在布局控件的资源中,我为其项设置了模板:
<Style>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding portfolio.PortfolioName}" />
<ListView ItemsSource="{Binding ?}">
</ListView>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
DataTemplate
下的TextBlock
绑定工作正常。
但是,我不知道如何绑定列表视图
项目资源
,以便它显示属于该组合的所有项目对象
编辑:
我想在布局控件中列出公文包。然后在公文包下,我想显示它包含哪些项目。下图显示了SelectedItem为Item 1时的UI
(首先,我显示此项目有哪些投资组合。这将给出3个投资组合。这可以正常工作。然后在UI上,我单击投资组合3,它将显示属于该投资组合的所有项目(项目1和项目2)。这还不起作用。)我们在代码中对数据模型建模的方式与在数据库中建模的方式不同。我们不会用“连接类”来模拟“连接表”。相反,我们创建分层数据模型。在您的情况下,应该有一个
项
类,该类具有类型为公文包
的公文包
属性。不应该有ItemPortfolio
类,因为它没有任何意义。那么,您的绑定应该是这样的`:
<ItemsControl ItemsSource="{Binding Items}" ... />
显然,要使这项工作正常,您需要在项
类中定义这些属性
更新>>>
在这种情况下,使用类型为ObservableCollection
的Items
属性创建一个公文包
类,并如下设置ItemsSource
:
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding PortfolioName}" />
<ListView ItemsSource="{Binding Portfolio}">
</ListView>
</StackPanel>
</DataTemplate>
<ItemsControl ItemsSource="{Binding Item.Portfolios}" ... />
为了澄清,您的项
类还应该具有类型为ObservableCollection
的集合属性。。。这将导致某些数据对象重复,但在WPF中,更重要的是为您的视图提供它们所需的数据。我在这里使用了扩展器控件,因为您希望PortfolioName作为标题并单击
PortfolioName您想显示相应的PortfolioItemList
xaml代码
<ItemsControl MaxHeight="300" ItemsSource="{Binding PortfolioInfo}" ScrollViewer.VerticalScrollBarVisibility="Auto" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander>
<Expander.Header>
<TextBlock Background="YellowGreen" Text="{Binding name}"></TextBlock>
</Expander.Header>
<Expander.Content>
<ListBox ItemsSource="{Binding ItemList}"></ListBox>
</Expander.Content>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public partial class MainWindow : Window
{
public ObservableCollection<Portfolio> PortfolioInfo { get; set; }
public MainWindow()
{
InitializeComponent();
PortfolioInfo = new ObservableCollection<Portfolio>()
{
new Portfolio(){name="Portfolio1", ItemList= new List<string>(){"Item1","Item2","Item3"}},
new Portfolio(){name="Portfolio2", ItemList= new List<string>(){"Item1","Item2","Item3"}},
new Portfolio(){name="Portfolio3", ItemList= new List<string>(){"Item1","Item2","Item3"}}
};
this.DataContext = this;
}
}
public class Portfolio
{
public string name { get; set; }
public List<string> ItemList { get; set; }
}
C#代码
<ItemsControl MaxHeight="300" ItemsSource="{Binding PortfolioInfo}" ScrollViewer.VerticalScrollBarVisibility="Auto" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander>
<Expander.Header>
<TextBlock Background="YellowGreen" Text="{Binding name}"></TextBlock>
</Expander.Header>
<Expander.Content>
<ListBox ItemsSource="{Binding ItemList}"></ListBox>
</Expander.Content>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public partial class MainWindow : Window
{
public ObservableCollection<Portfolio> PortfolioInfo { get; set; }
public MainWindow()
{
InitializeComponent();
PortfolioInfo = new ObservableCollection<Portfolio>()
{
new Portfolio(){name="Portfolio1", ItemList= new List<string>(){"Item1","Item2","Item3"}},
new Portfolio(){name="Portfolio2", ItemList= new List<string>(){"Item1","Item2","Item3"}},
new Portfolio(){name="Portfolio3", ItemList= new List<string>(){"Item1","Item2","Item3"}}
};
this.DataContext = this;
}
}
public class Portfolio
{
public string name { get; set; }
public List<string> ItemList { get; set; }
}
公共部分类主窗口:窗口
{
公共ObservableCollection端口组合信息{get;set;}
公共主窗口()
{
初始化组件();
PortfolioInfo=新的ObservableCollection()
{
新建公文包(){name=“Portfolio1”,ItemList=newlist(){“Item1”,“Item2”,“Item3”},
新建公文包(){name=“Portfolio2”,ItemList=newlist(){“Item1”、“Item2”、“Item3”},
新建公文包(){name=“Portfolio3”,ItemList=newlist(){“Item1”、“Item2”、“Item3”}
};
this.DataContext=this;
}
}
公课档案
{
公共字符串名称{get;set;}
公共列表项列表{get;set;}
}
注意:编辑样式/模板以获得所需的UI
r结果
在my item.item\u公文包绑定中,item\u公文包是实体框架导航属性。这个中间的表应该作为一个类删除(如您所提到的),但由于某种原因,实体框架在从数据库创建模型时没有这样做,尽管它在项目和投资组合之间存在多对多关系。我不想绑定到项目列表。相反,我想绑定到投资组合列表。更新我的问题以澄清。