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 控件模板中的模板绑定_Wpf_Xaml_Data Binding_Controltemplate_Templatebinding - Fatal编程技术网

Wpf 控件模板中的模板绑定

Wpf 控件模板中的模板绑定,wpf,xaml,data-binding,controltemplate,templatebinding,Wpf,Xaml,Data Binding,Controltemplate,Templatebinding,我有以下控件模板 我希望为控件中的图像控件设置source属性 模板使用模板绑定 但是,由于这是一个按钮控件的控件模板,而按钮控件不是 如果没有source属性,在这种情况下我不能使用TemplateBinding <ControlTemplate x:Key="BtnTemplate" TargetType="Button"> <Border CornerRadius="5" Margin="15" Cursor="Hand">

我有以下控件模板

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

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

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

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


请让我知道如何处理这种情况

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

public class ImageButton : Button
{
    // add Source dependency property a la Image
}
然后是模板:

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

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

以下代码可以正常工作:

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


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

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

然后像这样使用它:

<Button Content="Button" Template="{StaticResource ResourceKey=buttonTemplate}">
    <Button.Resources>
        <ImageSource x:Key="Img">SomeUri.png/</ImageSource>
    </Button.Resources>
</Button>

SomeUri.png/

我不太清楚您的问题,但为什么不使用ContentPresenter?它允许在更高级别上移动图像的代码

<ControlTemplate x:Key="BtnTemplate" TargetType="Button">
  ...
  <ContentPresenter/>
</ControlTemplate>
...
<Button Template="{StaticResource BtnTemplate}">
  <Image .../>
</Button>

...
...

我使用了标记属性,但这不起作用。Source=“{TemplateBinding Tag}”,在创建按钮时,Tag=“Temp.jpg”,但这不起作用。我希望只在XAML中设置源代码,而不使用codebehind。此外,标记可能不是一个好主意,因为controltemplate中可能有许多控件,我希望在创建对象时为它们设置一些属性。这不起作用,因为
Source
的类型是
ImageSource
,而“Temp.jpg”是
字符串。您需要使用转换器将字符串转换为ImageSources,就像
Image
控件那样。除了使用标记,还有其他方法吗。?我很担心,因为稍后我的控件模板中可能有多个控件,我想在创建对象时设置属性。但由于标记已被使用,我将无法再次使用。您可以按照Kent的建议执行。从按钮派生并创建您自己的控件。添加自定义依赖项属性并在使用标记时使用。我的问题与原始帖子略有不同,但这是唯一的解决方案。我花了整个上午的时间在这上面,因为单个资源字典中的错误阻止了UI在整个应用程序中加载到几个xaml设计器中。说真的,谢谢。可以用更简洁的方式来使用按钮吗?喜欢我被这个打动了,你能帮我一下吗?