Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
225
Field property is Null in CellValuePresenter?
posted

I am trying to control the foreground color of cells in a XamTreeGrid. I do this within a converter like the following:

<Style TargetType="{x:Type dataPresenter:CellValuePresenter}"> 

<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self},

Converter={StaticResource ForegroundConverter}}" />

</Style>

 

Inside the converter I need to know the name of the field the CellValuePresenter is for (the coloring is dependent on the name of the field).


The converter looks like the following:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

     var cellValuePresenter = value as CellValuePresenter; // works

     var fieldName = cellValuePresenter.Field.Name; // Field is null!!

Any idea what is happening? Is there another way for me to retrieve the field from a CellValuePresenter (from inside the converter)? I am using 16.2.20162.1006.


Thanks,

Damon

Parents
No Data
Reply
  • 6365
    Verified Answer
    Offline posted

    Hello Damon,

    Thank you for the code-snippets you have provided.

    In order to successfully get the Field of the CellValuePresenter inside the Converter for it's Foreground property, you can simply place your code within a Dispatcher's action in order to synchronize the executions.

    For example:

    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
        {
            var cvp = value as CellValuePresenter;
            var field = cvp.Field;
        }));

        return Binding.DoNothing;
    }

    Another approach I can suggest you is to handle your Foreground logic for the cells inside the Loaded event of the (Tree)CellValuePresenter. This will ensure that whenever the CellValuePresenter gets (pre)loaded, you will be able to set the Foreground at the right time.
    (Note that it depends if you are handling the Loaded event of the CellValuePresenter or the TreeCellValuePresenter, depending on your scenario)

    For example:

    private void TreeCellValuePresenter_Loaded(object sender, RoutedEventArgs e)
    {
        var cvp = (sender as TreeCellValuePresenter);
        if (cvp.Field.Name == "Name")
            cvp.Foreground = Brushes.Red;
    }

    I have attached a sample application, that uses the approach from above.

    If you have any further questions, please let me know.

    XamTreeGrid_sample.zip
Children