문제

Combobox의 항목 소스를 정수 배열로 설정 하시겠습니까?

도움이 되었습니까?

해결책

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

다른 팁

뷰 모델에서 콤보 박스로 오는 정수 배열을 바인딩하는 것과 비슷한 문제가있었습니다. 여기 나를 위해 일한 것이 무엇입니까?

여기에 XAML이 있습니다. ArrayOfIntegers 에게 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>

다음은 속성이있는 코드와 ViewModel입니다. ArrayOfIntegers

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

예:

<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 };
    }
}
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top