获取WPF组合框SelectedItem属性并使用MVVM模式分配给c#中的字符串变量

时间:2017-09-20 07:26:13

标签: c# wpf linq mvvm data-binding

有人能告诉我如何将组合框的选定项目变为字符串变量吗?

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() : "";

而不是传递&#34; MatchesProposalRev&#34;,想以同样的方式将combobox SelectedItem属性传递给RevisionNo,非常感谢使用任何建议/解决方案,因为我是MVVM设计模式的新手。

感谢。

1 个答案:

答案 0 :(得分: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));
        }
    }