2013-12-20 4 views
0

내 사용자 정의 컨트롤 내에서 MainControl Repository라는 종속성 속성이 있습니다. 나는이 XAML을XAML을 사용하여 바인딩 종속성 속성

<my:MainControl x:Name="mainControl" Repository="{Binding }" Visibility="Visible" /> 

내부 MainWindow.xaml.cs를를 사용하여 MainWindow를이 저장소 속성을 바인딩 할

public MainWindow() 
{ 
    InitializeComponent(); 
    _repository = new CarRepository(); 
    DataContext = _repository; 
} 
내가

public MainControl() 
{ 
    InitializeComponent(); 
    lblCountCars.Content = string.Format("there is {0} cars.", Repository.CountAllCars()); 
} 
를 사용하여 텍스트를 표시 할 생각

내부 MainControl 생성자

내가 여기서 뭔가 잘못하고있다. Repository="{Binding }"? 모든 코드 뒤에 사용자 정의 컨트롤을 등록 할 때 xaml을 사용하는 방법을 배우고 싶습니다.

업데이트 분명하게 말하십시오. 두 개의 사용자 정의 컨트롤을 사용하는 MainWindow 있습니다. MainControlTreassureControl. 유형의 저장소를이 컨트롤 중 하나에 보내려고 ICarRepository 유형의 Repository이라는 DependencyProperty를 모두 MainControlTreassureControl에 만들었습니다.

내 목표는 MainControlRepository 재산 (DP)에 저장소 인스턴스를 전송하고 또한 내가 간단한 텍스트를 표시 할뿐만 아니라, 추가 작업에 사용자 컨트롤에서이 저장소 인스턴스를 원하는 라벨 Content 속성 lblCountCars.Content = Repository.CountAllCars();

에 인쇄하는 것입니다.

그래서 나는
private ICarRepository _repository; 
public MainWindow() 
{ 
    InitializeComponent(); 
    _repository = new CarRepository(); 
    DataContext = _repository; 
} 

MainControl.xaml

<UserControl x:Name="mainControl"> 
<Label Name="lblCountBooks" Content="{Binding ElementName=mainControl, Path=Repository.CountAllBooks()}" 
<ItemsControl ItemsSource="{Binding ElementName=mainControl, Path=Repository}" /> 

MainControl.xaml MainWindow.xaml

<my:MainControl x:Name="mainControl" Repository="{Binding Repository}" /> 

MainWindow.xaml.cs를

노호 제안했습니다. CS

public static readonly DependencyProperty RepositoryProperty = DependencyProperty.Register(
      "Repository", 
      typeof(ICarRepository), 
      typeof(MainControl), 
      new PropertyMetadata(null)); 

     public ICarRepository Repository 
     { 
      get { return (ICarRepository)GetValue(RepositoryProperty); } 
      set { SetValue(RepositoryProperty, value); } 
     } 

레이블 내용이 예상되는 내용으로 업데이트되지 않습니다.

+0

pls는 너무 귀하의 DP를 들어, MainControl 어쩌면 코드를 사용자의 관련 XAML을 게시 : 예를 들어

blindmeis

답변

2

usercontrols의 종속성 속성을 사용하여 작업 할 때 DP 바인딩에 elementname 바인딩을 사용합니다. 그래서 내가 제거 할 것이다 당신의

lblCountCars.Content = 및 String.format ( Repository.CountAllCars() "{0} 차있다."); 이 같은

와 할 일이 :

<UserControl x:Name="uc"> 
    ...  
    <Label Content="{Binding ElementName=uc, Path=Repository.Count}"/><!-- you can use ContentStringFormat here to get you formattet string--> 
    <ItemsControl ItemsSource="{Binding ElementName=uc, Path=Reportsitory}"/> 

은 MainWindow를 바인딩하는 것은 바로 보이는 당신.

편집 : 속성에 바인딩하면됩니다. Repository.CountAllBooks() 대신에 속성이 필요합니다.ICarRepository에 속성을 만들 기회가 없다면 변환기와 Repository 속성을 convertparameter로 사용하여 정보를 얻을 수 있습니다.

1

이것은 MainControls 데이터 컨텍스트가 생성자에서 설정되지 않았기 때문일 수 있습니다. DataContextChanged 이벤트 (http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.datacontextchanged(v=vs.110).aspx)를 사용해야합니다.

public MainControl() 
{ 
    InitializeComponent(); 
    DataContextChanged += MainControl_DataContextChanged; 
} 

void MainControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) 
{ 
    lblCountCars.Content = string.Format("there is {0} cars.", Repository.CountAllCars());  
} 
관련 문제