C# 如何绑定到列表中的元素

C# 如何绑定到列表中的元素,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,我有一个要绑定到内部值的列表 public class LogEntryList { public List<LogEntry> LogEntries { get; set; } public LogEntryList() { LogEntries = new List<LogEntry>(); } } public class LogEntry { public string Name { get; set; } public strin

我有一个要绑定到内部值的列表

public class LogEntryList
{
    public List<LogEntry> LogEntries { get; set; }
    public LogEntryList() { LogEntries = new List<LogEntry>(); }
}
public class LogEntry
{
    public string Name { get; set; }
    public string Data { get; set; }
    public Brush BgColor { get; set; }
    public bool Selected { get; set; }
    public double Type { get; set; }
}
public class LogEntryList
{
    public List<LogEntry> LogEntries { get; set; }
    public LogEntryList() { LogEntries = new List<LogEntry>(); }
}
public class LogEntry
{
    public string Name { get; set; }
    public string Data { get; set; }
    public Brush BgColor { get; set; }
    public bool Selected { get; set; }
    public double Type { get; set; }
}

姓名:

如何将我的ListBoxItems绑定到名称、数据等的值?

根据Clemens的评论使用Window DataContext解决


姓名:
姓名:

DataContext=“logEntryList”
ElementName=“logEntryList”
都是毫无意义的。将LogEntryList实例分配给窗口的DataContext,如
DataContext=new LogEntryList()。此外,绑定的属性sytax较短:
。作为第一步,阅读。不要试图在不知道“某物”的含义的情况下编写“某物”。“这对WPF不起作用。”克莱门斯说得对,谢谢。我想得太多了。
<ListBox Grid.Row="1" DataContext="logEntryList">
    <ListBox.ItemsSource>
        <Binding ElementName="logEntryList" Path="LogEntries" Mode="OneWay" />
    </ListBox.ItemsSource>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Background="Purple">
                <TextBlock VerticalAlignment="Center">Name:</TextBlock>
                <TextBlock VerticalAlignment="Center" Margin="5" Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
public class LogEntryList
{
    public List<LogEntry> LogEntries { get; set; }
    public LogEntryList() { LogEntries = new List<LogEntry>(); }
}
public class LogEntry
{
    public string Name { get; set; }
    public string Data { get; set; }
    public Brush BgColor { get; set; }
    public bool Selected { get; set; }
    public double Type { get; set; }
}
DataContext = logEntryList;
logEntryList.LogEntries.Add(new LogEntry { Name = "Exception 2", Type = 02 });
logEntryList.LogEntries.Add(new LogEntry { Name = "Exception 7", Type = 07 });
<ListBox Grid.Row="1" Background="DarkGray" ItemsSource="{Binding LogEntries}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center" Background="Purple">Name:</TextBlock>
                <TextBlock VerticalAlignment="Center" Margin="5" Text="{Binding Name}" />
                <TextBlock VerticalAlignment="Center" Background="Purple">Name:</TextBlock>
                <TextBlock VerticalAlignment="Center" Margin="5" Text="{Binding Type}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>