【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 ...
随机推荐
- 解决Java“syntax error on token enum”问题
本来我的问题是jsp中变量名命名和保留关键字重复了,如下图.无意中又找到下面的问题和解决方案作为笔记. 解决方法:修改变量名. ==================================== ...
- JFinal连接数据库配置说明
本文采用的是加载配置文件的形式和数据库进行交互 ps:数据库采用的是postgresql 1.加载配置文件 public void configConstant(Constants me) { Pro ...
- Device trees, Overlays and Parameters of Raspberry Pi
Raspberry Pi's latest kernels and firmware, including Raspbian and NOOBS releases, now by default us ...
- JMeter学习笔记--创建数据库测试计划
添加线程组(并发用户):线程数(10),Rame-Up Period(0), Loop Count(3) 线程组添加JDBC请求(配置元件):Variable Name(MySQL), Databas ...
- configure: error: png.h not found.
PHP的时候提示这个错误 configure: error: png.h not found.,这个是选择安装GD模块才会出现的错误,详细错误信息如下 If configure fails try - ...
- 【转】谈“P=NP?”
“P=NP?” 通常被认为是计算机科学最重要的问题.有一个叫Clay Math的研究所,甚至悬赏 100 万美元给解决它的人.可是我今天要告诉你的是,这个问题其实是不存在的,它根本不需要解决. 我并不 ...
- 关于c语言内存分配,malloc,free,和段错误,内存泄露
1. C语言的函数malloc和free (1) 函数malloc和free在头文件<stdlib.h>中的原型及参数 void * malloc(size_t size ...
- Python islower() 方法
描述 Python islower() 方法检测字符串是否由小写字母组成. 相反的方法:isupper() 方法. 语法 islower() 方法语法: S.islower() 参数 无. 返回值 如 ...
- 实例讲解Nginx下的rewrite规则(转)
一.正则表达式匹配,其中:* ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文件* ...
- introduction to anaconda
一.安装好anaconda后进行更新 说明:在windows环境中的操作,在linux环境中请参考具体书籍. 1.进入cmd命令行 执行 conda update conda conda update ...