根据类型改变xaml模板 [英] vary a xaml template by type
问题描述
下面是一个DataTemplate的一部分,它定义了一条带if命令(带有图像的按钮)与客户联系的地带。当前为电话联系人定义的命令还有更多,因此我想将其用于其他类型的联系人方法(电子邮件等)。
Below is a piece of a DataTemplate that defines a strip if commands (buttons with images) for contacting clients. Currently defined for telephone contacts, there are several more commands, so I want to reuse this for other types of contact methods (email, etc.)
设计视图模型,只有两件事需要改变:
The way it and the view models behind it are designed, there are only two things that need to vary to do this:
- ContactCommand的图像和工具提示按钮
- 最后一个按钮
似乎最可重复使用的方法是使用整个按钮本身就是一个定义了DataType的DataTemplate,就像在本文的底部一样,但是我并不担心原始DataTemplate将如何使用它。虽然听起来很有希望,但我也从未使用过DataTemplateSelector。
It seems the most reusable approach would be to have the entire button be itself a DataTemplate with a DataType defined like at the bottom of this post, but I am not grokking how the original DataTemplate would consume this. I've also never used a DataTemplateSelector although that sounds promising.
什么是最好的方法?代码看起来如何?
What is the best approach? How would the code look?
干杯,
Berryl
Cheers,
Berryl
<DataTemplate x:Key="TelecomNumbersControlCommands">
<DataTemplate.Resources>
<!-- Image Style -->
<Style TargetType="{x:Type Image}">
<Setter Property="Height" Value="16" />
<Setter Property="Width" Value="16" />
</Style>
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal" Margin="5,0,5,0">
<Button Command="{Binding AddCommand}" >
<Image Source="{resx:Resx Key=Img_Simplicio_Add, ResxName=Presentation.Resources.MasterDetail}" />
<Button.ToolTip>
<TextBlock>
<TextBlock.Text>
<resx:Resx Key="Subject_AddNew_ToolTip" BindingPath="SubjectVm.DisplayName" ResxName="Presentation.Resources.MasterDetail"/>
</TextBlock.Text>
</TextBlock>
</Button.ToolTip>
</Button>
<Button Command="{Binding ContactCommand}" >
<Image Source="{resx:Resx Key=Img_Telephone, ResxName=Smack.Parties.Presentation.Resources.PartyDetailView}" />
<Button.ToolTip>
<TextBlock>
<TextBlock.Text>
<resx:Resx Key="ContactCommand_Telephone_Tooltip" BindingPath="SelectedVm" ResxName="Smack.Parties.Presentation.Resources.PartyDetailView"/>
</TextBlock.Text>
</TextBlock>
</Button.ToolTip>
</Button>
</Button>
<Button Command="{Binding SetDefaultAreaCodeCommand}" >
<Image Source="{resx:Resx Img_Widget, ResxName=Presentation.Resources.MasterDetail}" />
<Button.ToolTip>
<TextBlock>
<TextBlock.Text>
<resx:Resx Key="Subject_Settings" BindingPath="SubjectVm.DisplayName" ResxName="Presentation.Resources.MasterDetail"/>
</TextBlock.Text>
</TextBlock>
</Button.ToolTip>
</Button>
...
</StackPanel>
</DataTemplate>
对于RACHEL
带有按钮的修订按钮隐式数据模板
For RACHEL
Revised Button w/ implicit data templates
<Button Command="{Binding ContactCommand}" >
<Button.Resources>
<DataTemplate DataType="{x:Type CmTypes:TelecomNumberPcmShellVm}">
<Image Source="{resx:Resx Key=Img_Telephone, ResxName=Presentation.Resources.PartyDetailView}" >
<Image.ToolTip>
<TextBlock>
<TextBlock.Text>
<resx:Resx
Key="ContactCommand_Telephone_Tooltip"
BindingPath="SelectedVm" ResxName="Presentation.Resources.PartyDetailView"/>
</TextBlock.Text>
</TextBlock>
</Image.ToolTip>
</Image>
</DataTemplate>
</Button.Resources>
<DataTemplate DataType="{x:Type CmTypes:EmailPcmShellVm}">
<Image Source="{resx:Resx Key=Img_Email, ResxName=Presentation.Resources.PartyDetailView}" >
<Image.ToolTip>
<TextBlock>
<TextBlock.Text>
<resx:Resx
Key="ContactCommand_Email_Tooltip"
BindingPath="SelectedVm" ResxName="Presentation.Resources.PartyDetailView"/>
</TextBlock.Text>
</TextBlock>
</Image.ToolTip>
</Image>
</DataTemplate>
</Button>
Object Model
public class PcmShellVm<TCm> : SatelliteViewModel<Party, HashSet<PartyContactMechanism>>
where TCm : ContactMechanism
{
// commands...
}
public class TelephoneNumberPcmShellVm : PcmShellVm<Telephone>
{
...
}
public class EmailPcmShellVm : PcmShellVm<Email>
{
...
}
对象模型
Object model
public class PcmShellVm<TCm> : SatelliteViewModel<Party, HashSet<PartyContactMechanism>>
where TCm : ContactMechanism
{
// commands...
}
public class TelephoneNumberPcmShellVm : PcmShellVm<Telephone>
{
...
}
public class EmailPcmShellVm : PcmShellVm<Email>
{
...
}
推荐答案
您最后一个代码块中的 DataTemplate
不起作用,因为您将 DataTemplate
放入了 Button.Content
。如果将其放在< Button.Resources>
中,则只要 Button.Content
类型为<$,它就可以工作。 c $ c> CmTypes:TelecomNumberPcmShellVm
Your DataTemplate
in your last code block doesn't work because you are putting the DataTemplate
into Button.Content
. If you put it in <Button.Resources>
, it should work providing Button.Content
is of type CmTypes:TelecomNumberPcmShellVm
此外,您需要切换到 Image.ToolTip
将工具提示添加到图像,因为 Button.ToolTip
不是您的 DataTemplate
的有效属性
In addition, you need to switch to an Image.ToolTip
to add a tooltip to the Image, as Button.ToolTip
is not a valid property for your DataTemplate
<Button Command="{Binding ContactCommand}">
<Button.Resources>
<DataTemplate DataType="{x:Type CmTypes:TelecomNumberPcmShellVm}">
<Image Source="{resx:Resx Key=Img_Telephone, ResxName=Presentation.Resources.PartyDetailView}">
<Image.ToolTip>
<TextBlock>
<TextBlock.Text>
<resx:Resx Key="ContactCommand_Telephone_Tooltip"
BindingPath="SelectedVm"
ResxName="Presentation.Resources.PartyDetailView"/>
</TextBlock.Text>
</TextBlock>
</Image.ToolTip>
</Image>
</DataTemplate>
</Button.Resources>
</Button>
如果您确实想设置 Button.ToolTip
而不是 Image.ToolTip
,那么您可能需要在<$ c $中的 DataTrigger
中进行设置c> Button.Style 。通常对于这种情况,我有一个返回 typeof(value)
的转换器,所以我的 DataTrigger
看起来像这样:
If you actually do want to set Button.ToolTip
instead of Image.ToolTip
, then you will probably need to set it in a DataTrigger
in Button.Style
. Usually for this kind of scenario I have a converter that returns typeof(value)
, so my DataTrigger
looks something like this:
<DataTrigger Binding="{Binding Converter={StaticResource ObjectToTypeConverter}}"
Value="{x:Type CmTypes:TelecomNumberPcmShellVm}">
<Setter Property="ToolTip" ... />
</DataTrigger>
这也可以用来设置 Button.ContentTemplate
而不是使用上面显示的隐式数据模板。
This could also be used to set the Button.ContentTemplate
instead of using implicit data templates as shown above.
这篇关于根据类型改变xaml模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!