Binding a string list to a DataGridTemplateColumn TextBox

前端 未结 1 1769
[愿得一人]
[愿得一人] 2021-01-29 02:38

If I try to do that, I get \"System.Windows.Markup.XamlParseException\".

My XAML code looks like this:



        
1条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-29 03:08

    Additional information from exception is: Two-way binding requires Path or XPath. TextBox Text property has TwoWay binding mode by default. TwoWay bindings do not accept empty bindings like "{Binding}". Try the following.

    
        
            
                
            
        
    
    

    I think, changing your collection type and using some custom type instead of string will be a better solution though: XAML:

    
        
            
                
                
                    
                        
                            
                        
                    
                
            
        
    
    

    Code-behind:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }
    }
    
    public class MainViewModel
    {
        public ObservableCollection ErrorLog { get; set; } = new ObservableCollection
                                                                        {
                                                                            new Error("A"),
                                                                            new Error("B"),
                                                                        };
    }
    
    public class Error
    {
        public Error(string message)
        {
            Message = message;
        }
    
        public string Message { get; set; }
    }
    

    Also consider implement INotifyPropertyChanged interface to be able to change message from view model if needed.

    0 讨论(0)
提交回复
热议问题