使用角色/权限启用/禁用视图中的内容
我正在使用 mvvm-light 框架开发 WPF 应用程序。我对这两个都是新手。
我有一个允许用户编辑数据库中的记录的表单。管理员用户需要能够更新其他用户只读的字段。对我来说,将此启用/禁用代码放在视图的代码隐藏中很容易,但我的理解是它属于 ViewModel。
如何隐藏此文本框而不将代码放入视图中?
提前致谢。
<TextBox Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Name="uxMallNum" VerticalAlignment="Center"
Width="100" Height="25" MaxLength="50" Validation.ErrorTemplate="{DynamicResource validationTemplate}" Style="{DynamicResource textStyleTextBox}">
<TextBox.Text>
<Binding Path="MallNumber" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" >
<Binding.ValidationRules>
<local:StringRangeValidationRule MinimumLength="1" MaximumLength="50"
ErrorMessage="Mall Number is required and must be 50 characters or fewer." />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
I'm working on an WPF application using the mvvm-light framework. I'm new to both of these.
I have a form that allows a user to edit a record in a database. Admin users need to be able to update a field that should be read-only for other users. It would be easy for me to put this enable/disable code in the view's code-behind but my understanding is that this belongs in the ViewModel.
How do I hide this textbox without putting the code in the View?
Thanks in advance.
<TextBox Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Name="uxMallNum" VerticalAlignment="Center"
Width="100" Height="25" MaxLength="50" Validation.ErrorTemplate="{DynamicResource validationTemplate}" Style="{DynamicResource textStyleTextBox}">
<TextBox.Text>
<Binding Path="MallNumber" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" >
<Binding.ValidationRules>
<local:StringRangeValidationRule MinimumLength="1" MaximumLength="50"
ErrorMessage="Mall Number is required and must be 50 characters or fewer." />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经为这种类型的函数构建了一个转换器,尽管我不确定是否有更好的方法。
然后我将转换器添加到控件的可见性属性中。
您可以在转换器的参数中传递角色或用户名,但我的实例不需要它。
I've built a converter for this type of function, although I'm not sure if there's a better way.
Then I add the converter to the visibility property of a control.
You could pass in the roles, or usernames, in the parameter of the converter, but my instance didn't need it.