简体   繁体   English

在代码隐藏中更改DataGrid ItemsSource绑定

[英]Change DataGrid ItemsSource binding in code-behind

I need to be able to change the binding of a DataGrid in the code-behind if the DataGrid does not have items within it, and vice versa. 如果DataGrid中没有项目,则我需要能够在代码背后更改DataGrid的绑定,反之亦然。

As it stands, here is what my current attempt looks like: 就目前而言,这是我目前的尝试:

C#: C#:

List<Character> Characters = new List<Character>
            {
                new Character("Judge Dredd", Gender.Male, CharacterClass.Fighter),
                new Character("Princess Xena", Gender.Female, CharacterClass.Fighter),
                new Character("Hawkeye", Gender.Male, CharacterClass.Ranger),
                new Character("Laura Croft", Gender.Female, CharacterClass.Ranger),
                new Character("Merlin", Gender.Male, CharacterClass.Mage),
                new Character("Wicked Witch of the West", Gender.Female, CharacterClass.Mage)

            };

HeroBox.ItemsSource = Characters;

WPF: WPF:

<DataGrid x:Name="InvGrid"
          Background="DimGray"
          Grid.Column="1"
          ItemsSource="{Binding ElementName=HeroBox, Path=SelectedValue.Inventory}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="Equip"
                                Binding="{Binding Path=Equipped, Mode=TwoWay}"></DataGridCheckBoxColumn>
        <DataGridTextColumn Header="Name"
                            Binding="{Binding Path=Name, Mode=TwoWay}"></DataGridTextColumn>
        <DataGridTextColumn Header="Effect"
                            Binding="{Binding Path=Effect, Mode=TwoWay}"></DataGridTextColumn>
        <DataGridTextColumn Header="Cost"
                            Binding="{Binding Path=Cost, Mode=TwoWay}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

Attempt to change ItemsSource of InvGrid - includes compilation errors(C#): 尝试更改InvGrid的ItemsSource-包括编译错误(C#):

if (InvGrid.Items.Count == 0)
{
    Binding b = new Binding("HeroBox")
    {
        ElementName = "HeroBox",
        Path = "SelectedValue.Inventory"
    };
    InvGrid.ItemsSource = new Binding(b);
} 
else
{
    InvGrid.ItemsSource = null;
}

Essentially, I would like to achieve the same effect here: 本质上,我想在这里达到相同的效果:

ItemsSource="{Binding ElementName=HeroBox, Path=SelectedValue.Inventory}"

But in C#. 但是在C#中。

The InvGrid DataGrid pulls its data from the Inventory of the selected hero from the ComboBox (HeroBox) which is randomly generated at runtime. InvGrid DataGrid从运行时随机生成的ComboBox(HeroBox)的选定英雄的清单中提取数据。 Afterwards, the DataGrid should allow for the user to enter anything without automatically generating anything, yet at the moment the DataGrid randomly generates a new random item in the inventory, allowing the user to change it from there. 此后,DataGrid应该允许用户输入任何内容而不会自动生成任何内容,但此刻DataGrid会在库存中随机生成一个新的随机项目,从而允许用户从那里进行更改。

Don't bind the itemssource of the datagrid. 不要绑定数据网格的itemssource。 Instead implement the selectionchanged event of the combobox and set invgrid.itemssource = ((character)HeroBox.selectedItem).inventory within it (adding the necessary code is case HeroBox.selectedItem is null) 而是实现组合框的selectionchanged事件并在其中设置invgrid.itemssource = ((character)HeroBox.selectedItem).inventory (添加必要的代码是HeroBox.selectedItem为null的情况)

You might consider binding to a list of type ObservableCollection<Character> instead of List<Character> because it has INotifyCollectionChanged implemented already which helps you inform the UI that something has changed and it needs to update. 您可能会考虑绑定到类型为ObservableCollection<Character>List<Character>而不是List<Character>因为它已经实现了INotifyCollectionChanged,可以帮助您通知UI某些更改并且需要更新。

Here some helpful links: 这里有一些有用的链接:

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

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