Wpf列表框与字符串数组绑定

时间:2021-11-06 16:04:41

i have a listbox and this my xaml code;

我有一个列表框,这是我的xaml代码;

        <ListBox x:Name="DepremlerListesi" Margin="0,0,542,14">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="332" Background="#4CFFFFFF">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="132*"/>
                        <ColumnDefinition Width="200*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="40"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid Grid.RowSpan="3" Margin="0,0,12,0"
                          Background="Orange"
                          Width="120"
                          Height="120"
                          HorizontalAlignment="Left">
                        <TextBlock Text="{Binding XX}"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   FontSize="48" Foreground="White"/>
                    </Grid>
                    <TextBlock Grid.Column="1" Grid.ColumnSpan="3"
                               Text="{Binding YY}"
                               FontSize="16"
                               VerticalAlignment="Center"/>
                    <TextBlock Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" Text="Başlık" TextWrapping="Wrap"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Margin" Value="6"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

There is a string array in my hand. I want to add this array and that ListBox. I've searched a lot but I couldn't find enough information.

我手里拿着一个字符串数组。我想添加这个数组和ListBox。我搜索了很多,但我找不到足够的信息。

I tried (this page) the answers on the following page,

我试过(本页)下页的答案,

        ObservableCollection<string> oList;
        oList = new ObservableCollection<string>(MyArray);
        DepremlerListesi.DataContext = oList;

        Binding binding = new Binding();
        DepremlerListesi.SetBinding(ItemsControl.ItemsSourceProperty,binding);

        (DepremlerListesi.ItemsSource as ObservableCollection<string>).RemoveAt(0);

but the result (the yellow boxes are empty);

但结果(黄色方框是空的);

Wpf列表框与字符串数组绑定

the values it's supposed to be this way;

它应该是这样的价值观;

Wpf列表框与字符串数组绑定

With the list maybe it could be, but I don't know how. (this article)

也许它可能是列表,但我不知道如何。 (本文)

If you're wondering if what I want to do, i have a work on earthquakes. This data is the severity of the earthquake. I'm curious about solution suggestions. I hope my broken English with i told description. Thanks.

如果你想知道我想做什么,我有一个关于地震的工作。这些数据是地震的严重程度。我对解决方案建议感到好奇。我希望我的英语破碎,我告诉描述。谢谢。

2 个解决方案

#1


0  

The best way is to set the binding directly in the xaml:

最好的方法是直接在xaml中设置绑定:

Add this in the xaml:

在xaml中添加:

   <ListBox ItemsSource="{Binding DepremlerListesi}" Margin="0,0,542,14">

After that:

public MainWindow()
{
    this.DataContext = this;

    InitializeComponent();

    DepremlerListesi = new ObservableCollection<ClassToBind>();
    DepremlerListesi.Add(new ClassToBind("test1", "test2"));
    DepremlerListesi.Add(new ClassToBind("test3", "test4"));
    DepremlerListesi.Add(new ClassToBind("test5", "test6"));
    OnPropertyChanged("DepremlerListesi");
}

And I created the auxiliar class:

我创建了辅助类:

public class ClassToBind
{     
    public string XX {get;set;}
    public string YY { get; set; }      

    public ClassToBind(string var1, string var2)
    {
        XX = var1;
        YY = var2;
    }
}

#2


0  

Whats XX and YY? If you just want to bind a string array to the listbox, then set the listbox.ItemsSource to the array and simply use "{Binding}", exactly like that and it will bind to the item itself which in this case is the string.

什么是XX和YY?如果你只想将一个字符串数组绑定到列表框,那么将listbox.ItemsSource设置为数组并简单地使用“{Binding}”,就像那样,它将绑定到项本身,在本例中是字符串。

#1


0  

The best way is to set the binding directly in the xaml:

最好的方法是直接在xaml中设置绑定:

Add this in the xaml:

在xaml中添加:

   <ListBox ItemsSource="{Binding DepremlerListesi}" Margin="0,0,542,14">

After that:

public MainWindow()
{
    this.DataContext = this;

    InitializeComponent();

    DepremlerListesi = new ObservableCollection<ClassToBind>();
    DepremlerListesi.Add(new ClassToBind("test1", "test2"));
    DepremlerListesi.Add(new ClassToBind("test3", "test4"));
    DepremlerListesi.Add(new ClassToBind("test5", "test6"));
    OnPropertyChanged("DepremlerListesi");
}

And I created the auxiliar class:

我创建了辅助类:

public class ClassToBind
{     
    public string XX {get;set;}
    public string YY { get; set; }      

    public ClassToBind(string var1, string var2)
    {
        XX = var1;
        YY = var2;
    }
}

#2


0  

Whats XX and YY? If you just want to bind a string array to the listbox, then set the listbox.ItemsSource to the array and simply use "{Binding}", exactly like that and it will bind to the item itself which in this case is the string.

什么是XX和YY?如果你只想将一个字符串数组绑定到列表框,那么将listbox.ItemsSource设置为数组并简单地使用“{Binding}”,就像那样,它将绑定到项本身,在本例中是字符串。