How can I bind Foreground to a property in my ViewModel?

前端 未结 2 1213
-上瘾入骨i
-上瘾入骨i 2021-01-01 20:54

I would like to bind the foreground property of a TextBlock to a Property in my ViewModel.

This doesn\'t work :

Edit

View :

<
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-01 21:47

    Check if your solution is like that: View:

    
        
            
        
        
            
        
    
    

    ViewModel:

    public class MainVM : INotifyPropertyChanged
    {
        protected void OnPropertyChanged(string porpName)
        {
            var temp = PropertyChanged;
            if (temp != null)
                temp(this, new PropertyChangedEventArgs(porpName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    
        private System.Windows.Media.Brush _foregroundColor = System.Windows.Media.Brushes.DarkSeaGreen;
    
        public string FullName
        {
            get
            {
                return "Hello world";
            }
        }
    
        public System.Windows.Media.Brush ForegroundColor
        {
            get { return _foregroundColor; }
            set
            {
                _foregroundColor = value;
                OnPropertyChanged("ForegroundColor");
            }
        }
    }
    

    Running app

    and remember that if you want to set new value for ForegroundColor in VM you sholud do it like that:

    ForegroundColor = System.Windows.Media.Brushes.Red;
    

    to raise PropertyChangedEvent

    Accordind to new information about your problem, you could try this solution:

    CustomerHeaderViewModel.cs

    class CustomerHeaderViewModel : INotifyPropertyChanged
    {
        public ObservableCollection Customers { get; set; }
    
        public void LoadCustomers()
        {
            ObservableCollection customers = new ObservableCollection();
    
            //this is where you would actually call your service
            customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
            customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 22 });
            customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 33 });
            customers.Add(new Customer { FirstName = "Robert", LastName = "Smith", NumberOfContracts = 2 });
            customers.Add(new Customer { FirstName = "Hank", LastName = "Jobs", NumberOfContracts = 5 });
    
            Customers = customers;
        }
        protected void OnPropertyChanged(string porpName)
        {
            var temp = PropertyChanged;
            if (temp != null)
                temp(this, new PropertyChangedEventArgs(porpName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    
        private System.Windows.Media.Brush _foregroundColor = System.Windows.Media.Brushes.DarkSeaGreen;
    
        public System.Windows.Media.Brush ForegroundColor
        {
            get { return _foregroundColor; }
            set
            {
                _foregroundColor = value;
                OnPropertyChanged("ForegroundColor");
            }
        }
    }
    

    CustomerHeaderView.xaml

    
        
            
                
                    
                        
                            
                                
                                    
                                    
                                    
                                
                            
                        
                    
                
            
        
    
    

    In presented scenario the ForegroundColor property resides in CustomerHeaderViewModel.cs so it is value for all customers. In CustomerHeaderView.xaml I added x:Name for UserControl to have a possiblity to refer to DataContext of this element. If you don't want to use x:Name for UserControl, you can try this:

    
    

    Remember that DataContext of this control was set earlier in MainWindow.cs.

    MainWindow.cs

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CustomerHeaderViewModel customerHeaderViewModel = new CustomerHeaderViewModel();
            customerHeaderViewModel.LoadCustomers();
            CustomerHeaderView.DataContext = customerHeaderViewModel;
        }
    }
    

    App

提交回复
热议问题