Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
将图标放入XAML(WPF)中的文本框中_Wpf_Textbox_Icons - Fatal编程技术网

将图标放入XAML(WPF)中的文本框中

将图标放入XAML(WPF)中的文本框中,wpf,textbox,icons,Wpf,Textbox,Icons,我想在文本框的角落里放一个小图标(png文件)。我所说的大概意思是 到目前为止,我已经得到了(在我的主xaml文件中) 不幸的是,每当您想在WPF中更改控件的模板时,都必须覆盖整个模板,不能只修改其中的一小部分 幸运的是,所有模板都是公开的。例如,这是一个很好的例子 因此,为了向文本框添加图像,请从上面的链接中获取整个文本框模板,然后在您认为合适的位置添加图像 正如注释中指出的,您不必覆盖文本框的所有属性,只需覆盖模板属性。这可以使用BasedOn属性轻松完成: <Style x:Key=

我想在文本框的角落里放一个小图标(png文件)。我所说的大概意思是

到目前为止,我已经得到了(在我的主xaml文件中)


不幸的是,每当您想在WPF中更改控件的模板时,都必须覆盖整个模板,不能只修改其中的一小部分

幸运的是,所有模板都是公开的。例如,这是一个很好的例子

因此,为了向文本框添加图像,请从上面的链接中获取整个文本框模板,然后在您认为合适的位置添加图像

正如注释中指出的,您不必覆盖文本框的所有属性,只需覆盖
模板
属性。这可以使用
BasedOn
属性轻松完成:

<Style x:Key="IconTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
   <Setter Property="Template">
       ....
   </Setter>
</Style>

....

我将创建一个新的简单用户控件,而不是覆盖模板:

//pseudocode, does not compile!
<UserControl x:Class="MyUserControl" x:Name="root">
    <Grid>
        <!-- put some effort in aligning the items so it looks nice -->
        <TextBox Text="{Binding ElementName=root, Path=Text}" />
        <Image Source="{Binding ElementName=root, Path=IconSource}" />
    </Grid>
</UserControl>

public class MyUserControl : UserControl{
    dependencyproperty string Text;
    dependencyproperty string IconSource;
}
//伪代码,不编译!
公共类MyUserControl:UserControl{
从属属性字符串文本;
dependencyproperty字符串IconSource;
}
因此,您可以按如下方式使用它:

<my:MyUserControl Text="{Binding MyText}" IconSource="{Binding MyIcon}"/>

实际上,您只需在要创建的文本框样式中添加一个文本框,这实际上不会为您提供一个可以更改其来源的图像,但因为它将被硬编码到xaml中,我想您可以创建一个附加属性来传递该信息

无论如何,重要的一点是,如果要在文本框的样式中使用文本框,必须将内部文本框样式设置为{x:null}

        <Style TargetType="TextBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="50"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Style="{x:Null}" Grid.Column="0" 
                                       Text="{TemplateBinding Text}"
                                       Foreground="{TemplateBinding Foreground}"                                           
                                       Background="{TemplateBinding Background}" 
                                       FontFamily="{TemplateBinding FontFamily}"
                                       FontSize="{TemplateBinding FontSize}"
                                       FontWeight="{TemplateBinding FontWeight}"/>

                            <Image Grid.Column="1" Source=">Insert your image source here."/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


当覆盖模板时,这种情况下,对于其他属性,OP可以使用
BasedOn
属性,只覆盖他们需要的内容,即…背景色…@AaronMcIver-正确,我的回答不太清楚-我会让它更清楚一点…谢谢,最后我确实走了usercontrol路径。我得到了一个没有边框的文本框
<my:MyUserControl Text="{Binding MyText}" IconSource="{Binding MyIcon}"/>
        <Style TargetType="TextBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="50"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Style="{x:Null}" Grid.Column="0" 
                                       Text="{TemplateBinding Text}"
                                       Foreground="{TemplateBinding Foreground}"                                           
                                       Background="{TemplateBinding Background}" 
                                       FontFamily="{TemplateBinding FontFamily}"
                                       FontSize="{TemplateBinding FontSize}"
                                       FontWeight="{TemplateBinding FontWeight}"/>

                            <Image Grid.Column="1" Source=">Insert your image source here."/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>