wpf textbox样式重写

Meoy 2013-06-20 10:11:17
文本框里的左面是一张小图片,图片右边可以输入文字,求大神写一下
...全文
344 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ariesget 2013-06-20
  • 打赏
  • 举报
回复
确实继承TextBox再写个ImageTextBox类比较好,加2个属性ImageSource,ImageWidth,然后模板加个PART_Image,这样就比较好控制了。省的麻烦就直接用上面那个模板也无所谓,个人感觉TextBox基本上不会怎么用到Background,大部分都是白色底色.难控制的也只是个图片显示宽度而已。
cheng2005 2013-06-20
  • 打赏
  • 举报
回复
一定不要用#1的方式,那样的话Background只能控制图片,不能控制背景了。 推荐还是写一个自定义控件,继承自TextBox,模板里面加一个元素。 参考一下TextBox自己的模板和处理方式,这样做出来的东西才好通用
rinall 2013-06-20
  • 打赏
  • 举报
回复
引用 1 楼 ariesget 的回复:

    <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>
ariesget 2013-06-20
  • 打赏
  • 举报
回复

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

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧