Person의 observableCollection의 내용을 표시하는 격자와 선택한 행의 속성을 나타내는 두 개의 텍스트 상자가 있습니다. 마스터 디테일보기. IsSynchronizedWithCurrentItem을 사용하여 viewmodel의 컬렉션에 바인딩
당신은 단순히이 작업을 수행 할 수있는 데이터 컨텍스트에 ObservableCollection에 할당 :<Grid>
<Grid Background="Gray">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<igWPF:XamDataGrid Grid.Row="0" DataSource="{Binding}" IsSynchronizedWithCurrentItem="True" />
<StackPanel Grid.Row="1" Orientation="Horizontal">
<TextBox Height="21" Width="100" Margin="5,0,5,0" Text="{Binding Name}"></TextBox>
<TextBox Height="21" Width="100" Text="{Binding Age}"></TextBox>
</StackPanel>
</Grid>
</Grid>
IsSynchronizedWithCurrentItem-속성은 그리드에서 선택한 항목을 확인한다이 텍스트 상자에서 처리 된 하나입니다.
observablecollection이 datacontext에 직접 있지 않고 뷰 모델 (윈도우의 datacontext에 할당 됨)에있을 때이 정확한 작업을 수행 할 수 있는지 궁금합니다.
public class TestViewModel: DependencyObject
{
public TestViewModel(){
Results = new ObservableCollection<Person>();
Results.Add(new Person { Age = "23", Name = "Able" });
Results.Add(new Person { Age = "25", Name = "Baker" });
}
public ObservableCollection<TagDlgmtEntity> Results
{
get { return (ObservableCollection<Person>)GetValue(ResultsProperty); }
set { SetValue(ResultsProperty, value); }
}
public static readonly DependencyProperty ResultsProperty =
DependencyProperty.Register("Results", typeof(ObservableCollection<Person>), typeof(TestViewModel), new PropertyMetadata(null));
}
나는 시각적 트리에서 낮은 수준에 데이터 컨텍스트를 재 할당, 또는 격자의 selectedItem가 속성과 결합하지 싶습니다.
이 방법을 이런 식으로 사용할 수 있습니까?
감사합니다.
나는 다르게 뷰 모델을 구현하는 것이 좋습니다
INotifyPropertyChanged
을 구현합니다. 'INotifyPropertyChanged'를 구현 한 자신의 ViewModelBase를 구현하거나 (더 나은 IMO) 예를 들어 MVVM 프레임 워크를 사용하십시오. [MVVMLight] (http://mvvmlight.codeplex.com/), 괜찮은 ViewModelBase를 무료로 제공합니다. 일반적으로 ViewModel의 의존성 프로퍼티를 둘러 볼 필요는 없습니다. – EagleBeak