2014-10-03 3 views
1

기본 창에서 새 창을 표시했습니다. Main.cs에서 Upload.xaml의 ListBox로 일부 데이터를 바인딩해야합니다.부모 창에서 데이터 바인딩

가장 좋은 방법은? 현재로서는이 객체에 액세스 할 수있는 것처럼 보이지 않습니다. 작동하지 않습니다.

나는 창을 열려면 : Main.xaml.cs :

ShowDimmer 방법은 바로 인수에서 폼을 표시
private void upload_Click(object sender, RoutedEventArgs e) 
     { 
      //Show the upload form 
      Upload uploadForm = new Upload(); 
      Functions.ShowDimmer(uploadForm); 

      //Manage result 

     } 

은, 그냥 폼 뒤에를 어둡게 뒤에 반 투명 형태를 생성하고 새로운 것을 위에 올리세요.

그리고 여기이 내가 내 업로드 창을 DataContext를 사용하여 시도했다

//Public list of users and form can access 
     ObservableCollection<User> LoggedUsers = new ObservableCollection<User>(); 
     public ObservableCollection<User> Logged 
     { 
      get 
      { 
       return LoggedUsers; 
      } 
     } 

내가 Main.xaml.cs에 결합하기 위해 노력하고있어 List 객체가

<ListBox Width="542" Height="100" Grid.Column="1" ItemsSource="{Binding Logged, UpdateSourceTrigger=PropertyChanged}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <DockPanel Style="{StaticResource Users}"> 
         <DockPanel.Background> 
          <ImageBrush ImageSource="{Binding ThumbLoc}" /> 
         </DockPanel.Background> 
         <Label> 
          <Label.Content> 
           <TextBlock> 
            <TextBlock.Text> 
             <MultiBinding StringFormat="{}{0} {1}"> 
              <Binding Path="FirstName" /> 
              <Binding Path="LastName" /> 
             </MultiBinding> 
            </TextBlock.Text> 
           </TextBlock> 
          </Label.Content> 
         </Label> 
        </DockPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ListBox> 

업로드에 대한 내 XAML입니다 하지만 액세스 방법을 알 수는 없습니다.

public partial class Upload : Window 
    { 
     public Upload() 
     { 
      InitializeComponent(); 
      DataContext = Main.LoggedUsers; 
     } 
    } 

답변

1

나쁜 습관을 가지고 있습니다.

public partial class Upload : Window { 
    public Upload(YourMainWindowClassName main){ 
     InitializeComponent(); 
     DataContext = main.LoggedUsers; // or whatever your property name is 
    } 
} 

을하고 전화 :하지만 지금,이 시도

Upload uploadForm = new Upload(this); 
+0

나쁜 것은 무엇입니까? –

+1

andreask의 답변을 참조하십시오. abour 디자인 패턴 등을 알아야합니다. –

+0

@MartynBall이 작동하면 답변을 수락합니다. 그렇지 않으면 코드 –

1

난 당신이 LoggedUsers (당신이 결합 할 다른 모든 속성) 별도의 뷰 모델 클래스를 정의하는 게 좋을 것. 그럼 당신은 할 수 있습니다

  • 중 하나로이 뷰 모델을 설정 한 첫 번째 창의 DataContext는 두 번째 창으로 전달하고있다 DataContext 또한
  • 로 설정하거나 App.xaml의 자원의 뷰 모델 클래스를 참조하고 설정 두 창 모두 DataContextStaticResource 마크 업 확장을 사용하여 만듭니다. 이렇게하면 두 개의 창에서 Viewmodel 인스턴스가 하나만 만들어지고 두 창 모두에서 사용됩니다.
관련 문제