简体   繁体   English

绑定清单 <Object> 到C#WPF中的comboxbox

[英]Bind List<Object> to a comboxbox in c# wpf

I have a static class named Building which contains a List<Beam> Beams as its property; 我有一个名为Building的静态类,它包含一个List<Beam> Beams作为其属性;

public static class Building
{
    public static readonly List<Beam> Beams = new List<Beam>();
}

public class Beam
{
    public string Story;
    public double Elevation;
}

I'm trying to Bind the Building.Beams to a combobox in XAML so that Elevation and Story properties of each item in Building.Beams list is displayed in different columns in the combobox. 我试图将Building.Beams Bind到XAML中的组合框,以便Building.Beams列表中每个项目的Elevation和Story属性都显示在组合框中的不同列中。 I have been able to implement the two columns, I just can't Bind these properties. 我已经能够实现这两列,但是我无法绑定这些属性。

Here is what I have tried so far: 到目前为止,这是我尝试过的:

<ComboBox x:Name="cmbBuilding"  ItemsSource="{Binding}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid Width="300">
                <TextBlock Width="150"  Text="{Binding Path=Story }"/>
                <TextBlock Width="150" Text="{Binding Path=Elevation}"/>
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

var b1 = new Beam { Elevation = 320, Story = "ST1" };
var b2 = new Beam { Elevation = 640, Story = "ST2" };
Building.Beams.Add(b1);
Building.Beams.Add(b2);

First of all you can't bind with fields . 首先, 您不能与字段绑定

Convert Story and Elevation to properties (automatic properties in your case will do) 将故事和高程转换为属性(您需要的自动属性即可)

public class Beam
{
    public string Story { get; set;}
    public double Elevation { get; set;}
}

Second, you should use ObservableCollection in case you are adding items to the list after loading finishes so that UI gets notification. 其次,如果要在加载完成后向列表中添加项目,则应该使用ObservableCollection ,以便UI能够收到通知。

public static readonly ObservableCollection<Beam> Beams
                                   = new ObservableCollection<Beam>();

Is it maybe because you have declared Beams as readonly yet you try to ADD items to it? 可能是因为您已将Beams声明为只读,但您尝试向其中添加项目吗? Beams is also defined as a variable, try removing the readonly and making it a property with a getter and setter Beams也被定义为变量,请尝试删除只读并使其具有getter和setter属性

Try this example: 试试这个例子:

XAML

<Grid>
    <ComboBox x:Name="cmbBuilding" Width="100" Height="25" ItemsSource="{Binding Path=Beams}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="300">
                    <TextBlock Width="150" Text="{Binding Path=Story}" HorizontalAlignment="Left" />
                    <TextBlock Width="150" Text="{Binding Path=Elevation}" HorizontalAlignment="Right" />
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <Button Content="Add item" VerticalAlignment="Top" Click="Button_Click" />
</Grid>

Code-behind

public partial class MainWindow : Window
{
    Building building = new Building();

    public MainWindow()
    {
        InitializeComponent();

        building.Beams = new List<Beam>();

        building.Beams.Add(new Beam 
                           { 
                               Elevation = 320, 
                               Story = "ST1" 
                           });

        this.DataContext = building;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var b1 = new Beam { Elevation = 320, Story = "ST1" };
        var b2 = new Beam { Elevation = 640, Story = "ST2" };

        building.Beams.Add(b1);
        building.Beams.Add(b2);

        cmbBuilding.Items.Refresh();
    }
}

public class Building
{
    public List<Beam> Beams
    {
        get;
        set;
    }
}

public class Beam
{
    public string Story 
    {
        get;
        set;
    }

    public double Elevation
    {
        get;
        set;
    }
}

Some notes

  • When you use properties in the Binding , you need to be properties with get and set, not fields. Binding使用属性时,您需要成为具有get和set的属性 ,而不是具有字段的属性

  • Properties, what were added to the List<T> will automatically update, you should call MyComboBox.Items.Refresh() method, or use ObservableCollection<T> : 属性,添加到List<T>属性将自动更新,您应该调用MyComboBox.Items.Refresh()方法,或使用ObservableCollection<T>

ObservableCollection represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. ObservableCollection表示一个动态数据集合,该集合在添加,删除或刷新整个列表时provides notifications

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM