Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:28:12+00:00 2026-05-16T18:28:12+00:00

Using the WPF DataGrid I have the need to change various display and related

  • 0

Using the WPF DataGrid I have the need to change various display and related properties of a DataGridCell – such as Foreground, FontStyle, IsEnabled and so on – based on the relevant value of the cell object property.

Now this is easy to do in code, for example (using an Observable Collection of ObservableDictionaries):

  var b = new Binding("IsLocked") { Source = row[column], Converter = new BoolToFontStyleConverter() };
  cell.SetBinding(Control.FontStyleProperty, b);

and works fine, however I cannot see how to do this in XAML since I can find no way to set Path to a cell object’s property.

One XAML attempts is:

<Setter Property="FontStyle">
    <Setter.Value>
        <MultiBinding Converter="{StaticResource IsLockedToFontStyleConverter}" Mode="OneWay" UpdateSourceTrigger="PropertyChanged">
              <Binding />
              <Binding RelativeSource="{x:Static RelativeSource.Self}"/>
        </MultiBinding>
    </Setter.Value>
</Setter>

but there is no binding to the IsLocked property

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    var row = (RowViewModel) values[0];
    var cell = (DataGridCell) values[1];
    if (cell != null && row != null)
    {
        var column = DataGridMethods.GetColumn(cell);
        return row[column].IsLocked ? "Italic" : "Normal";
    }

    return DependencyProperty.UnsetValue;
}

Please note that a previous version returned row[col].IsLocked and set the FontStyle using a DataTrigger but a returned object is not databound.

Note, of course, that the application does not know what the columns are at design time.

Finally DataTable’s are far too inefficient for my requirements but I would be interested to see how this is done with DataTables anyway, if there is such a solution for them, this might be useful elsewhere (although I prefer using collections).

Surely this is a common issue and I am a WPF noobie trying to go all MVVM on my project, but this issue is holding me back with respect to using the WPF DataGrid.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:28:12+00:00Added an answer on May 16, 2026 at 6:28 pm

    Well here is the simplest solution I have found. (Actually I had it before I posted this and the other question but was embarrased at such a solution.Since have heard nothing else here and just it is in case anyone else is faced with the same problem, I thought I would share it.)

    Put a reference to the cell object in the DataGridCell Tag property. I do this with a combination of XAML and a code binding inside a converter as follows:

       <Setter Property="Tag">
           <Setter.Value>
               <MultiBinding Converter="{StaticResource CellViewModelToTagConverter}" Mode="OneWay" UpdateSourceTrigger="PropertyChanged">
                  <Binding />
                  <Binding RelativeSource="{x:Static RelativeSource.Self}"/>
              </MultiBinding>
           </Setter.Value>
       </Setter>
    

    and

     public class CellViewModelToTagConverter : IMultiValueConverter
     {
         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
         {
            var row = values[0] as RowViewModel;
            var cell = values[1] as DataGridCell;
            if (row != null && cell != null)
            {
                var column = DataGridMethods.GetColumn(cell);
                // hack within hack!!! (using tag way is itself a hack?)
                var b = new Binding("Self") {Source = row[column]};
                cell.SetBinding(FrameworkElement.TagProperty, b);
                //...
                //return row[column];
                return DependencyProperty.UnsetValue;
            }
            return DependencyProperty.UnsetValue;
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    You can tell what I think of this solution by my comments inside the converter.(I had to add a Self property to the Cell object and make Self=this in the constructor).

    Still it enables my Datagrid coding to be entirely MVVM – if you accept that what I have done inside the converter is consistent with MVVM. Anyway it works!

    So doing it this way I can see and manage everything from XAML such as control such binding only on certain columns by placing the XAML within the relevant column cellstyles (that is not doing this via DataGrid.CellStyle).

    Anyway, an example of usage is

    <Style.Triggers>
          <DataTrigger Value="true" Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag.IsLocked}">
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="IsEnabled" Value="False"/>
           </DataTrigger>
     </Style.Triggers>
    

    On the XAML level it is both simple and IMHO elegant (especially for various ToolTips and Popups for which I make heavy usage of cell object’s properties). However I am sure there is a better way of doing this, is there?

    Hopefully this all goes away when I can use Net 4.0 and dynamic objects, but for this project I cannot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the datagrid from the WPF Toolkit for 3.5. I have a need
I'm using the WPF toolkit datagrid. I have it set to SelectionUnit=Cell and SelectionMode=Extended
In my wpf datagrid I have implemented validation using IDataErrorInfo . When there is
I am using WPF, and attempting to follow the MVVM pattern. Our team has
I'm attempting to print a number of WPF controls (datagrid, charts etc) that are
I'm building a cash management software using WPF for learning purposes, and I'm having
I am trying to implement my first WPF application using an MVVM design pattern.
I have a byte array that can be very big in size. I want
2 Questions : Firstly: Is it possible to Toggle Transparency on a WPF window?
I've got a situation which I want to fetch data from a database, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.