【问题标题】:Change Row/Cell color in LightSwitch 2010在 LightSwitch 2010 中更改行/单元格颜色
【发布时间】:2018-08-28 07:11:53
【问题描述】:

如果我有一个显示酒店列表的页面(从 1 到 500 的行)。我想将行颜色更改为红色。 例如第一行有 5 列:


Name      Age      Email      Tel      Salary      Gender_Man

Mo        25       xxx@x.com  12546    10          Yes

操作:如果Gender_Man == True,则将行颜色更改为红色。

由于我是 C# 新手,请告诉我在哪里以及如何在电灯开关中做到这一点?

【问题讨论】:

  • 为什么是负面评价?

标签: c# grid conditional-statements visual-studio-lightswitch


【解决方案1】:

我已经这样做了,但在 VB.NET 中。但是,我的解决方案改编自 herehere,它们在 C# 中有示例,尽管一些示例类的链接已损坏。

基本上,您需要创建一个方法,在加载时将转换器类绑定到每一行。该绑定将采用特定的列名,对您来说它将是Gender_Man。在上面的链接中,他们使用列 Status 并且能够仅使用列名进行绑定。我的专栏名称是Color,我必须使用New Binding("Details.Entity.Color"),所以您可能需要尝试一下。

转换器类是IValueConverter 的一个非常简单的子类,它必须实现一个可以留空的构造函数Convert,它测试列的值并返回一个具有所需ARGB 值的SolidColorBrush 对象背景是,ConvertBack 只是抛出一个异常。

另外,快速评论一下,如今的性别不仅仅是男性和女性,因此使用布尔值来捕获这些信息可能会导致令人头疼的问题。最好使用字符串,并可能为正确的验证制作一个选择列表。

【讨论】:

    【解决方案2】:

    这是我为这个问题想出的,作为 LS 2015 的更通用的解决方案。

    假设:我有一个名为 MySpecialConfigurationGrid 的数据网格

    假设:MySpecialConfigurationGrid 的底层 EF 类型是 MySpecialConfiguration

    给定:我要着色并用作颜色信息源的列单元称为 MyColor

    Given:此代码进入 LSML 背后的代码。

        void proxy_ControlAvailable(object sender, ControlAvailableEventArgs e)
        {
            var ctrl = (DataGrid)e.Control;
            ctrl.LoadingRow += new EventHandler<DataGridRowEventArgs>(Ctrl_LoadingRow);
        }
    
        private void Ctrl_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            //Getting a particular cell is tricky, 
            // you have cross reference the row instance in the particular
            // column you want to find, and then get that thing's parent, 
            // which happens to be the DataGridCell you can control the rendering of.
            DataGrid grid = (DataGrid)sender;
            DataGridRow row = new DataGridRow();
            row = e.Row;
            var cell = (grid.Columns.First(item => item.SortMemberPath == nameof(MySpecialConfiguration.MyColor))
                .GetCellContent(row) as Microsoft.LightSwitch.Presentation.Framework.ContentItemPresenter)
                .Parent as DataGridCell;
    
            Binding bgbinding = new Binding(nameof(MySpecialConfiguration.MyColor))
            {
                Mode = BindingMode.TwoWay,
                Converter = new RowColorConverter(),
                ValidatesOnExceptions = true
            };
            cell.SetBinding(DataGridCell.BackgroundProperty, bgbinding);
        }
    
        partial void MySpecialConfiguration_InitializeDataWorkspace(global::System.Collections.Generic.List<global::Microsoft.LightSwitch.IDataService> saveChangesTo)
        {
            Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
            {
                var proxy = this.FindControl(nameof(MySpecialConfigurationGrid));
                proxy.ControlAvailable += new EventHandler<ControlAvailableEventArgs>(proxy_ControlAvailable);
            });
        }
    
        public class RowColorConverter : IValueConverter
        {
            public RowColorConverter() { }
            #region IValueConverter Members
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value != null)
                {
                    var match = Regex.Match(value as string, "#(?<R>[a-fA-F0-9]{2})(?<G>[a-fA-F0-9]{2})(?<B>[a-fA-F0-9]{2})");
    
                    if (match.Success)
                    {
                        return new System.Windows.Media.SolidColorBrush(Color.FromArgb(255,
                            sToB(match.Groups["R"].Value),
                            sToB(match.Groups["G"].Value),
                            sToB(match.Groups["B"].Value)));
                    }
                }
    
                return new System.Windows.Media.SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
            }
    
            private byte sToB(string s)
            {
                return byte.Parse(s, System.Globalization.NumberStyles.HexNumber);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
    
            }
            #endregion
        }
    

    基本上,它的工作方式是将事件附加到行的创建,然后绑定到行的加载事件。从那里,您可以访问数据网格本身及其可视化树。做一些技巧来获取对 DataGridCell 的引用,然后将具有 IValueConverter 的绑定添加到其背景依赖项属性。 IValueConverter 将 HTML 十六进制颜色三元组字符串解析为组件部分,并从中返回 SolidColorBrush。

    对于您的特定 Gender_Man = true 事物,调整 ValueConverter 以接受适当的值,并更改代码其他位中的 nameof() 调用以引用适当的列进行绑定(绑定附加到颜色列) ,以及用于查找要着色的目标列的单元格。

    【讨论】:

      猜你喜欢
      • 2013-03-15
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多