안녕하세요, 모두 ObservableCollection에 의해 채워지는 목록보기가 있습니다. 이제 목록에서 선택한 항목의 값을 가져 와서 저장하려고합니다. 내가 어떻게 이걸 이룰 수 있니? Catel : MVVM listview에서 선택된 값을 가져 와서 변수에 저장하는 방법?
public StopViewModel(IGrtrService grtrService)
{
Argument.IsNotNull(() => grtrService);
_grtrService = grtrService;
AllStops = _grtrService.LoadStop();
Stop_Line = _grtrService.LoadLines();
SearchCollection = new Command(OnSearchPressed);
}
public ObservableCollection<Stop> AllStopsCollection // Must be property or DP to be bound!
{
get { return AllStops; }
set
{
if (Equals(value, AllStops)) return;
AllStops = value;
}
}
public Grtr Grtr
{
get { return GetValue<Grtr>(GrtrProperty); }
set { SetValue(GrtrProperty, value); }
}
public static readonly PropertyData GrtrProperty = RegisterProperty("Grtr", typeof(Grtr));
}
그리고 나는 다음과 같은 코드를 가지고있는 XAML 파일에
이 : 내가 댓글에서 보듯이<catel:StackGrid x:Name="LayoutRoot">
<catel:StackGrid.ColumnDefinitions>
<ColumnDefinition />
</catel:StackGrid.ColumnDefinitions>
<catel:StackGrid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</catel:StackGrid.RowDefinitions>
<ToolBarTray Grid.Row="0" VerticalAlignment="Top" Background="Azure">
<ToolBar>
<TextBox Width="150" Text="{Binding Path=SearchValue}" />
<Button Content="Search" Command="{Binding SearchCollection}" />
<Button Content="Pass Object" Command="{Binding SearchCollection}" />
</ToolBar>
</ToolBarTray>
<ListBox Grid.Row="1" ItemsSource="{Binding AllStopsCollection}" SelectedValue="{Binding SelectedStop}" />
</catel:StackGrid>
hm, 그리고 ViewModel의 "SelectedStop"속성은 어디에 있습니까? –
thats 문제를 어떻게 해야할지 모르겠다. 나는 속성을 만드는 방법을 알고 있지만 그것을 사용하고 선택한 값을 할당하는 것을 모른다. – user3182266
실제로 실제로 속성을 만들어야합니다. public Stop SelectedStop { get {return _selectedStop; } 세트 { if (같음 (value, _selectedStop)) return; _selectedStop = value; }} [INotifyPropertyChanged] (http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx) 인터페이스를 구현하고 해당 속성이 "OnPropertyChanged"로 설정되어 있는지 확인하십시오. 변경되었습니다. –