XPath : Bind to last item of collection

后端 未结 2 1596
野的像风
野的像风 2021-01-24 12:16

Can I Bind TextBox.Text to last item of an ObservableCollection ?

I tried this:



        
2条回答
  •  终归单人心
    2021-01-24 12:55

    Please try the method following,

    1, use IValueConverter.

    class DataSourceToLastItemConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            IEnumerable items = value as IEnumerable;
            if (items != null)
            {
                return items.LastOrDefault();
            }
            else return Binding.DoNothing;
        }
    
        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    }
    
    
    

    Then binding like this:

    
        
            
        
        
    
    

    提交回复
    热议问题