0

Its a simple task still I have not found a solution for it.

I have a list of Objets each holding 2 properties, a String and a byte[]. I am setting the list as an itemscource for my Datatgid. I created a textcolumn with a binding to the string property. Now I want to add a Checkboxcolumn, which should NOT be checked if the byte[] property is NULL, and should be checkt otherwise.

I have found no example for this so far, I guessed that this might be possible to do with a datatrigger but I cant even write somthing down without the compliler complaining.

1
  • Have you tried using a value converter? You'll be able to convert between byte[] and bool, here's a link: wpf-tutorial.com/data-binding/… Commented Dec 3, 2014 at 13:43

1 Answer 1

1

You can use the databinding engine and bind the property directly to the checkbox. Of course, the checkbox is expecting a Nullable<bool>, so you'll have to use a IValueConverter to transform the byte[] array into a bool?:

[ValueConversion(typeof(object), typeof(bool?)]
public class IsNullConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value == null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}


<CheckBox IsChecked="{Binding Path=MyByteArray,
                              Converter={StaticResource MyConverter},
                              Mode=OneWay}"
          IsEnabled="False" />

Note, I marked the binding mode as OneWay and the checkbox as disabled (i.e. readonly) because with the scenario, it doesn't make sense to allow the user to make changes to the state of the checkbox.

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

3 Comments

I cant set the StaticResoure in my XAML. I create an instance of the IsNullConverter called myconverter and try this: <DataGridCheckBoxColumn Header="Bild vorhanden" Binding="{Binding image, Converter={StaticResource myconverter}}" /> VisualStudio complains that it cant find it.
@Lorgarn: See this msdn link on how to create a static resource: msdn.microsoft.com/en-us/library/ms750613(v=vs.110).aspx
It is not working like that. It took me almost all night to figure it out. First, I had to create a lokal namespace, which I could use afterwards to create a resource from the converter class without creating an instance of it in code behind. Then I can use the Key value in the resource as a StaticResource like in the code above. I really apreciate you help and I will mark this as answered, but I cant post the code in this comment and you example would be much more of a help for others if the XAML would be complete.

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.