Wpf 是否将组合框的ItemsSource设置为整数数组?
将组合框的ItemsSource设置为整数数组?是:Wpf 是否将组合框的ItemsSource设置为整数数组?,wpf,arrays,combobox,binding,itemssource,Wpf,Arrays,Combobox,Binding,Itemssource,将组合框的ItemsSource设置为整数数组?是: <Window x:Class="IntArrayItemsSource.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid
<Window x:Class="IntArrayItemsSource.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ComboBox ItemsSource="{Binding}"/>
</Grid>
</Window>
namespace IntArrayItemsSource {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1: Window {
public Window1() {
InitializeComponent();
this.DataContext = new int[] { 1, 2, 3, 4, 5, 6, 7 };
}
}
}
名称空间IntArrayItemsSource{
///
///Window1.xaml的交互逻辑
///
公共部分类Window1:Window{
公共窗口1(){
初始化组件();
this.DataContext=newint[]{1,2,3,4,5,6,7};
}
}
}
0
1.
2.
我在将来自ViewModel的整数数组绑定到ComboBox时遇到了类似的问题。
这是对我有用的东西
这里是XAML,在这里我们将属性ArrayFintegers
绑定到组合框的ItemsSource
<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>
<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>
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; }
}
}