C# 更改代码隐藏中的DataGrid ItemsSource绑定
如果DataGrid中没有项,我需要能够在代码中更改DataGrid的绑定,反之亦然 目前,我目前的尝试是这样的: C#: 基本上,我想在这里达到同样的效果:C# 更改代码隐藏中的DataGrid ItemsSource绑定,c#,wpf,binding,datagrid,combobox,C#,Wpf,Binding,Datagrid,Combobox,如果DataGrid中没有项,我需要能够在代码中更改DataGrid的绑定,反之亦然 目前,我目前的尝试是这样的: C#: 基本上,我想在这里达到同样的效果: ItemsSource="{Binding ElementName=HeroBox, Path=SelectedValue.Inventory}" 但在C#中 InvGrid DataGrid从运行时随机生成的组合框(HeroBox)中提取选定英雄的清单中的数据。之后,DataGrid应允许用户在不自动生成任何内容的情况下输入任何内容,
ItemsSource="{Binding ElementName=HeroBox, Path=SelectedValue.Inventory}"
但在C#中
InvGrid DataGrid从运行时随机生成的组合框(HeroBox)中提取选定英雄的清单中的数据。之后,DataGrid应允许用户在不自动生成任何内容的情况下输入任何内容,但此时DataGrid会在库存中随机生成一个新的随机项目,允许用户从此处进行更改。不要绑定DataGrid的项源。改为实现组合框的selectionchanged事件,并在其中设置
invgrid.itemssource=((字符)HeroBox.selectedItem)。inventory
(如果HeroBox.selectedItem为空,则添加必要的代码) 您可以考虑绑定到类型<代码>观察表> <代码>,而不是<代码>清单>代码>,因为它已经实现了IntIfyCopyTeC改,它帮助您通知UI已经发生了变化,并且需要更新。
以下是一些有用的链接:
- 用于交换DataGrid中的数据:
<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>
if (InvGrid.Items.Count == 0)
{
Binding b = new Binding("HeroBox")
{
ElementName = "HeroBox",
Path = "SelectedValue.Inventory"
};
InvGrid.ItemsSource = new Binding(b);
}
else
{
InvGrid.ItemsSource = null;
}
ItemsSource="{Binding ElementName=HeroBox, Path=SelectedValue.Inventory}"