在WPF中显示未知数量的对象 [英] Showing unknown number of objects in WPF

查看:84
本文介绍了在WPF中显示未知数量的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


假设我有以下代码:

Hi
Suppose I have these codes:

class Friend
{
    public string Name;
    public Icon icon;
}

List<Friend> myFirends=....;



现在,我想在WPF中以这种模式显示"myFirends"的所有成员:



Now i want to show all members of "myFirends" in this pattern in WPF:

<TextBlock Text="{Binding path=Name}">
<Image Source="{Binding path=icon}">





How is it possible?

推荐答案

这完全取决于您想要作为TextBlockImage实例的容器的容器以及如何展示这些FrameworkElements.您需要使用从System.Windows.Controls.ItemsControl继承的类型的容器类,请参见 http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx [ http://msdn.microsoft.com/en-us/library/bb613548.aspx [ ^ ].

了解绑定:
http://msdn.microsoft.com/en-us/library/ms752347.aspx [ ^ ],
http://msdn.microsoft.com/en-us/magazine/cc163299.aspx [ ^ ];
绑定ItemsControl的示例: http://www.galasoft.ch/mydotnet/articles/article- 2007041201.aspx [ ^ ].

—SA
It totally depends on what you want to have as a container for those instances of TextBlock and Image and how you want to present these FrameworkElements. You need to use the container class of the type inheriting from the System.Windows.Controls.ItemsControl, see http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx[^]. In this case, you can all/remove/bind arbitrary number of arbitrary objects to such container.

See also http://msdn.microsoft.com/en-us/library/bb613548.aspx[^].

Learn about binding:
http://msdn.microsoft.com/en-us/library/ms752347.aspx[^],
http://msdn.microsoft.com/en-us/magazine/cc163299.aspx[^];
Example of binding of ItemsControl: http://www.galasoft.ch/mydotnet/articles/article-2007041201.aspx[^].

—SA


可以,但是您需要一个类似于上述解决方案的容器.您可以创建一个列表框并按以下方式设置模板:


It is possible but you need a container like the solution above said. You can create a list box and set the Template like this:


<ListBox x:name="myListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>			
<TextBlocktext="{Binding path=Name}">
<Image Source="{Binding path=icon}">
</TextBlock>
</StackPanel>
</DataTemplate>			
</ListBox.ItemTemplate>
</ListBox>

在后面的代码中,您可以设置myListBox.ItemsSource = myFriends

In the code behind you can set the myListBox.ItemsSource = myFriends


,您需要一个容器.对象渲染的方式可以在容器项目模板上,也可以在其他位置设置对象渲染的模板.

例如:

As said you need a container. How you have the object render can be on the containers item template or you can set the objects rendering tempalte elsewhere.

For example:

<datatemplate datatype="{x:Type">
  <stackpanel>
   <textblock text="{Binding" path="Name}/">
   <image source="{Binding" path="Icon}/">
  </image></textblock></stackpanel>
</datatemplate>



只要将其从资源字典中拉入或在控制资源中进行了定义,它将呈现该对象.另一种选择是将朋友"视图构建为用户控件.这样,您可以使用设计器对其进行修改.如果这样做,您将执行以下操作:



As long as this was pulled in from a resource dictionary or defined in that controls resources it will render the object. Another option is to built the "Friend" view as a user control. That way you can modify it using the designer. If you do that you would do something like this:

<datatemplate>
   <local:friendview xmlns:local="#unknown" />
</datatemplate>



FriendView是根据需要设置的用户控件.

然后只需设置容器(正如其他人指出的那样)



Where FriendView is a usercontrol set up as you want.

Then just set up your container (as others have pointed out)

 <listbox itemsource="{Binding" path="MyFriends}/">
</listbox>



确保MyFriends是公开的(您将其设为私有)



Make sure MyFriends is public (you have it as private)


这篇关于在WPF中显示未知数量的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆