WPF StackPanel工具提示绑定失败

时间:2014-07-17 15:40:15

标签: c# wpf

我的WPF XAML文件中有以下代码:

 <DataGrid ItemsSource="{Binding CustomersViewModel}" AutoGenerateColumns="False">
        <DataGrid.Columns>

            <DataGridTemplateColumn Header="Customer">
                <DataGridTemplateColumn.CellTemplate>                   
                    <DataTemplate>
                     <StackPanel DataContext="{Binding}">                            
                        <StackPanel.ToolTip>
                            <ToolTip DataContext="{Binding RelativeSource={RelativeSource Self},
                       Path=PlacementTarget.DataContext}">                   
                                    <TextBlock>
                                    <Run Text="Customer Name: "/>
                                    <Run Text="{Binding FullName}"/>
                                    </TextBlock>
                                </ToolTip>
                            </StackPanel.ToolTip>

                            <TextBlock Text="{Binding FullName}"/> 
                            <TextBlock Text="{Binding Address}" />
                            <TextBlock Text="{Binding BirthDate}"/>


                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>



 public ObservableCollection<CustomerViewModel> CustomersViewModel

我添加了ToolTip DataContext属性,现在崩溃已经消失,但它没有绑定到任何东西,FullName出来是空的。 StackPanel位于Datagrid中,定义如下:

 <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">

更新:

  public partial class CustomersScreen : UserControl
    {
        private ObservableCollection<CustomerViewModel> _customersViewModel;

        public CustomersScreen ()
        {
            InitializeComponent();

            _customersViewModel= new ObservableCollection<CustomerViewModel>()
                {
                    new CustomerViewModel() { FirstName = "Mary", LastName = "Kate", City="Houston", State="Texas", BirthDate = "12/19/2981"}, 
                    new CustomerViewModel() { FirstName = "John", LastName="Doe", City="Austin", State="Texas", BirthDate = "03/01/2001"}
                };


            this.DataContext = _customersViewModel; 
        }

        public ObservableCollection<CustomerViewModel> CustomersViewModel
        {
            get { return _customersViewModel; }

        }
    }

2 个答案:

答案 0 :(得分:1)

您的viewmodel应该为INotifyPropertyChanged属性实现FullName,如下所示:

public class CustomViewModel : INotifyPropertyChanged {
   public CustomViewModel(){
      //....
   }
   protected event PropertyChangedEventHandler propertyChanged;
   protected void OnPropertyChanged(string property){
      var handler = propertyChanged;
      if(handler != null) handler(this, new PropertyChangedEventArgs(property));
   }
   event INotifyPropertyChanged.PropertyChanged {
      add { propertyChanged += value; }
      remove { propertyChanged -= value;}
   }
   public string FullName { 
      get { 
           return String.Format("{0}, {1}", LastName, FirstName);
      } 
   }
   string firstName;
   string lastName; 
   public string FirstName {
     get { return firstName;}
     set {
        if(firstName != value){
           firstName = value;
           OnPropertyChanged("FirstName");
           //also trigger PropertyChanged for FullName
           OnPropertyChanged("FullName");
        }
     }
   }
   public string LastName {
     get { return lastName;}
     set {
        if(lastName != value){
           lastName = value;
           OnPropertyChanged("LastName");
           //also trigger PropertyChanged for FullName
           OnPropertyChanged("FullName");
        }
     }
   }
} 

答案 1 :(得分:-1)

Run不是frameworkelement,因此无法绑定。

查看此主题是否提供了解决方案。

Databinding TextBlock Runs in Silverlight / WP7

此致