gpt4 book ai didi

c# - WPF ComboBox XML 绑定(bind)和 ViewModel 绑定(bind)?

转载 作者:行者123 更新时间:2023-11-30 13:05:25 27 4
gpt4 key购买 nike

我有一个包含国家列表的 XML 文件。我在 xaml 中使用 XMLDataProvider 来绑定(bind)组合框的 ItemsSource。我还有一个 viewModel,它有一个我想将所选值绑定(bind)到的属性。我尝试使用本地命名空间绑定(bind)到 View 模型:

SelectedValuePath="国家"

SelectedValue="{Binding local:Project.ProjectInfo.CompanyCountry}"

但是我必须为 xmlProvider 设置 DataContext。

有没有办法让绑定(bind)在 viewModel 中工作?

提前致谢。

最佳答案

将您的 ViewModel 放入您的 .Resources 并绑定(bind)到它?

<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。基本上您有两个数据源 - 一个在数据上下文中,另一个在您的资源中。

编辑:如果需要,您可以切换两者,这并不重要。您可以在资源中拥有任意数量的数据源。

关于c# - WPF ComboBox XML 绑定(bind)和 ViewModel 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4973762/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com