简体   繁体   English

屏蔽WPF ListView / GridView中列的内容

[英]Masking the contents of a column in a WPF ListView / GridView

I have a WPF ListView that is bound to an ObservableCollection of Employee objects. 我有一个WPF ListView绑定到Employee对象的ObservableCollection。 My XAML looks like this: 我的XAML如下所示:

<ListView x:Name="myListView" ItemsSource="{Binding Employees}">
    <ListView.View>
        <GridView AllowsColumnReorder="False">
            <GridViewColumn DisplayMemberBinding="{Binding EmployeeName}" Width="175">
                <GridViewColumnHeader Content="EmployeeName"/>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding EmployeeID}" Width="125">
                <GridViewColumnHeader Content="EmployeeID"/>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding EmployeeSecretCode}" Width="125">
                <GridViewColumnHeader Content="EmployeeSecretCode"/>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

I need to display the EmployeeSecretCode column but I'd like to mask the contents of it either fully or partially. 我需要显示EmployeeSecretCode列,但我想完全或部分屏蔽它的内容。 So instead of displaying the actual code (let's say 12345), I'd either like to display ***** (since 12345 is 5 digits) or alternatively, ******** (since the max size of the code is 8 digits). 因此,我不想显示实际的代码(比如12345),而是显示***** (因为12345是5位数字),或者显示******** (因为代码是8位数字)。 Is there an easy way to do this? 是否有捷径可寻?

A simple example of achieving this requirement with an IValueConverter 一个使用IValueConverter满足此要求的简单示例

internal class MyConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    var input = value.ToString();
    // change "input.Length" in the following line to 8 if you just want 8 "*" regardless of length
    return new String('*', input.Length);
  }

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

and xaml: 和xaml:

<ListView x:Name="myListView"
          ItemsSource="{Binding Employees}">
  <ListView.Resources>
    <local:MyConverter x:Key="MyConverter" />
  </ListView.Resources>
  ...
      <GridViewColumn Width="125"
                      DisplayMemberBinding="{Binding EmployeeSecretCode,
                                                      Converter={StaticResource MyConverter}}">
        <GridViewColumnHeader Content="EmployeeSecretCode" />
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>

Note: 注意:

Do consider the security implications of such implementations in the UI. 一定要考虑UI中此类实现的安全性。 Using an application like Snoop one can easily get the DataContext and thereby the actual value you're masking here. 使用诸如Snoop之类的应用程序可以轻松获取DataContext ,从而获得您在此处掩盖的实际值。 Thereby this is merely a UI mask and does not offer anything in terms of viable security(actually it adds a weak link cos the unsafe value is exposed to the UI Views). 因此,这仅仅是一个UI掩码,在可行的安全性方面没有提供任何帮助(实际上,如果不安全的值暴露在UI视图中,则会增加一个弱链接cos)。

If that is something you are indeed considering then keep the "actual" value in your Model/back-end encrypted/protected in whichever way you see fit and only give the View the masked data in it's property. 如果确实要考虑这一点,则以您认为合适的任何方式对“模型/后端”中的“实际”值进行加密/保护,并仅在其属性中赋予“查看”蒙版数据。 This will then mean, even if someone uses a snoop like app, all they would get from the DataContext would be " * " and not the actual secret value. 这将意味着,即使有人使用类似snoop的应用程序,从DataContext获得的所有内容都是“ *”,而不是实际的秘密值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM