【问题标题】:Set ItemsSource of a ComboBox to an Array of Integers?将 ComboBox 的 ItemsSource 设置为整数数组?
【发布时间】:2010-12-21 23:32:36
【问题描述】:

将 ComboBox 的 ItemsSource 设置为整数数组?

【问题讨论】:

    标签: wpf arrays combobox binding itemssource


    【解决方案1】:

    是的:

    <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 };
        }
    }
    }
    

    【讨论】:

      【解决方案2】:
      <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>
      

      【讨论】:

        【解决方案3】:

        在将来自 ViewModel 的整数数组绑定到 ComboBox 时,我遇到了类似的问题。 这对我有用。

        这里是 XAML,我们将属性 ArrayOfIntegers 绑定到 ComboBox 的 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>
        

        这是后面的代码和具有ArrayOfIntegers属性的ViewModel

        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; } 
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-06-29
          • 2011-09-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-07
          • 1970-01-01
          • 2013-05-20
          • 2023-04-01
          • 1970-01-01
          相关资源
          最近更新 更多