2017-09-20 45 views
0

任何人都可以告訴我如何將組合框的選定項目獲取到字符串變量?獲取WPF組合框SelectedItem屬性,並在C#中使用MVVM模式分配給字符串變量

XAML:

<ComboBox Name="cborevisions" ItemsSource="{Binding purchaseOrderViewModel.purchaseOrderDetailsCollection, Mode=TwoWay}" 
      Grid.Row="0" Grid.Column="2" HorizontalAlignment="Left" Height="25" Width="50" Foreground="Black" Margin="5,5"> 
    <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding MatchesProposalRev.,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Width="50"/> 
      </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

C#:

public ObservableCollection<PurchaseOrderDetailsViewModel> purchaseOrderDetailsCollection 
    { 
     get 
     { 
      return _purchaseOrderDetailsCollection; 
     } 
     set 
     { 
      _purchaseOrderDetailsCollection = value; 
      RaisePropertyChanged("purchaseOrderDetailsCollection"); 
     } 
    } 

C#:

RevisionNo = !string.IsNullOrEmpty(purchaseOrderViewModel.purchaseOrderDetailsCollection.LastOrDefault().MatchesProposalRev) ? purchaseOrderViewModel.purchaseOrderDetailsCollection.LastOrDefault().MatchesProposalRev.ToString() : ""; 

不是傳遞 「MatchesProposalRev」 的,想要以同樣的方式將ComboBox的SelectedItem屬性傳遞給RevisionNo,對於任何使用MVVM設計模式的新建議/解決方案,都會非常感激。

謝謝。

+0

您,您的集合包含的類型的虛擬機添加屬性,並設置在XAML中'的SelectedItem = {結合PropertyOnViewModel}' – Stuart

+0

只是結合將'SelectedItem'綁定到y視圖模型屬性。 – dymanoid

回答

0

XAML:

<ComboBox SelectedItem={Binding SelectedPurchaseOrder}> 

C#

private PurchaseOrderDetailsViewModel _selectedPurchaseOrder; 
public PurchaseOrderDetailsViewModel SelectedPurchaseOrder 
    { 
     get 
     { 
      return _selectedPurchaseOrder; 
     } 
     set 
     { 
      if(_selectedPurchaseOrder== value) return; 
      _selectedPurchaseOrder= value; 
      RaisePropertyChanged(nameof(SelectedPurchaseOrder)); 
     } 
    } 
相關問題