Tao*_*ias 1 c# wpf binding combobox
我试图用一些数据绑定一个组合框.问题是我在组合框中有这样的数据:
<ComboBox>
<ComboBoxItem>Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBoxItem>Item 3</ComboBoxItem>
<ComboBoxItem>Item 4</ComboBoxItem>
<ComboBoxItem>Item 5</ComboBoxItem>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
当加载组合框的表单时,我有一个加载的资源,它有一个int,我想将它绑定到这个组合框.因此,如果该int为1,我希望组合框显示项目1等,当我更改组合框的项目时,我想相应地更新该int.
有没有办法将此资源绑定到组合框以实现?
先感谢您
以下是有关如何执行此操作的完整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>
Run Code Online (Sandbox Code Playgroud)
请注意以下事项:
sysXML命名空间声明为映射到System在CLR命名空间mscorlib程序组件Binding上SelectedIndex设置为OneWay,因为它默认为TwoWay,直接绑定到一个资源时,这是没有意义的