-2

This question is related to this question that I asked yesterday and was unable to find an answer for. I am wondering if it is possible in XAML to cast between types. I have found [this answer][2], but I was unable to get that syntax to work. I would like to be able to downcast to a type to access some of it's properties. I would prefer a XAML only method, but if C# needs to be used I can do that as well. Any help is appreciated. Thanks

Edit:

Adding some code to clarify my question.

My issue is when I try to bind to a property specific to one of the subclass unit types, XAML doesn't see it and I just get back 0's. Normally, if this was being done in C# I would just cast it and access the property, but I am not able to do that at this point in my code. The bindings are being created in C# like this:

 dgtc.Header = Properties.Resources.MaxPressure;
 dgtc.MinWidth = 25;
 dgtc.Width = Properties.Settings.Default.MaxPressureColumnWidth;
 dgtc.IsReadOnly = true;
 dgtc.Binding = new Binding("Unit.MaxDepthRelativeToEntry")
 {
     Converter = new DistanceUnitsConverter()
 };

Where dgtc is a DataGridTextColumn. Unit.MaxDepthRelativeToEntry comes through as 0 because it is a property on the subclass of an AUnit, so XAML thinks I am trying to access an non-existent property.

From the second link so far I have tried some of the following syntaxes:

dgtc.Binding = new Binding("AUnit.MaxDepthRelativeToEntry")
dgtc.Binding = new   Binding("Unit(MyNameSpace:AUnit).MaxDepthRelativeToEntry")
dgtc.Binding = new Binding("Unit(MyNameSpace:AUnit.MaxDepthRelativeToEntry)")

and was not able to get any of those to work. I have also tried doing this through a converter, but the issue is, I don't have the list of units available to me when I am constructing the DataGrid/ setting up the bindings/etc. so I am not able to grab the property out of the instance and return it. Does anyone know of any way that I can, preferably in XAML, get to the properties of a subclass type of the type I am binding to?

Community
  • 1
  • 1
KSF
  • 370
  • 1
  • 4
  • 21
  • I don't think so that you can cast in xaml, as it is only a markup, in c# there should be no problem to cast as long as the cast is valid (maybe through converter, or code behind). – adminSoftDK Jul 07 '16 at 16:28

1 Answers1

0

As Eli pointed out, it's difficult to tell exactly what you are trying to accomplish without some example code. However, it's possible that a simple solution for you may be to just use a Binding with a Converter. Here's an (untested) example of a converter that would attempt to cast to a specific type:

public class CastAsMyTypeConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var myTypeObject = value as MyType;
        return myTypeObject ?? Binding.DoNothing;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
Tim Oehler
  • 109
  • 3