文本框里的左面是一张小图片,图片右边可以输入文字,求大神写一下

解决方案 »

  1.   


        <Style  x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}"> 
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="40" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Label Background="{TemplateBinding Background}"/>
                                <Border Grid.Column="1" x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" SnapsToDevicePixels="true">
                                    <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>        <TextBox Width="300" Height="30" Style="{StaticResource TextBoxStyle1}">
                <TextBox.Background>
                    <ImageBrush ImageSource="图片路径" />
                </TextBox.Background>
            </TextBox>
      

  2.   

    一定不要用#1的方式,那样的话Background只能控制图片,不能控制背景了。
    推荐还是写一个自定义控件,继承自TextBox,模板里面加一个元素。参考一下TextBox自己的模板和处理方式,这样做出来的东西才好通用
      

  3.   

    确实继承TextBox再写个ImageTextBox类比较好,加2个属性ImageSource,ImageWidth,然后模板加个PART_Image,这样就比较好控制了。省的麻烦就直接用上面那个模板也无所谓,个人感觉TextBox基本上不会怎么用到Background,大部分都是白色底色.难控制的也只是个图片显示宽度而已。