使用角色/权限启用/禁用视图中的内容

发布于 2024-09-24 16:08:13 字数 1009 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2024-10-01 16:08:13

我已经为这种类型的函数构建了一个转换器,尽管我不确定是否有更好的方法。

public class AdminVisibilityConverter : IValueConverter
{
    #region Methods
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool isAdmin = WebContext.Current.User.IsInRole("Admin");

        return isAdmin ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    #endregion
}

然后我将转换器添加到控件的可见性属性中。

<toolkit:AccordionItem Tag="#ManageAnnouncements" Visibility="{Binding Source=User, Converter={StaticResource AdminVisibilityConverter}}">

您可以在转换器的参数中传递角色或用户名,但我的实例不需要它。

I've built a converter for this type of function, although I'm not sure if there's a better way.

public class AdminVisibilityConverter : IValueConverter
{
    #region Methods
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool isAdmin = WebContext.Current.User.IsInRole("Admin");

        return isAdmin ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    #endregion
}

Then I add the converter to the visibility property of a control.

<toolkit:AccordionItem Tag="#ManageAnnouncements" Visibility="{Binding Source=User, Converter={StaticResource AdminVisibilityConverter}}">

You could pass in the roles, or usernames, in the parameter of the converter, but my instance didn't need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文