1

I want to bind the object Info in my xaml column:

<DataGridTextColumn Width="200" Binding="{Binding Path=Message.Info.InfoName, Mode=OneWay, TargetNullValue=No Information available}">
    <DataGridTextColumn.Header>
        <Label>Information</Label>
    </DataGridTextColumn.Header>
</DataGridTextColumn>

For each message I've a constructor and in the same message class I've this property:

public IMessageInformation Info
{
    get
    {
        //if (_info == null)
        //  return "no info available";
        return _info;
    }

    set
    {
        _info = value;
        NotifyPropertyChanged("Info");
    }
}

Now, for each message the WPF column should display either the name of the information (gets from the object Info (Info.InfoName property)) or if the message has no information (other type) it should display "No Info available".

If there is no information available I get no object (Info == null).


My problem:

  • If the message has information the property Info gets an object with the name and the Binding works
  • If the message has no information the property Info gets no object and the column is empty.

What I've tried:

I've tried to insert an if statement into the Info property (getter). The statement works but of course I can't return a string if the property expects the return type object info.

And my other approach TargetNullValue=No Information available} doesn't work too.

2 Answers 2

3

You should add a FallbackValue attribute to your XAML like this:

Binding="{Binding
    Path=Message.Info.InfoName,
    Mode=OneWay,
    FallbackValue=No Information available}"

This is because your binding fails due to the fact that the Info part in your binding path is null. You should see a matching error in your debug output.

Sign up to request clarification or add additional context in comments.

2 Comments

Good one, but will TargetNullValue address the problem in this case, since the source is defined?
No, because the source is not defined :). Source is defined by the whole path, not a part of it. And in this case the path is not fully populated.
0

Depends a bit on how you fetch the data but one solution could be to have an "InfoName" property on the message which you populate when you fetch the data.

List<Message> messages = context.Messages.Select(m => new Message
{
    InfoName = m.Info != null ? m.InfoName : "no info available",
    // etc.
})
.ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.