[英]WPF Combo Box Binding
I am trying to bind a combo box with some data. 我正在尝试将一些数据绑定到组合框。 The problem is that I have the data in the combo box like this: 问题是我在组合框中有这样的数据:
<ComboBox>
<ComboBoxItem>Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBoxItem>Item 3</ComboBoxItem>
<ComboBoxItem>Item 4</ComboBoxItem>
<ComboBoxItem>Item 5</ComboBoxItem>
</ComboBox>
when the form with the combo box is loaded I have a Resource loaded that has an int that I want to bind it to this combo box. 当加载带有组合框的表单时,我加载了一个具有一个int的资源,我想将其绑定到此组合框。 So if that int is 1 i want the combo box to show Item 1 etc. and when I change the item of the combo box I want to update that int accordingly. 因此,如果该int为1,则我希望组合框显示项目1等,并且当我更改组合框的项目时,我想相应地更新该int。
Is there a way to bind this resource to the combo box to achive that? 有没有一种方法可以将此资源绑定到组合框来实现?
Thank you in advance 先感谢您
Here is a complete XAML sample on how to do this: 这是有关如何执行此操作的完整XAML示例:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1">
<Window.Resources>
<sys:Int32 x:Key="TheIndex">2</sys:Int32>
</Window.Resources>
<ComboBox SelectedIndex="{Binding Source={StaticResource TheIndex}, Mode=OneWay}">
<ComboBoxItem>One</ComboBoxItem>
<ComboBoxItem>Two</ComboBoxItem>
<ComboBoxItem>Three</ComboBoxItem>
<ComboBoxItem>Four</ComboBoxItem>
</ComboBox>
</Window>
Note the following: 请注意以下几点:
sys
XML namespace is declared as a mapping to the System
CLR namespace in the mscorlib assembly sys
XML名称空间在mscorlib程序集中声明为到System
CLR名称空间的映射 Binding
on SelectedIndex
is set to OneWay
because it defaults to TwoWay
, which makes no sense when binding directly to a resource 将SelectedIndex
上的Binding
设置为OneWay
因为它默认为TwoWay
,当直接绑定到资源时这没有任何意义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.