【C#/WPF】TextBlock/TextBox/Label编辑文字的问题
标题有点描述不清,就当是为了方便自己用时易于搜索到。
总之需求是:显示用户信息(文字)时,允许用户编辑自己的信息。
效果图如下:
点击【编辑】按钮前:
点击【编辑】按钮后,允许编辑:
- 别吐槽为甚性别还能再改。。。我就是意思意思这个效果
- 使用了样式,但不是本文关注点,于是省略
- 按钮的作用也不是本文关注点,省略
一种解决思路:
正常显示用的TextBlock,然后在它上面放一个TextBox并隐藏。点击【编辑】按钮后再显示TextBox,达到看起来像是编辑文字的效果!
前台代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<!-- 用户名 -->
<TextBlock Grid.Row="0" Grid.Column="0" Text="用户名:" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<Grid Grid.Row="0" Grid.Column="1" Margin="5" Grid.ColumnSpan="2" Height="30" VerticalAlignment="Center" HorizontalAlignment="Left">
<Label x:Name="userId" FontSize="16" Width="180"/>
<TextBox x:Name="editUserId" FontSize="16" Width="180" Visibility="Collapsed"/>
</Grid>
<!-- 编辑 -->
<Grid Grid.Row="0" Grid.Column="3" Margin="10,0,10,0" VerticalAlignment="Center" HorizontalAlignment="Right">
<Button x:Name="editBtn" Command="{Binding EditCommand}" Style="{StaticResource myButton1}" Content="编辑" Width="60" Height="25"/>
<Button x:Name="editConfirmBtn" Command="{Binding EditConfirmCommand}" Style="{StaticResource myButton1}" Content="确定" Width="60" Height="25" Margin="0,0,0,1" Visibility="Collapsed"/>
<Button x:Name="editCancelBtn" Command="{Binding EditCancelCommand}" Style="{StaticResource myButton1}" Content="取消" Width="60" Height="25" Margin="-70,0,70,0" Visibility="Collapsed"/>
</Grid>
<!-- 昵称 -->
<TextBlock Grid.Row="1" Grid.Column="0" Text="昵称:" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<Grid Grid.Row="1" Grid.Column="1" Margin="5" Grid.ColumnSpan="2" Height="30" VerticalAlignment="Center" HorizontalAlignment="Left">
<Label x:Name="nickName" FontSize="16" Width="180"/>
<TextBox x:Name="editNickName" FontSize="16" Width="180" Visibility="Collapsed"/>
</Grid>
<!-- 性别 -->
<TextBlock Grid.Row="1" Grid.Column="2" Text="性别:" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<Grid Grid.Row="1" Grid.Column="3" Margin="5" Height="30" VerticalAlignment="Center" HorizontalAlignment="Left">
<Label x:Name="sex" FontSize="16" Width="180"/>
<StackPanel Orientation="Horizontal">
<RadioButton x:Name="editSexMale" GroupName="sex" Content="男" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0" Visibility="Collapsed"/>
<RadioButton x:Name="editSexFemale" GroupName="sex" Content="女" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="30,0,0,0" Visibility="Collapsed"/>
</StackPanel>
</Grid>
</Grid>
这里【编辑】、【确认】、【取消】三个按钮的Command绑定到点击事件,就是控制这三个按钮组的显隐和TextBox的显隐切换,实现界面效果后再添加数据的修改和上传逻辑。
【C#/WPF】TextBlock/TextBox/Label编辑文字的问题的更多相关文章
- WPF TextBlock 判断 isTextTrimmed 文本是否超出
WPF TextBlock 设置TextTrimming情况下 判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号) private bool HasTextTrimmed(T ...
- WPF TextBlock IsTextTrimmed 判断文本是否超出
WPF TextBlock/TextBox 设置TextTrimming情况下 判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号) private bool IsTextTr ...
- WPF 设置TextBox为空时,背景为文字提示
WPF 设置TextBox为空时,背景为文字提示. <TextBox FontSize="17" Height="26" Margin="2 ...
- [转]在WPF中区别TextBlock和Label
TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ...
- WPF学习二:TextBlock和Label的区别
TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ...
- 获取wpf datagrid当前被编辑单元格的内容
原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...
- 重新想象 Windows 8 Store Apps (1) - 控件之文本控件: TextBlock, TextBox, PasswordBox, RichEditBox, RichTextBlock, RichTextBlockOverflow
原文:重新想象 Windows 8 Store Apps (1) - 控件之文本控件: TextBlock, TextBox, PasswordBox, RichEditBox, RichTextBl ...
- WPF自定义TextBox及ScrollViewer
原文:WPF自定义TextBox及ScrollViewer 寒假过完,在家真心什么都做不了,可能年龄大了,再想以前那样能专心坐下来已经不行了.回来第一件事就是改了项目的一个bug,最近又新增了一个新的 ...
- Wpf解决TextBox文件拖入问题、拖放问题
在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同, 解放方法如下: 使用Previ ...
随机推荐
- springboot + mybatis +easyUI整合案例
概述 springboot推荐使用的是JPA,但是因为JPA比较复杂,如果业务场景复杂,例如企业应用中的统计等需求,使用JPA不如mybatis理想,原始sql调优会比较简单方便,所以我们的项目中还是 ...
- Android UI-实现底部切换标签(fragment)
Android UI-实现底部切换标签(fragment) 前言 本篇博客要分享的一个UI效果--实现底部切换标签,想必大家在一些应用上面遇到过这样的效果了,最典型的就是微信了,能够左右滑动切换页面. ...
- 各大主流.Net的IOC框架
Autofac下载地址:http://code.google.com/p/autofac/ Castle Windsor下载地址:http://sourceforge.net/projects/cas ...
- PHP-数据库永久连接
以下为PHP官网上对数据库永久连接做的解释: 永久的数据库连接是指在脚本结束运行时不关闭的连接.当收到一个永久连接的请求时.PHP 将检查是否已经存在一个(前面已经开启的)相同的永久连接.如果存在,将 ...
- springmvc异常统一处理
http://www.cnblogs.com/xd502djj/archive/2012/09/24/2700490.html
- java打包成window service服务[转]
1 解释 java project 我说的是main方法作为程序入口的java工程,有别于 web project. 这样的工程 一般都是web project的附属扫描程序或一些独立的执行程序,如 ...
- RabbitMQ与.net core(一)安装
一.安装Erlang环境 前提:erlang版本与rabbitmq版本需按照要求对应!!! 1.在安装erlang之前先安装下依赖文件(这一步不要忘掉了, 不然后面./configure的时候要报错) ...
- Python学习笔记010——形参与实参
在使用中忽略了一个问题,形参有些和实参类似,也不能是“关键字后面含有位置参数”,即“默认形参”后面必须不能含有“位置”形参! def test(a=100,b): print("test&q ...
- Android 实现 HttpClient 请求Https
如题,默认下,HttpClient是不能请求Https的,需要自己获取 private static final int SET_CONNECTION_TIMEOUT = 5 * 1000; priv ...
- iOS9中怎样在日历App中创建一个随意时间之前開始的提醒(三)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 四.创建随意时间之前開始的提醒 如今我们找到了指定源中的指定日 ...