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에 대한 경험이 거의 없습니다.
코드 벽을 죄송합니다.이 질문을하는 더 간단한 방법을 모르겠습니다.
이 유일한 이유입니다하지만 DependencyProperty에가 "속성"다음에 속성 이름이 있어야하는 경우 잘 모르겠어요 : 대신,이처럼
UserControl
의 시각적 트리 내에서 설정합니다. 귀하의 경우 DP는 "RowsProperty"일 때 Rows ** Custom ** Property로 노출됩니다. – AndrewS고마워요,하지만 그건 차이를 만들지 못했습니다. – user2023861