基于模型中的数据折叠 Silverlight LOB 应用程序中的 UIElements

发布于 11-04 21:08 字数 345 浏览 2 评论 0原文

在表单中,我对返回的每个字段都有一个用户控件。该控件由堆栈面板内的标签和 texblock 组成。该控件是构成我的表单的数据模板的一部分,该表单由包含用户控件的包装面板组成。我的意图是当渲染表单时评估模型中返回的绑定属性,如果它为空,则将控件的可见性设置为折叠。目的是仅在返回数据的表单中呈现字段。环绕面板允许控件保持内联,而不是允许表单中出现过多的空白。

我最初的想法是迭代返回的列表,如果模型中的属性为空,则通过依赖属性将控件的可见性设置为折叠。我在这里担心的是性能,因为某些表单有超过 700 个字段/属性。

我很想知道是否有人做过类似的方法或者他们使用什么方法来控制 UIElements 的可见性

提前感谢您的任何建议

Within a form I have a user control for each field being returned. The control consists of a label and a texblock within a stack panel. This control is part of a datatemplate that makes up my form which is comprised of a wrap panel which contains the user controls. My intent is when the form is rendered to evaluate the bound property returned in my model and if it null set the visibility of the control to collapsed. The intent is to only have fields rendered within the form that has data being returned. The wrap panel allows for the controls to stay inline vs allowing excess white space in the form.

My initial thought was to iterate through the List that is returned and if the property in the model is null set the visibility of the control to collapsed via a dependency property. A concern I have here is with performance as some forms have over 700 fields / properties.

I was curious to learn if anyone has done a similar approach or what approach they used to control the visibility of UIElements

Thanks in advance for any suggestions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吹梦到西洲2024-11-11 21:08:27

我们使用依赖属性来确定控件的可见性。我们与我们的授权库一起执行此操作。因此,在我们的 xaml 中,代码如下所示:

<ListBoxItem x:Name="About" 
    Content="About Us"  
    AuthLib:Authorization.Visibility="WebUser"
    Margin="10,5,10,5" />
<ListBoxItem x:Name="Accounting" 
    Content="Work Order Acct" 
    AuthLib:Authorization.Visibility="Admin, Accounting,Finance"
    Margin="10,5,10,5" />

其中 WebUser 是任何经过身份验证的用户,显然会计/财务/管理角色具有更高的权限。

我们已经在一个页面上进行了数十次调用,没有任何问题,但从来没有数百次。可能值得复制/粘贴看看效果如何。

如果值得的话,这是我们的 Auth 库中的可见性属性:

#region Visibility

  public static string GetVisibility(UIElement obj)
    {
        return (string)obj.GetValue(VisibilityProperty);
    }
  public static void SetVisibility(UIElement obj, string value)
    {
     obj.SetValue(VisibilityProperty, value);
    }

    /// Using a DependencyProperty as the backing store for requiresRole.  This enables animation, styling, binding, etc...
  public static readonly DependencyProperty VisibilityProperty = DependencyProperty.RegisterAttached(
     "Visibility", 
        typeof(string), 
        typeof(Authorization),
     new PropertyMetadata(Visibility_Callback));
    // This callback will be invoked when some control will receive a value for your 'Visibility' property
  private static void Visibility_Callback(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        var uiElement = (UIElement)source;

        if (App.IsAuthenticated)
        {
            RecalculateControlVisibility(uiElement);
        }
        else
        {
            EventHandler eh = null;
            eh = delegate
            {
                RecalculateControlVisibility(uiElement);
            };
            App.Authenticated += eh;
                RecalculateControlVisibility(uiElement);
        }
    }

    private static void RecalculateControlVisibility(UIElement control)
    {
        //Authorization.UserHasRole() - is your code to check roles
        if (Authorization.UserHasRole(GetVisibility(control)))
        {
            control.Visibility = Visibility.Visible;
        }
        else
        {
            control.Visibility = Visibility.Collapsed;
        }
    }

  #endregion

We use Dependency Properties to determine visibility of controls. We do this in concert with our Authorization library. So in our xaml, the code looks something like this:

<ListBoxItem x:Name="About" 
    Content="About Us"  
    AuthLib:Authorization.Visibility="WebUser"
    Margin="10,5,10,5" />
<ListBoxItem x:Name="Accounting" 
    Content="Work Order Acct" 
    AuthLib:Authorization.Visibility="Admin, Accounting,Finance"
    Margin="10,5,10,5" />

Where WebUser is any authenticated user, and obviously Accounting/Finance/Admin roles have elevated privilages.

We've done this with dozens of calls on a page without any problem, but never hundreds. Might be worth a copy/paste to see how it goes.

In case it's worthwhile, here's the visibility property in our Auth library:

#region Visibility

  public static string GetVisibility(UIElement obj)
    {
        return (string)obj.GetValue(VisibilityProperty);
    }
  public static void SetVisibility(UIElement obj, string value)
    {
     obj.SetValue(VisibilityProperty, value);
    }

    /// Using a DependencyProperty as the backing store for requiresRole.  This enables animation, styling, binding, etc...
  public static readonly DependencyProperty VisibilityProperty = DependencyProperty.RegisterAttached(
     "Visibility", 
        typeof(string), 
        typeof(Authorization),
     new PropertyMetadata(Visibility_Callback));
    // This callback will be invoked when some control will receive a value for your 'Visibility' property
  private static void Visibility_Callback(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        var uiElement = (UIElement)source;

        if (App.IsAuthenticated)
        {
            RecalculateControlVisibility(uiElement);
        }
        else
        {
            EventHandler eh = null;
            eh = delegate
            {
                RecalculateControlVisibility(uiElement);
            };
            App.Authenticated += eh;
                RecalculateControlVisibility(uiElement);
        }
    }

    private static void RecalculateControlVisibility(UIElement control)
    {
        //Authorization.UserHasRole() - is your code to check roles
        if (Authorization.UserHasRole(GetVisibility(control)))
        {
            control.Visibility = Visibility.Visible;
        }
        else
        {
            control.Visibility = Visibility.Collapsed;
        }
    }

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