Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 按类型更改xaml模板_Wpf_Xaml_Mvvm_Datatemplate - Fatal编程技术网

Wpf 按类型更改xaml模板

Wpf 按类型更改xaml模板,wpf,xaml,mvvm,datatemplate,Wpf,Xaml,Mvvm,Datatemplate,下面是数据模板的一部分,它定义了用于联系客户机的条带if命令(带图像的按钮)。目前为电话联系人定义了多个命令,因此我想将其用于其他类型的联系人方法(电子邮件等) 它的设计方式及其背后的视图模型,只有两件事需要改变才能做到这一点: ContactCommand按钮的图像和工具提示 最后一个按钮 看起来最可重用的方法是让整个按钮本身成为一个DataTemplate,其数据类型定义如本文底部所示,但我不是在摸索原始DataTemplate将如何使用它。我也从未使用过DataTemplateSelect

下面是数据模板的一部分,它定义了用于联系客户机的条带if命令(带图像的按钮)。目前为电话联系人定义了多个命令,因此我想将其用于其他类型的联系人方法(电子邮件等)

它的设计方式及其背后的视图模型,只有两件事需要改变才能做到这一点:

  • ContactCommand按钮的图像和工具提示
  • 最后一个按钮
  • 看起来最可重用的方法是让整个按钮本身成为一个DataTemplate,其数据类型定义如本文底部所示,但我不是在摸索原始DataTemplate将如何使用它。我也从未使用过DataTemplateSelector,尽管这听起来很有希望

    最好的方法是什么?代码看起来如何

    干杯,
    贝里尔

    当前数据模板
    
    ...
    
    给瑞秋 带隐式数据模板的修订按钮
    
    对象模型
    公共类PcmShellVm:SatelliteViewModel
    其中TCm:接触机制
    {
    //命令。。。
    }
    公共类电话号码PcmShellVm:PcmShellVm
    {
    ...
    }
    公共类EmailPcmShellVm:PcmShellVm
    {
    ...
    }
    
    对象模型
    公共类PcmShellVm:SatelliteViewModel
    其中TCm:接触机制
    {
    //命令。。。
    }
    公共类电话号码PcmShellVm:PcmShellVm
    {
    ...
    }
    公共类EmailPcmShellVm:PcmShellVm
    {
    ...
    }
    
    上一个代码块中的
    数据模板
    不起作用,因为您正在将
    数据模板
    放入
    按钮。内容
    。如果您将其放入
    ,它应该可以提供
    按钮。内容
    的类型为
    CmTypes:TelecomNumberPcmShellVm

    此外,您需要切换到
    图像。工具提示
    以向图像添加工具提示,如
    按钮。工具提示
    不是
    数据模板的有效属性

    <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>
    

    这也可用于设置
    按钮。ContentTemplate
    而不是使用如上所示的隐式数据模板。

    上一个代码块中的
    DataTemplate
    无效,因为您正在将
    DataTemplate
    放入
    按钮。Content
    。如果你把它放在
    中,它可能会提供
    按钮。内容
    的类型是
    CmTypes:TelecomNumberPcmShellVm
    @Rachel。嗨,Rachel-我试过了,错误转移到“Button.ToolTip”[在类型“Button”中找不到可附加属性“ToolTip]。“这对你有意义吗?”瑞秋。为什么不把你的评论变成一个答案呢?我相信你已经很接近了。谢谢,我把它作为一个答案,连同关于
    工具提示的答案一起发布。我最初没有把它作为答案发布,因为我不确定它是否回答了你的问题:)所以它编译了,但不起作用;这意味着它根本没有出现。由于没有数据绑定错误(并且绑定的命令可以工作),我猜这表明它由于某种原因找不到类型。请参阅张贴末尾的修订!我想我喜欢这种情况下的转换器解决方案,因为我实际上可以重用它,从而使xaml更加精简;但我不能肯定我知道如何编码。似乎DataTemplateSelector方法也可以工作,但我也不知道如何编写代码!所以我要问一个不同的问题——这样,这篇文章就不会比现在更复杂了,你可以有机会获得更多的分数,这显然是你需要的:——)。Cheers@Berryl修改后的XAML中的
    按钮.Content
    仍设置为
    DataTemplate
    。如果
    Button.Content
    被设置为类型为
    TelecomNumberPcmShellVm
    的对象(例如
    Content=“{Binding SomeTelecomNumberProperty}”
    ),它将使用
    Button.Resources
    中定义的
    DataTemplate
    来绘制该项。是的,只需输入'Content={Binding}'就可以了。如果你愿意,我会喜欢的。干杯
    <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>
    {
        ...
    }
    
    <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>
    
    <DataTrigger Binding="{Binding Converter={StaticResource ObjectToTypeConverter}}" 
                 Value="{x:Type CmTypes:TelecomNumberPcmShellVm}">
        <Setter Property="ToolTip" ... />
    </DataTrigger>