C# WPF StackPanel工具提示绑定失败

C# WPF StackPanel工具提示绑定失败,c#,wpf,C#,Wpf,我的WPF XAML文件中有以下代码: <DataGrid ItemsSource="{Binding CustomersViewModel}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="Customer"> <DataGridTemplateColumn.Cel

我的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
我添加了工具提示DataContext属性,现在崩溃消失了,但它没有绑定到任何东西,全名显示为空。StackPanel位于Datagrid内部,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; }

        }
    }

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

看看这个线程是否提供了一个解决方案


关于

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

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");
        }
     }
   }
} 

如果您只是使用一个简单的文本而不是{Binding FullName},它可以正常工作,所以它一定是绑定出了问题。这意味着您需要发布更多关于绑定的代码。谢谢,我试过了,但它仍然挂在屏幕上。我已经用更多的代码更新了我的问题,这可能会有所帮助。@johndoe你没有任何DataContext在外面吗?我以为你有一个,如果没有,你必须为你的StackPanel设置DataContext,比如DataContext={Binding}谢谢你的帮助!我将签出INotifyPropertyChanged。@约翰请尝试查看我更新的关于为全名实现INotifyPropertyChanged的代码,无论何时更改FirstName或LastName,都应触发PropertyChanged。这是因为绑定不能用于只读属性,除非它们是单向的。我把它改成了单向,现在一切都正常了。非常感谢你的帮助!Run中的文本是一个DependencyProperty,因此它是可绑定的。