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
1715
Xam data grid dynamic binding
posted

Hi,

i have xamdatagrid and iam adding columns to grid from code behind of viewmodel(initialized event).

iam binding a collection of person objects.

public class persontype
{
public bool isvalid;
}

public class status
{
public string failedstatus;

public string successtatus;
}


public class person
{
public persontype persontype;

public status personstatus;

public string name;

public int age;

public status Status;

}

while binding i want to check if persontype valid or not.if type is valid then i want to set binding path  of status column  to personstatus.successtatus

else ant to set binding path  of status column  to personstatus.failedstatus

how can i achive this.any idea pls share me.

Note. iam completely adding columns from code.

Regards,

Jafar VM

  • 34510
    Offline posted

    Hi Jafar,

    You'll need to use an UnboundField for status column.  Create a converter that takes in a person and checks the 'isvalid' property for true/false.  If true, return "personstatus.successtatus".  If false, return "personstatus.failedstatus".  Then create your unbound field binding but bind it to the whole person object.  You will also attach the converter here. 

    UnboundField uf = new UnboundField { Name = "Status" };
    Binding b = new Binding();
    b.Converter = new PersonStatusConverter();
    uf.Binding = b;
    
    public class PersonStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            person p = (person)value;
            if (p.persontype.isvalid)
                return p.personstatus.successtatus;
            else
                return p.personstatus.failedstatus;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

     

    This will dynamically change what value is displayed for the cells in the status column based on what the value of "isvalid".  Let me know if you have any questions on this.