【发布时间】:2012-11-12 13:06:15
【问题描述】:
下面是一段 DataTemplate,它定义了一条用于联系客户的命令(带图像的按钮)。目前是为电话联系人定义的,还有几个命令,所以我想将它重用于其他类型的联系方式(电子邮件等)
它和它背后的视图模型的设计方式,只有两件事需要改变才能做到这一点:
- ContactCommand 按钮的图像和工具提示
- 整个最后一个按钮
似乎最可重用的方法是让整个按钮本身成为一个 DataTemplate,并在本文底部定义一个 DataType,但我不知道原始 DataTemplate 将如何使用它。尽管听起来很有希望,但我也从未使用过 DataTemplateSelector。
最好的方法是什么?代码看起来如何?
干杯,
浆果
当前数据模板
<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>
给瑞秋
带有隐式数据模板的修订按钮
<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>
{
...
}
对象模型
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是CmTypes:TelecomNumberPcmShellVm类型的情况下工作。 -
@Rachel。嗨,Rachel - 我试过了,错误转移到“Button.ToolTip”[在“Button”类型中找不到可附加属性“ToolTip”]。这对你有意义吗?
-
@Rachel。为什么不将您的评论变成答案?我敢肯定你很接近一个。干杯
-
当然,我将其发布为答案,以及关于
ToolTip的答案。我最初没有将其发布为答案,因为我不确定它是否回答了您的问题:)
标签: wpf xaml mvvm datatemplate