2013-05-03 4 views
0

DependencyProperty에 문제가 있습니다. DependencyProperty를 기본값으로 설정 한 것 이외의 다른 값으로 설정할 수 없습니다. DependencyProperty.Register(...)내 UserControl의 DependencyProperty가 설정되어 있지 않습니다.

DataGrid가 포함 된 UserControl이 있습니다. UserControl에는 비즈니스 개체의 컬렉션을 수락하는 DependencyProperty가 있습니다. 내 DependencyProperty를 통해 설정할 UserControl있는 DataGrid ItemsSource 싶습니다.

내 UserControl.xaml의 데이터 그리드는 다음과 같습니다

<DataGrid ItemsSource="{Binding Rows}" AutoGenerateColumns="True" /> 

내 UserControl.xaml.cs 코드는 다음과 같습니다

public MyGrid() 
{ 
    InitializeComponent(); 
    this.DataContext = this; 
} 

public BObjectCollection Rows 
{ 
    get { return (BObjectCollection)GetValue(RowsCustomProperty); /* never gets called */ } 
    set { SetValue(RowsCustomProperty, value); /* never gets called */ } 
} 
public static DependencyProperty RowsCustomProperty = DependencyProperty.Register("Rows", typeof(BObjectCollection), typeof(MyGrid), new PropertyMetadata(new BObjectCollection(), RowsChanged, RowsCoerce)); 

private static void RowsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    MyGrid tab = (MyGrid)d; //never gets called 
} 

private static object RowsCoerce(DependencyObject d, object value) 
{ 
    MyGrid tab = (MyGrid)d; 
    BObjectCollection newValue = (BObjectCollection)value; 
    return newValue; 
} 

I가 컨트롤의 DependencyProperty를 하나의 UserControl을 포함하고 설정하는 MainWindow를이 BObjectCollection (BindingList를 상속). MainWindow.xaml의 코드는 간단하다 :

<local:MyGrid Rows="{Binding Rows}" /> 

내 MainWindow.xaml.cs를은 다음과 같습니다

public MainWindow() 
{ 
    this._rows = new BObjectCollection(); 
    this._rows.Add(new BObject("AAA", 10)); 
    this._rows.Add(new BObject("BBB", 20)); 
    this._rows.Add(new BObject("CCC", 30)); 

    this.DataContext = this; 
    InitializeComponent(); 
} 

private readonly BObjectCollection _rows; 
public BObjectCollection Rows { get { return this._rows; } } 

내 문제는, 그 나는 MainWindow를의 BObjectCollection에서의 세 가지 항목으로 BObjectCollection을 만들 수 있지만, 내 UserControl 비어 있습니다. 중단 점을 설정하여 RowsCoerce 트립의 중단 점만 확인하고 newValue은 빈 BObjectCollection이며 세 개의 항목이있는 BObjectCollection이 아닙니다. 행 getter 및 setter 내 중단 점 및 RowsChanged 메서드를 절대로 여행.

UserControl이 BObjectCollection을 3 개의 항목 (MainWindow에서 작성한 항목)과 함께 수신하지 못하는 이유를 알 수 없습니다. 나는 여기서 무엇을 잘못 했는가? DependencyProperty를 잘못 설정 했습니까? DependencyProperties에 대한 경험이 거의 없습니다.

코드 벽을 죄송합니다.이 질문을하는 더 간단한 방법을 모르겠습니다.

+0

이 유일한 이유입니다하지만 DependencyProperty에가 "속성"다음에 속성 이름이 있어야하는 경우 잘 모르겠어요 : 대신,이처럼 UserControl의 시각적 트리 내에서 설정합니다. 귀하의 경우 DP는 "RowsProperty"일 때 Rows ** Custom ** Property로 노출됩니다. – AndrewS

+0

고마워요,하지만 그건 차이를 만들지 못했습니다. – user2023861

답변

1
것은 당신은 당신의 UserControlRows 결합을 할당하고 있지만 당신은 그 자체로 UserControlDataContext을 설정하는

. 따라서 바인딩은 의 Rows 속성으로 처리되고 WindowRows 속성에는 적용되지 않습니다.

UserControlDataContext을 설정하는 것은 일반적으로 좋지 않습니다.

<UserControl x:Name="root"> 
    <Grid DataContext={Binding ElementName=root}"> 
     <DataGrid ItemsSource="{Binding Rows}"/> 
    </Grid> 
</UserControl> 
+0

감사합니다. UserControl의 생성자에서'this.DataContext = this'를 제거했을 때 작동했습니다. 나는 DependencyProperties와 Binding을 완전히 이해하지 못할 것이다. – user2023861

+1

probs 없음. 해당 행을 삭제 한 경우에만 우연히 일치하는 것입니다. 윈도우에서'Rows' 속성의 이름을 바꾸면 바뀔 것입니다. 즉,'UserControl'에서'Rows' 속성을 그대로 사용하지 않고 있습니다. –

관련 문제