如何在 Silverlight MVVM 中绑定子对象列表
在 Silverlight 中,MVVM 我必须创建一个属性窗口,但我只有一个 List
对象,其中 AProperty
是一个带有一些子类的抽象类。
我想将其绑定到 Silverlight 控件(但绑定到哪一个?),但有一些条件:
- 某些子属性必须显示为文本框,某些子属性必须显示为复选框,某些子属性必须显示为组合框。它来自动态类型。
- AProperty 类有一个
PropertyGroup
和一个Name
字段。顺序必须按字母顺序(PropertyGroup > Name
)
有什么想法或工作示例吗?
我的代码:
public abstract class AProperty {
private String _Name;
private String _Caption;
private String _PropertyGroup;
public String Name {
get { return _Name; }
set { _Name = value; }
}
public String Caption {
get { return _Caption; }
set { _Caption = value; }
}
public String PropertyGroup {
get { return _PropertyGroup; }
set { _PropertyGroup = value; }
}
}
List<AProperty> Properties;
和 xaml:
<ListBox ItemsSource="{Binding ... Properties ...}">
<!-- Here order the items -->
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="250"
Text="{Binding Path=Caption}" />
<!-- And here need I a textbox, or a checkbox, or a combobox anyway -->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我发现值转换器仅适用于控件属性,而不适用于整个堆栈面板内容。
In Silverlight, MVVM I have to create a property window, but I have just aList<AProperty>
object, where AProperty
is an abstract class with some child classes.
I want to bind it to a Silverlight control (but to which one?), with some conditions:
- some child-properties must be shown as a textbox, some as a checkbox, and some as a combobox. It comes from the dynamic type.
- the AProperty class has a
PropertyGroup
and aName
field. The order must be alphabetic (PropertyGroup > Name
)
Any idea or working example?
My code:
public abstract class AProperty {
private String _Name;
private String _Caption;
private String _PropertyGroup;
public String Name {
get { return _Name; }
set { _Name = value; }
}
public String Caption {
get { return _Caption; }
set { _Caption = value; }
}
public String PropertyGroup {
get { return _PropertyGroup; }
set { _PropertyGroup = value; }
}
}
List<AProperty> Properties;
And the xaml:
<ListBox ItemsSource="{Binding ... Properties ...}">
<!-- Here order the items -->
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="250"
Text="{Binding Path=Caption}" />
<!-- And here need I a textbox, or a checkbox, or a combobox anyway -->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I've found value converters just for a control attribute, not for a whole stackpanel content.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用值转换器来检查对象的类型并返回正确的控件类型。
您有任何示例代码以便我可以给出更好的示例吗?
You could use a value converter to check what Type the object is and return the correct control type.
Do you have any sample code so I could give a better example?
安迪提到的 ValueConverter 应该可以工作。在您的 Textblock 下面有一个 ContentPresenter,并绑定它的内容。
内容={绑定,ConverterParameter={StaticResource NAMEOFCONVERTER}}。
与属性的绑定为空,因为您已经拥有想要作为上下文的对象。然后只需检查类型并返回您想要的控件即可。您可能想要制作只是文本块等的用户控件,以便您可以在那里设置绑定,否则您将不得不在代码中执行此操作。
The ValueConverter Andy mentions should work. Underneath your Textblock have a ContentPresenter, and bind it's Content.
Content={Binding , ConverterParameter={StaticResource NAMEOFCONVERTER}}.
The binding to the property is empty since you already have the object you want as the context. Then just check the type and return the control you want. you may want to make usercontrol's that are just Textblocks, etc. so that you can set the binding there, otherwise you will have to do it in code.
谢谢杰西和安迪,这是解决方案
转换器:
和xaml代码:
两篇文章:
http://shawnoster.com/blog/post/Dynamic -Silverlight-TreeView.aspx 中的图标
http://www.silverlightshow.net/items/Silverlight-2-Getting-to-the-ListBoxItems-in-a-ListBox.aspx
Thanks Jesse and Andy, here is the solution
The converter:
And the xaml code:
Two artikel:
http://shawnoster.com/blog/post/Dynamic-Icons-in-the-Silverlight-TreeView.aspx
http://www.silverlightshow.net/items/Silverlight-2-Getting-to-the-ListBoxItems-in-a-ListBox.aspx