下面是一段 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>
{
...
}