تعيين ItemsSource من تحرير وسرد لمجموعة من الأعداد الصحيحة؟
-
22-07-2019 - |
سؤال
وتعيين ItemsSource من تحرير وسرد لمجموعة من الأعداد الصحيحة؟
المحلول
<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>
نصائح أخرى
وكان لي مشكلة مماثلة مع ملزمة مجموعة من الأعداد الصحيحة قادمة من ViewModel لتحرير وسرد. هنا ما عملت بالنسبة لي.
وهنا 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 };
}
}
}
لا تنتمي إلى StackOverflow