[英]WPF ComboBox XML Binding AND ViewModel Binding?
我有一個包含國家/地區列表的XML文件。 我在xaml中使用XMLDataProvider來綁定組合框的ItemsSource。 我還有一個viewModel,其中包含我想要將所選值綁定到的屬性。 我試圖使用命名空間local綁定到viewmodel:
SelectedValuePath="Country"
SelectedValue="{Binding local:Project.ProjectInfo.CompanyCountry}"
但是我不得不為xmlProvider設置DataContext。
有沒有辦法讓綁定在viewModel中工作?
提前致謝。
如果您的ViewModel是視圖的公共屬性,則可以為視圖命名並以此方式訪問它。
<Window Name="Window"
...>
<ComboBox SelectedValue="{Binding ElementName=Window, Path=ViewModel.Property}" ... />
...或類似的東西。
將ViewModel放在.Resources中並綁定到它?
<UserControl .... xmlns:local="Project">
<UserControl.Resources>
<local:ProjectInfo x:key="ProjectInfo"/>
</UserControl.Resources>
<UserControl.DataContext>
<XmlObjectDataProvider ... />
</UserControl.DataContext>
<ComboBox ItemsSource="{Binding}" SelectedValuePath="Country" SelectedValue="{Binding CompanyCountry,Source={StaticResource ProjectInfo}}"/>
HTH。 基本上你有兩個數據源 - 一個在datacontext中,另一個在你的資源中。
編輯:你可以切換這兩個,如果需要,它並不重要。 您可以在資源中擁有任意數量的數據源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.