Xamarin表单-获取自定义控件运行时(使用valueconverter?) [英] Xamarin Forms - Get custom control runtime (with valueconverter?)
问题描述
我有一个Xamarin表单应用程序.它有一个页面.该页面具有一个列表视图. listviews绑定到源列表.我想要的是根据列表中每个项目都具有的特定属性来决定使用哪种自定义控件.我怎样才能做到这一点?
I have a Xamarin forms application. It has a page. That page has a listview. The listviews is bind to a sourcelist. What I want is to decide what custom control to use based on a specific property that each item in the list has. How can I achieve this?
还要求自定义控件绑定到列表中的值.
It is also required that the custom control binds to values from the list.
我尝试了以下操作:
<ContentView Content="{Binding IsPriority, Converter={StaticResource RoadIndicatorBackgroundValueConverter}}" ControlText="{Binding TowingOrder.Location.RoadIndicator}"></ContentView>
那是行不通的,因为我无法与特定自定义控件的ControlText绑定.
That won't work because I can't bind with the ControlText of the specific custom control.
上图显示了问题. A1,A50和蓝色图标都是自定义控件,不是图像.这些自定义控件还有更多.我不能只制作10个数据模板并复制粘贴所有内容吗?那将是很多重复的代码.我不能只在数据模板中使用基础对象,然后将属性与转换器绑定,然后使用开关/案例来确定要返回哪个自定义控件?
The image above shows the problem. The A1, A50 and the blue icons are all custom controls, those aren't images. There are some more of these custom controls. I can't just make 10 datatemplates right and copy paste everything? That would be so much duplicate code. Can't I just use a base object in a datatemplate, and bind a property with a converter and use a switch/case to decide which custom control to return?
推荐答案
在运行时绑定整个布局的内容不是一个好的设计.
It is not a good design to binding the content of whole layout in runtime.
从屏幕截图中可以看出,每个单元格都很相似.因此,您可以创建一个基本的自定义控件并预先定义所有控件,然后在代码中绑定属性IsVisible
.然后设置其值以控制应显示哪个元素.
From the screen shot , it seems that each cell are similar .So you could create a base Custom Control and define all controls in advance and binding the propertyIsVisible
in code behind . Then set its value to control which element should been displayed .
如果单元格之间有很多挖掘差异,请使用 DataTemplateSelector 更好.
If there are do many dig differences between the cells , use DataTemplateSelector is better .
这篇关于Xamarin表单-获取自定义控件运行时(使用valueconverter?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!