2014-08-27 2 views
0

안녕하세요, 모두 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> 

+1

hm, 그리고 ViewModel의 "SelectedStop"속성은 어디에 있습니까? –

+0

thats 문제를 어떻게 해야할지 모르겠다. 나는 속성을 만드는 방법을 알고 있지만 그것을 사용하고 선택한 값을 할당하는 것을 모른다. – user3182266

+0

실제로 실제로 속성을 만들어야합니다. 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"로 설정되어 있는지 확인하십시오. 변경되었습니다. –

답변

2

이후 Catel을 사용하면 자동으로 변경 알림을 처리합니다. 이 속성을 정의하면됩니다.

public Stop SelectedStop 
{ 
    get { return GetValue<Stop>(SelectedStopProperty); } 
    set { SetValue(SelectedStopProperty, value); } 
} 

public static readonly PropertyData SelectedStopProperty = RegisterProperty("SelectedStop", typeof(Stop)); 

값으로 설정됩니다.

프로 팁 :

public Stop SelectedStop { get; set; } 

을하고 자동으로 위의 기록으로 최종 Catel 속성으로 변환됩니다 : 당신이 Catel.Fody를 사용하는 경우, 당신은이를 작성할 수 있습니다.

+0

감사합니다. 그러나이 메서드를 사용하면 XAML의 바인딩을 다음과 같이 설정해야합니다. SelectedValue = "{SelectedStop, Mode = twoWay 바인딩" – user3182266

+0

: 예. 답변 : WPF에서 사용자 정의 할 수있는 기본 바인딩 모드를 확인하여 실제로 필요한지 확인해야합니다. –

0

그냥 알아낼 수 없습니다

내 뷰 모델입니다 목록의 선택한 항목을 속성에 바인딩하는 방법. 그래서 우선 당신은 당신의보기 모델의 속성을 풀어서 작성해야

public Stop SelectedStop 
    { 
     get 
     { 
      return _selectedStop; 
     } 
     set 
     { 
      if (Equals(value, _selectedStop)) return; 
      _selectedStop = value; 
     } 
    } 

이이 변경된 때 "OnPropertyChanged를"INotifyPropertyChanged 인터페이스를 구현하고 귀하의 재산이 제기되고 텟 있는지 확인합니다. 그리고 목록 상자가 설정해야합니다 당신의 ViewModel에서

<ListBox Grid.Row="1" ItemsSource="{Binding AllStopsCollection}" SelectedValue="{Binding SelectedStop, Mode=TwoWay}" /> 
0

: 당신의 창에서

private stop _selectedStop; 

public Stop SelectedStop 
{ 
    get 
    { 
    return _selectedStop; 
    } 
    set 
    { 
    if (_selectedStop!= value) 
    _selectedStop = value; 
    OnPropertyChanged("SelectedStop"); //U should implement this method using INotifyPropertyChanged 
    } 
} 

(XAML)을 양방향으로의 바인딩 모드를 설정합니다

<ListBox Grid.Row="1" ItemsSource="{Binding AllStopsCollection}" SelectedValue="{Binding SelectedStop, Mode=twoWay}" />