WPF C#将标签绑定到包含自定义对象的ObervableCollection的DataGrid

WPF C#将标签绑定到包含自定义对象的ObervableCollection的DataGrid,c#,wpf,binding,wpfdatagrid,C#,Wpf,Binding,Wpfdatagrid,好的,这是我的问题。我正在为我的RPG系统创建一个简单的应用程序。这是一个角色图表,玩家可以创建他们的英雄并查看统计数据。下面是英雄的基本职业中的重要内容: namespace CharacterChart.Classes { [DataContract(Name = "Character", Namespace = "http://www.jambs.com")] abstract class Base : INotifyPropertyChanged { public event P

好的,这是我的问题。我正在为我的RPG系统创建一个简单的应用程序。这是一个角色图表,玩家可以创建他们的英雄并查看统计数据。下面是英雄的基本职业中的重要内容:

namespace CharacterChart.Classes
{
[DataContract(Name = "Character", Namespace = "http://www.jambs.com")]
abstract class Base : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    [DataMember]
    private string Name;

    [DataMember]
    public ObservableCollection<Attribute> Attributes;

    protected void OnPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
    public Base()
    {
        this.Name = "Nowa postać";
        Attributes = new ObservableCollection<Attribute>();
        Attributes.Add(new Attribute { Key = 'S', Value = 0 });
        Attributes.Add(new Attribute { Key = 'Z', Value = 0 });
        Attributes.Add(new Attribute { Key = 'P', Value = 0 });
        Attributes.Add(new Attribute { Key = 'B', Value = 0 });
        Attributes.Add(new Attribute { Key = 'C', Value = 0 });
        Attributes.Add(new Attribute { Key = 'I', Value = 0 });
        Attributes.Add(new Attribute { Key = 'W', Value = 0 });
        Attributes.Add(new Attribute { Key = 'D', Value = 0 });
    }

    public string name
    {
        get { return Name; }
        set { Name = value; OnPropertyChanged("Name"); }
    }

    public ObservableCollection<Attribute> attributes
    {
        get { return Attributes; }
        set { Attributes = value; OnPropertyChanged("Attributes"); }
    }
}
}
在主程序中,我声明了一个可观的英雄集合,并为Datagrid设置了一个ItemsSource。在XAML中是这样的:

<DataGrid Name="DataHeroes" Grid.Column="0" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Path=name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <DataGridTextColumn Header="Strength" Binding="{Binding Path=attributes[0].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </DataGrid.Columns>
    </DataGrid>

现在我想添加一个功能,让我在DataGrid中选择一个英雄,然后显示他的所有统计信息。不幸的是,显示属性对我不起作用。以下是我尝试过的:

<TextBlock Text="{Binding ElementName=DataHeroes, Path=SelectedIndex.attributes[0].Value, Mode=OneWay}" Grid.Column="2"/>

我该如何引用我的属性集合并显示所选属性


此示例仅引用单个属性,我稍后将展开它们。

SelectedIndex返回一个整数,该整数显然没有“attributes”属性。将您的线路更改为:

<TextBlock Text="{Binding ElementName=DataHeroes, Path=SelectedItem.attributes[0].Value, Mode=OneWay}" Grid.Column="2"/>

SelectedItem
返回在UI上选择的绑定对象。()


请注意,如果选择了多个项目,这将返回第一个用户选择的项目。

一个很好的问题和修辞性的答案:将DataGrid的SelectedValue绑定到视图模型中的属性,然后将Textblock绑定到该属性的属性[0]。。。不要忘记使用IsSynchronizedWithCurrentItem=true@GayotFow这是一种方法,或者你可以将SelectedItem与ElementName一起使用(链接次数足够多,你可以用很少的代码/不需要代码来做一些非常酷的事情)@BradleyDotNET,是的,但是当SelectedItem是一个源时,警告他如果选择了多个项目会发生什么:)@GayotFow,谢谢添加到我的答案中。对于多选gotchaThanks的完整性+1。我在其他程序中也做了同样的事情,但我复制错了。我检查了数百次,仍然找不到为什么它不工作D现在没事了,再次谢谢。
<TextBlock Text="{Binding ElementName=DataHeroes, Path=SelectedItem.attributes[0].Value, Mode=OneWay}" Grid.Column="2"/>