0

大家好,我有一个由 ObservableCollection 填充的列表视图。现在我想从列表中获取所选项目的值并存储它。我怎样才能做到这一点?

这是我的视图模型:

  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>

4

3 回答 3

2

由于您使用的是 Catel,它会自动为您处理更改通知。只需定义此属性:

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

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

它将被设置为该值。

专业提示:如果你使用Catel.Fody,你可以这样写:

public Stop SelectedStop { get; set; } 

它将自动转换为上面写的最终 Catel 属性。

于 2014-08-27T11:53:31.217 回答
0

正如我从评论中看到的那样,您只是无法弄清楚如何将列表的选定项目绑定到属性。所以首先你需要在你的视图模型中创建对应的属性:

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

确保您实现了INotifyPropertyChanged接口,并且您的属性在更改时会引发“OnPropertyChanged”。对于列表框,您应该设置:

<ListBox Grid.Row="1" ItemsSource="{Binding AllStopsCollection}"   SelectedValue="{Binding SelectedStop, Mode=TwoWay}" /> 
于 2014-08-27T11:38:01.077 回答
0

在您的 ViewModel 中:

private stop _selectedStop;

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

在您的 Window (XAML) 中,将 Binding 的模式设置为 TwoWay :

 <ListBox Grid.Row="1" ItemsSource="{Binding AllStopsCollection}"   SelectedValue="{Binding SelectedStop, Mode=twoWay}" /> 
于 2014-08-27T11:40:29.080 回答