2014-07-17 2 views
0

을 실패 내 WPF의 XAML 파일에 다음 코드를했다 :WPF StackPanel의 도구 설명 바인딩

<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 ItemsSource="{Binding}" AutoGenerateColumns="False"> 

UPDATE :

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

     } 
    } 
+1

이 작동 좋아요, 그래서 Binding에 문제가있을 것입니다. 즉, 바인딩에 대해 더 많은 코드를 게시해야합니다. –

답변

1
이 같은 FullName 속성에 대한 INotifyPropertyChanged를 구현해야합니다 귀하의 ViewModel

감사합니다 : 당신은 그냥 대신`{하면 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"); 
     } 
    } 
    } 
} 
+0

고마워,하지만 그 화면에 여전히 달려있다. 내 질문에 도움이 될 수있는 더 많은 코드가 업데이트되었습니다. –

+0

@johndoe 외부에 DataContext가 없습니까? 난 당신이 하나 있다고 생각한다면, 당신은 'DataContext = "{Binding}"와 같은'StackPanel'에 대해'DataContext'를 설정하지 않아도됩니다. " –

+0

그러나 Datagrid 속성의 ItemsSource를 설정하고 있습니다. ItemsSource는 CustomerViewModel 객체를 포함하는 컬렉션입니다. 툴팁의 CustomerViewModel 객체에 그리드의 각 행을 바인딩해야합니다. –

-1

실행 그렇게 결합 할 수없는 frameworkelement 아닙니다.

이 스레드가 솔루션을 제공하는지 확인하십시오.

Databinding TextBlock Runs in Silverlight/WP7

+0

실행을 바인딩 할 수 있습니다. –

+1

'Run'의'Text'는'DependencyProperty'이므로 바인딩 가능합니다. –