其他数据模板中的一个数据模板 [英] One Data Template inside Other data template

查看:78
本文介绍了其他数据模板中的一个数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


参考以下主题,


https://social.msdn.microsoft.com/Forums / Silverlight的/ EN-US / bb1d29c9-b875-4bb7-8782-3f42a637c924 /动态决定 - 什么 - 用户控制 - 使用 - 一些模板功能于XAML添加论坛= WPF和放大器;教授为必填


我想进行重构作为一些控件(在上面例如。标签将重复所有模板)。我想为通用控件创建一个通用模板,为特定类型创建独特控件的单独数据模板。 


我尝试过以下代码, 

< ItemsControl.Resources> 
< DataTemplate DataType =" {x:Type local:Entry}">
< StackPanel Orientation =" Horizo​​ntal"高度= QUOT; 40">
< Label Content =" {Binding Text}"宽度= QUOT; 250"高度= QUOT; 25"余量= QUOT; 2英寸VerticalAlignment = QUOT;中心" />
< Button Content =" X"宽度= QUOT; 25"高度= QUOT; 25"余量= QUOT; 2英寸/>
< Button Content =" Y"宽度= QUOT; 25"高度= QUOT; 25"余量= QUOT; 2英寸/>
< / StackPanel>
< / DataTemplate>
< DataTemplate DataType =" {x:Type local:CheckBoxEntry}">
< StackPanel Orientation =" Horizo​​ntal"高度= QUOT; 40">
< CheckBox IsChecked =" {Binding IsChecked,Mode = TwoWay}"余量= QUOT; 2英寸VerticalAlignment = QUOT;中心" />
< / StackPanel>
< / DataTemplate>
< DataTemplate DataType =" {x:Type local:ComboBoxEntry}">
< StackPanel Orientation =" Horizo​​ntal"高度= QUOT; 40">
< ComboBox SelectedItem =" {Binding Text}"宽度= QUOT; 250"高度= QUOT; 25"
ItemsSource =" {Binding SelectionEntities}"余量= QUOT; 2英寸VerticalAlignment = QUOT;中心">
< i:Interaction.Triggers>
< i:EventTrigger EventName =" DropDownOpened">
< i:InvokeCommandAction Command =" {Binding DOP}" />
< / i:EventTrigger>
< i:EventTrigger EventName =" SelectionChanged">
< i:InvokeCommandAction Command =" {Binding DOP}" />
< / i:EventTrigger>
< / i:Interaction.Triggers>
< / ComboBox>
< / StackPanel>
< / DataTemplate>

< /ItemsControl.Resources>

但问题是,我无法看到为类型Entry定义的那些通用控件。


所以我想创建一个通用数据模板,我想拥有子模板。


这可能吗?!如果是这样,请告诉我如何。


问候,


Sasikala





解决方案


也许你可以试试这个:

< ItemsControl.Resources> 
< DataTemplate x:Key =" BaseDataTemplate">
< StackPanel Orientation =" Horizo​​ntal"高度= QUOT; 40">
< Label Content =" {Binding Text}"宽度= QUOT; 250"高度= QUOT; 25"余量= QUOT; 2英寸VerticalAlignment = QUOT;中心" />
< Button Content =" X"宽度= QUOT; 25"高度= QUOT; 25"余量= QUOT; 2英寸/>
< Button Content =" Y"宽度= QUOT; 25"高度= QUOT; 25"余量= QUOT; 2英寸/>
< / StackPanel>
< / DataTemplate>

< DataTemplate DataType =" {x:Type local:Entry}">
< ContentPresenter Content =" {Binding}" ContentTemplate =" {StaticResource BaseDataTemplate}" />
< / DataTemplate>
< DataTemplate DataType =" {x:Type local:CheckBoxEntry}">
< StackPanel Orientation =" Horizo​​ntal"高度= QUOT; 40">
< ContentPresenter Content =" {Binding}" ContentTemplate =" {StaticResource BaseDataTemplate}" />
< CheckBox IsChecked =" {Binding IsChecked,Mode = TwoWay}"余量= QUOT; 2英寸VerticalAlignment = QUOT;中心" />
<! - others - >
< / StackPanel>
< / DataTemplate>
.....
< /ItemsControl.Resources>

最好的问候,


Bob


Hi,

With reference to the below thread,

https://social.msdn.microsoft.com/Forums/silverlight/en-US/bb1d29c9-b875-4bb7-8782-3f42a637c924/dynamically-deciding-what-user-control-to-add-using-some-templates-in-xaml?forum=wpf&prof=required

I want to do refactoring as some of the controls(in the above eg. Label will get repeated for all template). I want to create one common template for generic controls and separate data template for unique controls for the particular type. 

I tried like the below code, 

<ItemsControl.Resources>
                        <DataTemplate DataType="{x:Type local:Entry}">
                            <StackPanel Orientation="Horizontal" Height="40">
                                <Label Content="{Binding Text}" Width="250" Height="25" Margin="2" VerticalAlignment="Center"/>
                                <Button Content="X" Width="25" Height="25" Margin="2"/>
                                <Button Content="Y" Width="25" Height="25" Margin="2"/>
                            </StackPanel>
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type local:CheckBoxEntry}">
                            <StackPanel Orientation="Horizontal" Height="40">
                                <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Margin="2" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type local:ComboBoxEntry}">
                            <StackPanel Orientation="Horizontal" Height="40">
                                <ComboBox SelectedItem="{Binding Text}" Width="250" Height="25" 
                                      ItemsSource="{Binding SelectionEntities}" Margin="2" VerticalAlignment="Center">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="DropDownOpened">
                                            <i:InvokeCommandAction Command="{Binding DOP}"/>
                                        </i:EventTrigger>
                                        <i:EventTrigger EventName="SelectionChanged">
                                            <i:InvokeCommandAction Command="{Binding DOP}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </ComboBox>
                            </StackPanel>
                        </DataTemplate>

                    </ItemsControl.Resources>

But problem with this is, i am not able to see those generic controls which is defined for type Entry.

So i want to create one generic data template within that i want to have child templates.

Can this possible?! if so please tell me how.

Regards,

Sasikala


解决方案

Hi,

Maybe you can try this:

   <ItemsControl.Resources>
                <DataTemplate x:Key="BaseDataTemplate">
                    <StackPanel Orientation="Horizontal" Height="40">
                        <Label Content="{Binding Text}" Width="250" Height="25" Margin="2" VerticalAlignment="Center"/>
                        <Button Content="X" Width="25" Height="25" Margin="2"/>
                        <Button Content="Y" Width="25" Height="25" Margin="2"/>
                    </StackPanel>
                </DataTemplate>
                
                <DataTemplate  DataType="{x:Type local:Entry}">
                    <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource BaseDataTemplate}"/>
                </DataTemplate>
                <DataTemplate DataType="{x:Type local:CheckBoxEntry}">
                    <StackPanel Orientation="Horizontal" Height="40">
                        <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource BaseDataTemplate}"/>
                        <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Margin="2" VerticalAlignment="Center"/>
                        <!--others-->
                    </StackPanel>
                </DataTemplate>             
               .....
            </ItemsControl.Resources>

Best Regards,

Bob


这篇关于其他数据模板中的一个数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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