Click here to Skip to main content
15,886,067 members
Home / Discussions / WPF
   

WPF

 
QuestionWPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Nicolai Schrade10-May-11 5:01
Nicolai Schrade10-May-11 5:01 
AnswerRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Ian Shlasko10-May-11 5:38
Ian Shlasko10-May-11 5:38 
GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Nicolai Schrade10-May-11 6:00
Nicolai Schrade10-May-11 6:00 
GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Ian Shlasko10-May-11 6:09
Ian Shlasko10-May-11 6:09 
GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Nicolai Schrade10-May-11 6:35
Nicolai Schrade10-May-11 6:35 
GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Ian Shlasko10-May-11 6:52
Ian Shlasko10-May-11 6:52 
GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Nicolai Schrade10-May-11 22:57
Nicolai Schrade10-May-11 22:57 
AnswerRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Pete O'Hanlon10-May-11 6:35
mvePete O'Hanlon10-May-11 6:35 
What you need to do is bind the background property to the actual property that contains the Locked/Editable value, and then use a value converter to change the colour as appropriate. Here's a sample of the converter:
C#
namespace MySample.Converters
{
  using System;
  using System.Globalization;
  using System.Windows.Data;
  using System.Windows.Media;

  /// <summary>
  /// Converter to determine whether or not a number is negative, and apply 
  /// conditional formatting if it is.
  /// </summary>
  public class StatusTextColorConverter : IValueConverter
  {
    #region IValueConverter Members
    /// <summary>
    /// Convert from the value to the colour brush.
    /// </summary>
    /// <param name="value">The value to convert.</param>
    /// <param name="targetType">The parameter is not used.</param>
    /// <param name="parameter">The parameter is not used.</param>
    /// <param name="culture">The parameter is not used.</param>
    /// <returns>The populated colour brush.</returns>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      var brush = new SolidColorBrush(Colors.White);

      if (value != null && value == "Locked")
      {
        brush = Colors.Yellow;
      }

      return brush;
    }

    /// <summary>
    /// Unused in this implementation.
    /// </summary>
    /// <param name="value">The parameter is not used.</param>
    /// <param name="targetType">The parameter is not used.</param>
    /// <param name="parameter">The parameter is not used.</param>
    /// <param name="culture">The parameter is not used.</param>
    /// <returns>The parameter is not used.</returns>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }

    #endregion
  }
}
Then you declare your converter as a reference in your XAML resources:
XML
<conv:StatusTextColorConverter x:Key="statusTextColorConverter"/>
(Here conv is a link to the converter namespace). Finally, you add it to your cell grid style:
XML
<Setter Property="Background" Value="{Binding Path=PropertyContainingEditableText, Converter={StaticResource statusTextColorConverter}}}"/>
By doing this, you remove the need to implement a UI based logic item in your ViewModel.

Forgive your enemies - it messes with their heads


My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility


GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Nicolai Schrade10-May-11 22:58
Nicolai Schrade10-May-11 22:58 
GeneralRe: WPF Datagrid and MVVM - Binding CellStyle to a ViewModel Property Pin
Pete O'Hanlon10-May-11 23:46
mvePete O'Hanlon10-May-11 23:46 
QuestionStretch StatusBarItem Pin
Jean-Louis Leroy10-May-11 4:06
Jean-Louis Leroy10-May-11 4:06 
AnswerRe: Stretch StatusBarItem Pin
Ian Shlasko10-May-11 4:35
Ian Shlasko10-May-11 4:35 
GeneralRe: Stretch StatusBarItem Pin
Jean-Louis Leroy10-May-11 5:08
Jean-Louis Leroy10-May-11 5:08 
QuestionDatabinding CheckBox in a ListBox Pin
kurmanc9-May-11 6:30
kurmanc9-May-11 6:30 
AnswerRe: Databinding CheckBox in a ListBox Pin
Ian Shlasko9-May-11 6:36
Ian Shlasko9-May-11 6:36 
GeneralRe: Databinding CheckBox in a ListBox [modified] Pin
kurmanc9-May-11 8:38
kurmanc9-May-11 8:38 
GeneralRe: Databinding CheckBox in a ListBox Pin
Ian Shlasko9-May-11 8:46
Ian Shlasko9-May-11 8:46 
GeneralRe: Databinding CheckBox in a ListBox Pin
kurmanc9-May-11 21:23
kurmanc9-May-11 21:23 
GeneralRe: Databinding CheckBox in a ListBox Pin
kurmanc11-May-11 6:11
kurmanc11-May-11 6:11 
GeneralRe: Databinding CheckBox in a ListBox Pin
Ian Shlasko11-May-11 11:47
Ian Shlasko11-May-11 11:47 
GeneralRe: Databinding CheckBox in a ListBox Pin
kurmanc11-May-11 21:17
kurmanc11-May-11 21:17 
QuestionCan you make a treeview behave like a tabcontrol? Pin
bfis1081379-May-11 6:16
bfis1081379-May-11 6:16 
AnswerRe: Can you make a treeview behave like a tabcontrol? Pin
Ian Shlasko9-May-11 6:31
Ian Shlasko9-May-11 6:31 
QuestionBool To Visibility Converter Problem Pin
Kevin Marois9-May-11 5:57
professionalKevin Marois9-May-11 5:57 
AnswerRe: Bool To Visibility Converter Problem Pin
Ian Shlasko9-May-11 6:09
Ian Shlasko9-May-11 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.