2011-10-05 3 views
3

"Binding"유형의 DependencyProperty를 만드는 데 문제가 있습니다. 다른 형식은 제대로 작동하며 바인딩을 사용하여 채우면 성공적으로 해결됩니다."Binding"유형의 DependencyProperty가 업데이트되지 않습니다.

내 시나리오에서는 DataGrid가 열을 수행하는 것과 거의 같은 방식으로 Raw 바인딩을 잡아서 자식 개체의 속성에 바인딩 할 수 있습니다. 즉, 열에 지정된 각 바인딩에 대해 바인딩합니다. ItemsSource 컬렉션의 각 항목에 DataContext 자체를 바인딩하는 대신

<mg:MultiSelectDataGrid x:Name="Grid" DockPanel.Dock="Left" 
    ItemsSource="{Binding Path=Rows}" DataContext="{Binding}" 
    AutoGenerateColumns="False" UriBinding="{Binding Path=UrlItems}"> 

그리고 내 "MultiSelectDataGrid"에

:

public static readonly DependencyProperty UriBindingProperty = 
     DependencyProperty.Register("UriBinding", typeof(BindingBase), 
      typeof(MultiSelectDataGrid), 
      new PropertyMetadata { PropertyChangedCallback = OnBindingChanged}); 


    private static void OnBindingChanged(DependencyObject d, 
          DependencyPropertyChangedEventArgs e) 
    { 
     // This is never enterred 
    } 


    public BindingBase UriBinding 
    { 
     get { return (BindingBase)GetValue(UriBindingProperty); } 
     set { SetValue(UriBindingProperty, value); } 
    } 

라는 결코 극복 콜백, 결코 설정되지 가져옵니다 속성입니다. 콜백을 사용하여 모든 종류의 순열을 시도했습니다. 나에게 어떤 성공을 준 유일한 이유는 바인딩을 문자열 (예 : UriBinding = "hello")로 대체 한 경우였습니다.이 경우 콜백을 실행하고 속성을 설정하지만 물론 실패합니다. 잘못된 유형.

내가 뭘 잘못하고 있니? 이 예제의 전체로드를 봤는데, 이것이 DataGrid가 수행해야하는 작업이라고 생각합니다.

감사

답변

7
내가 그 알고

호기심 유일한 장소 Binding type 속성이 DataGridTextColumn, DataGridCheckBoxColumn 등으로 파생 DataGridBoundColumn 클래스 ...

그리고 흥미롭게도이 속성은 종속성 속성가 없습니다 있습니다. 일반 CLR 유형 속성입니다. 바인딩의 인프라 스트럭처는 바인딩 유형 DP에 바인딩 할 수 없다는 한계를 기반으로합니다. 같은 클래스의

다른 특성은 아주 잘 가시성과 같은 드프 있으며, 헤더 등

Binding 속성이 동일한에 대한 매우 조잡한 설명과 함께 다음과 같이 선언 DataGridBoundColumn에서

...

이것은 DP가 아니기 때문에 값을 얻는다면 바인딩을 평가할 것입니다.

/// <summary> 
    ///  The binding that will be applied to the generated element. 
    /// </summary> 
    /// <remarks> 
    ///  This isn't a DP because if it were getting the value would evaluate the binding. 
    /// </remarks> 
    public virtual BindingBase Binding 
    { 
     get 
     { 
      if (!_bindingEnsured) 
      { 
       if (!IsReadOnly) 
       { 
        DataGridHelper.EnsureTwoWayIfNotOneWay(_binding); 
       } 

       _bindingEnsured = true; 
      } 

      return _binding; 
     } 

     set 
     { 
      if (_binding != value) 
      { 
       BindingBase oldBinding = _binding; 
       _binding = value; 
       CoerceValue(IsReadOnlyProperty); 
       CoerceValue(SortMemberPathProperty); 
       _bindingEnsured = false; 
       OnBindingChanged(oldBinding, _binding); 
      } 
     } 
    } 
3

@ WPF-IT 솔루션이 작동하는 동안 당신은 CLR 속성을 첨부 할 수 없기 때문에, 그것은, 연결된 속성에 적합합니다. 이 문제를 해결하기 위해 첨부 된 속성을 평소대로 정의하고 BindingOperations.GetBindingBase()을 호출하여 바인딩 개체를 가져올 수 있습니다.

private static void OnMyPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    // Can also use GetBinding(), GetBindingExpression() 
    // GetBindingExpressionBase() as needed. 
    var binding = BindingOperations.GetBindingBase(d, e.Property); 
} 
관련 문제