首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >控件模板中的模板绑定

控件模板中的模板绑定
EN

Stack Overflow用户
提问于 2011-01-09 18:47:36
回答 4查看 45K关注 0票数 22

我有以下控件模板。

我希望使用模板绑定在控件模板中设置图像控件的source属性。

但是由于这是一个按钮控件的控件模板,而按钮控件没有source属性,所以在这种情况下我不能使用TemplateBinding。

代码语言:javascript
复制
<ControlTemplate x:Key="BtnTemplate" TargetType="Button">
        <Border CornerRadius="5"  Margin="15" Cursor="Hand">
            <StackPanel>
                <Image Name="Img" Style="{StaticResource ImageStyle}" Source="temp.jpg" Height="100" Width="100" Margin="5"></Image>
                <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label>
            </StackPanel>
        </Border>
    </ControlTemplate>

因为我必须为按钮的不同实例设置不同的图像,所以我不能硬编码路径。

请告诉我如何处理这种情况。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-01-09 19:09:38

我建议使用动态资源,例如按如下方式定义模板:

代码语言:javascript
复制
<ControlTemplate x:Key="buttonTemplate" TargetType="Button">
    <Border CornerRadius="5"  Margin="15" Cursor="Hand">
        <StackPanel Orientation="Horizontal" Background="Yellow">
            <Image Source="{DynamicResource ResourceKey=Img}" Height="100" Width="100" Margin="5"></Image>
            <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label>
        </StackPanel>
    </Border>
</ControlTemplate>

并像这样使用它:

代码语言:javascript
复制
<Button Content="Button" Template="{StaticResource ResourceKey=buttonTemplate}">
    <Button.Resources>
        <ImageSource x:Key="Img">SomeUri.png/</ImageSource>
    </Button.Resources>
</Button>
票数 35
EN

Stack Overflow用户

发布于 2011-01-09 19:05:46

TemplateBinding是一个轻量级的“绑定”,它不支持传统绑定的一些功能,例如使用与目标属性关联的已知类型转换器自动进行类型转换(例如将字符串URI转换为BitmapSource实例)。

以下代码可以正常工作:

代码语言:javascript
复制
<Window x:Class="GridScroll.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2">
<Window.Resources>
    <Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="5"  Margin="15" Cursor="Hand" Background="Red">
                        <StackPanel Orientation="Horizontal" Background="White">
                            <Image Name="Img" Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Margin="5"></Image>
                            <Label Content="{TemplateBinding Content}" Margin="2"></Label>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</Window.Resources>
<StackPanel Orientation="Horizontal">
    <Button Style="{StaticResource ButtonStyle}" Tag="a.jpeg" Content="a"/>
    <Button Style="{StaticResource ButtonStyle}" Tag="b.png" Content="b"/>
</StackPanel>

票数 5
EN

Stack Overflow用户

发布于 2011-01-09 18:52:16

你还没有真正说过你希望你的按钮的消费者如何设置来源。例如,您可以使用Button.Tag属性,然后在模板中绑定到该属性。或者你可以定义你自己的控件:

代码语言:javascript
复制
public class ImageButton : Button
{
    // add Source dependency property a la Image
}

然后是模板:

代码语言:javascript
复制
<ControlTemplate TargetType="ImageButton">
    <Border CornerRadius="5"  Margin="15" Cursor="Hand">
        <StackPanel>
            <Image Name="Img" Style="{StaticResource ImageStyle}" Source="{TempateBinding Source}" Height="100" Width="100" Margin="5"></Image>
            <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label>
        </StackPanel>
    </Border>
</ControlTemplate>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4638741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档