【问题标题】:Wpf listbox binding with string arrayWpf列表框与字符串数组绑定
【发布时间】:2016-11-08 18:54:48
【问题描述】:

我有一个列表框,这是我的 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>

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

我尝试了(this 页面)下一页上的答案,

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

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

它应该是这样的值;

可能有列表,但我不知道如何。 (this文章)

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

【问题讨论】:

    标签: c# arrays wpf xaml listbox


    【解决方案1】:

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

    在 xaml 代码中添加:

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

    之后:

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

    我创建了辅助类:

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

    【讨论】:

      【解决方案2】:

      什么是 XX 和 YY?如果您只想将字符串数组绑定到列表框,则将 listbox.ItemsSource 设置为数组并简单地使用“{Binding}”,就像那样,它将绑定到项目本身,在这种情况下是字符串。

      【讨论】:

      • 只是简单,公共字符串 XX { get;放; } public int YY { 得到;放; }
      猜你喜欢
      • 2012-05-05
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      相关资源
      最近更新 更多