【问题标题】:WPF StackPanel ToolTip Binding FailingWPF StackPanel 工具提示绑定失败
【发布时间】:2014-07-18 02:09:33
【问题描述】:

我的 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; }

        }
    }

【问题讨论】:

  • 如果你只是使用简单的文本而不是{Binding FullName},它工作正常,那么它一定是绑定有问题。这意味着您需要发布更多关于您的绑定的代码。

标签: c# wpf


【解决方案1】:

您的视图模型应该为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");
        }
     }
   }
} 

【讨论】:

  • 谢谢,我试过了,但它仍然挂在那个屏幕上。我已经用更多可能有帮助的代码更新了我的问题。
  • @johndoe 你外面没有任何 DataContext 吗?我以为你有一个,如果没有,你必须为你的StackPanel 设置DataContext,就像这样DataContext="{Binding}"
  • 感谢您的帮助!我将检查 INotifyPropertyChanged。
  • @johndoe 尝试查看我更新的关于实现 INotifyPropertyChanged 的代码,FullNamePropertyChanged 应该在 FirstNameLastName 更改时触发。
  • 原来是因为绑定不能用于只读属性,除非它们是 OneWay。我将其更改为 OneWay,现在一切正常。非常感谢您的帮助!
【解决方案2】:

Run 不是框架元素,因此无法绑定。

看看这个帖子是否提供了解决方案。

Databinding TextBlock Runs in Silverlight / WP7

问候

【讨论】:

  • Text in RunDependencyProperty,因此可以绑定。
猜你喜欢
  • 1970-01-01
  • 2010-10-22
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多