【问题标题】:Changing color of row depending on few properties in silverlight根据silverlight中的几个属性改变行的颜色
【发布时间】:2011-09-13 12:14:22
【问题描述】:

我有具有 3 个属性的对象:

CouldBeWhite bool
CouldBeGreen bool
CouldBeRed bool

    If CouldBeWhite is true, then foreground of row should be white,  if
    CouldBeGreen is true, and CouldBeWhite is false, then row should be
    Green If CouldBeRed is true, and  and CouldBeWhite is false and
    CouldBeGreen is false then row should be Red In other case row
    should be blue.

我现在的想法是在 viewmodel Color 中添加一些新属性,当我计算行的颜色时。

或者也许有更好的方法来实现这个?

【问题讨论】:

  • 这种逻辑在我脑海中烧出一个洞。 enum ShouldBe { White, Green, Red } 怎么样,然后完成。

标签: silverlight gridview silverlight-4.0 mvvm


【解决方案1】:

将此枚举添加到您的项目中:-

public enum RowState
{
     Blue,
     Red,
     Green,
     White
}

然后将此属性添加到您的 ViewModel:-

private RowState _RowState;
public RowState RowState
{
     get { return _RowState; }
     set
     {
         if (value != _RowState)
         {
               _RowState = value;
               NotifyPropertyChanged("RowState"); // Assumes typical implementation of INotifyPropertyChanged 
         }
     }
}

private void UpdateRowState()
{
    if (CouldBeWhite)
         RowState = RowState.White;
    else if (CouldBeGreen)
         RowState = RowState.Green;
    else if (CouldBeRed)
         RowState = RowState.Red;
    else
         RowState = RowState.Blue;
}

CouldBeXXX 属性发生变化时调用 UpdateRowState。

请考虑一下,您可能不是一时兴起将前景设为白色、红色或绿色。会有一些原因为什么它的白色、红色或绿色。因此,在您的代码中考虑一个简单的短名称来表示这些原因,并将颜色名称替换为那些更有意义的名称。

现在在这个blog 中获取StringToValueConverter 的代码。在UserControl.Resources 中将此实例添加到您的 Xaml:

         <local:StringToObjectConverter x:Key="RowStateToBrush">
             <ResourceDictionary>
                 <SolidColorBrush Color="Red" x:Key="Red" />
                 <SolidColorBrush Color="Green" x:Key="Green" />
                 <SolidColorBrush Color="White" x:Key="White" />
                 <SolidColorBrush Color="Blue" x:Key="__default__" /> 
            </ResourceDictionary>
         </local:StringToObjectConverter>

你可以绑定TextBlock:

  <TextBlock Text="{Binding SomeTextProperty}"  Foreground="{Binding RowState, Converter={StaticResource RowStateToBrush}}" />

【讨论】:

    【解决方案2】:

    在自定义值转换器中实现此逻辑可能会更简洁。比如:

      public class RowColorConverter : IValueConverter
            {
                public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                {
                    var myObj = value as MyObjectType();
    
                    if (myObj.CouldBeWhite)
                    {
                        return new SolidColorBrush(Colors.White);
                    }
                    if (myObj.CouldBeGreen )
                    {
                        return new SolidColorBrush(Colors.Green);
                    }
                    if (myObj.CouldBeRed )
                    {
                        return new SolidColorBrush(Colors.Red);
                    }
                    return new SolidColorBrush(Colors.Blue);
    
                }  
    
               public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                {
                    return null;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-11
      • 2022-01-13
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      相关资源
      最近更新 更多