WPF MVVM创建动态控件 [英] WPF MVVM Creating Dynamic controls
问题描述
WPF MVVM创建动态控制 - 我有,我有工作的(你可以说像一个SQL Server作业)的详细信息网格。
WPF MVVM Creating Dynamic controls - I have a grid on which I have a job's (you can say something like a sql server job) details.
现在的每一项工作有可能是'N'一些工作变量。当我取的记录工作也得到工作变量,是名称 - 值对,其中值可以是集合或日期时间值,甚至一个int或一个字符串,这个集合。
Now for every job there could be 'n' number of job variables. When I fetch the record for a job it gets this collection of Job variables which are Name-value pair, where value could be a collection or a datetime value or even an int or a string.
现在我试图在这里实现的是: - 如果运行变量是一个日期时间,然后我需要一个datepicker - 如果它是一个INT /字符串我需要一个文本框 - 如果它是一个集合,然后组合框。 - 这是一个位域,然后一个复选框
Now what I am trying to achieve here is : -- If the run variable is a datetime then I need a datepicker -- If it is a int/String I need a text box -- If it is a collection then a combo box . -- it it is a bit field then a check box
我不知道如何实现它,因为这些数值可以不同,每一个作业。
I am not sure how to achieve it since these values can differ for every single job.
推荐答案
我假设你会放一些种类的物体重新presenting这些名称/值对成的ItemsControl
通过设置的ItemsSource
属性。
I am assuming that you will put some kind of objects representing these name/value pairs into an ItemsControl
by setting its ItemsSource
property.
有一对夫妇的解决方案,你可以使用。
There are a couple of solutions you can use.
该方法涉及暴露通过 YourPropertyType
属性作为一个字符串类型您的每一个对象的。您可以设置 ItemTemplate中
你的的ItemsControl
来承载模板的 ContentControl中
。该 ContentControl中
本身都有其的ContentTemplate
动态与触发器选择。
This approach involves exposing the "type" of each of your objects through the YourPropertyType
property as a string. You will set the ItemTemplate
of your ItemsControl
to a template which hosts a ContentControl
. The ContentControl
itself will have its ContentTemplate
selected dynamically with triggers.
所有这一切都可以在XAML声明来完成。
All of this can be done declaratively in XAML.
我假设你有进一步的的DataTemplates
的 DefaultTemplate
(它可以为空), IntegerTemplate
, StringTemplate的
等勾画出视觉树每种情况。
I am assuming you have further DataTemplates
named DefaultTemplate
(this can be empty), IntegerTemplate
, StringTemplate
, etc to sketch out the visual tree for each case.
这随后将是 ItemsControl.ItemTemplate
:
<DataTemplate>
<ContentControl
x:Name="MyContentControl"
Content="{Binding}"
ContentTemplate="{StaticResource DefaultTemplate}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding YourPropertyType}" Value="Integer">
<Setter TargetName="MyContentControl" Property="ContentTemplate"
Value="{StaticResource IntegerTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding YourPropertyType}" Value="String">
<Setter TargetName="MyContentControl" Property="ContentTemplate"
Value="{StaticResource StringTemplate}" />
</DataTrigger>
<!-- and so on -->
</DataTemplate.Triggers>
</DataTemplate>
使用 DataTemplateSelector
:
此方法需要code-背后,却不会强迫你揭露类型每个名称/值对字符串,它可以让你选择用更复杂的逻辑来使用的模板。
Using a DataTemplateSelector
:
This approach requires code-behind, but it does not force you to expose the "type" of each name/value pair as a string and it allows you to choose which template to use with much more complex logic.
这涉及到创建一个类是模板选择:
It involves creating a class to be the template selector:
class YourObjectDataTemplateSelector : DataTemplateSelector
{
public DataTemplate DefaultTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var yourObject = (YourObjectType) item;
// Get hold of a DataTemplate based on any attribute of item
var templateToUse = this.DefaultTemplate;
return templateToUse;
}
}
然后,你需要在某个地方实例化一个模板选择器(比方说,在你的用户控件
)
<UserControl.Resources>
<localNamespace:YourObjectDataTemplateSelector
x:Key="TemplateSelector"
DefaultTemplate="{StaticResource DefaultTemplate}"
/>
</UserControl.Resources>
请注意,我接触的 YourObjectDataTemplateSelector
A DefaultTemplate
属性并将其设置为XAML的模板。在实践中,你会增加,当它在 YourObjectDataTemplateSelector
定义类型的DataTemplate
的更多的属性,而配置模板选择到控制的资源字典。这使您可以使用的StaticResource
标记扩展在XAML直接设置每个个案的模板。
Notice that I exposed a DefaultTemplate
property from YourObjectDataTemplateSelector
and set that to a template from XAML. In practice, you would define more properties of type DataTemplate
on YourObjectDataTemplateSelector
, and "configure" the template selector when adding it into the resource dictionary of your control. This allows you to directly set the templates for each case using the StaticResource
markup extension from XAML.
最后,线模板选择你的的ItemsControl
:
Finally, wire the template selector to your ItemsControl
:
<ItemsControl
ItemsSource="..."
ItemTemplateSelector={StaticResource TemplateSelector}"
/>
这篇关于WPF MVVM创建动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!