簡體   English   中英

WPF StackPanel ToolTip綁定失敗

[英]WPF StackPanel ToolTip Binding Failing

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

        }
    }

您的視圖模型應為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");
        }
     }
   }
} 

運行不是框架元素,因此無法綁定。

查看此線程是否提供解決方案。

數據綁定TextBlock在Silverlight / WP7中運行

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM