WPF Binding to multidimensional array in the xaml

后端 未结 2 1902
遥遥无期
遥遥无期 2020-12-31 16:16

I have trouble to formulate the XAML string to link to a specific element in a multidimensional array.

The DataContext contains the following lines:

         


        
2条回答
  •  执笔经年
    2020-12-31 16:29

    You can bind to a Two dimensional array using a MultivalueConverter defined like this :

     class MultiDimensionalCoverter:IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            return (values[0] as String[,])[(int) values[1], (int) values[2]];  
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    that MultiDimensionalCoverter get 3 parameters, the Two Dimention array plus the two indexes, and the Xaml will be like this :

    
            
        
        
            
                
            
        
    

    defining the indexes as properties in your VM is probably more appropriate, i am using fixed value only to demonstrate.

提交回复
热议问题