Shi*_*mmy 3 arrays wpf binding combobox itemssource
将ComboBox的ItemsSource设置为整数数组?
Shi*_*mmy 13
<Window
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">
<Window.Resources>
<x:Array x:Key="Integers" Type="{x:Type sys:Int32}">
<sys:Int32>0</sys:Int32>
<sys:Int32>1</sys:Int32>
<sys:Int32>2</sys:Int32>
</x:Array>
</Window.Resources>
<ComboBox ItemsSource="{Binding Source={StaticResource Integers}}" />
</Window>
Run Code Online (Sandbox Code Playgroud)
我在将来自 ViewModel 的整数数组绑定到 ComboBox 时遇到了类似的问题。这对我有用。
这是 XAML,我们将属性绑定ArrayOfIntegers 到ItemsSourceComboBox
<Window x:Class="POpUpWindow.comboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="comboBox" Height="300" Width="300">
<Grid>
<ComboBox x:Name="combox" IsReadOnly="True"
VerticalAlignment="Center" SelectedIndex="0"
ItemsSource="{Binding ArrayOfIntegers}">
</ComboBox>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是背后的代码和具有属性的 ViewModelArrayOfIntegers
public partial class comboBox : Window
{
private ViewModel mViewModel = new ViewModel();
public comboBox()
{
InitializeComponent();
this.DataContext = mViewModel;
}
}
public class ViewModel : ViewModelBase
{
public ViewModel()
{
ArrayOfIntegers = new int[]{4, 6, 9};
}
private int[] mArrayOfIntegers = new int[3];
public int[] ArrayOfIntegers
{
get { return mArrayOfIntegers; }
set { mArrayOfIntegers = value; }
}
}
Run Code Online (Sandbox Code Playgroud)