c# - WPF 数据绑定(bind)不显示

标签 c# wpf xaml data-binding

所以我遇到了一个简单的 XAML 数据绑定(bind)错误。我得到了下面的 XAML,其中包含两个类(到目前为止),它们用于数据绑定(bind),一个行,其中包含一个 ObservableCollection 行。这些节点有一堆额外的关联信息,我试图以类似网格的方式显示这些节点(它将用于寻路算法。)

问题是“此处”TextBlock 没有显示。但我知道节点已正确绑定(bind),因为它们的值显示在调试堆栈面板中。

<Window.Resources>
    <local:Row x:Key="TestRow">
        <local:Node x="0" y="0" Cost="20" />
        <local:Node x="0" y="1" Cost="20" />
        <local:Node x="0" y="2" Cost="20" />
    </local:Row>
</Window.Resources>
<Grid Name="MainGrid" Margin="10,10,10,10" >
    <Grid.RowDefinitions>
        <RowDefinition Height="150" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <StackPanel Margin="0,25,0,0" 
                DataContext="{Binding ElementName=AStarListView, 
                                      Path=SelectedItem}" 
                x:Name="Debugging" Orientation="Vertical" >
        <TextBlock Text="{Binding x}" />
        <TextBlock Text="{Binding y}" />
        <TextBlock Text="{Binding Cost}" />
    </StackPanel>
    <ListView Grid.RowSpan="2" Margin="2,2,2,2" Grid.Column="1" 
              x:Name="AStarListView" 
              ItemsSource="{StaticResource TestRow}" >
        <ListView.ItemTemplate>
            <DataTemplate DataType="local:Row">
                <ListView ItemsSource="{Binding nodes}" Width="48" Height="48" >
                    <ListView.ItemTemplate>
                        <DataTemplate DataType="local:Node"> 
                            <TextBlock Text="Here" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

这是(剥离的)Node 类。

public class Node : INotifyPropertyChanged
{
    public Tuple<int, int> coordinates { get; set; }

    public int x
    {
        get
        {
            if (this.coordinates == null)
                return -1;
            return this.coordinates.Item1;
        }
        set
        {
            if (this.coordinates != null)
                this.coordinates = new Tuple<int, int>(value, y);
            else
                this.coordinates = new Tuple<int, int>(value, -1);

            OnPropertyChanged("x");
        }
    }
    public int y
    {
        get
        {
            if (this.coordinates == null)
                return -1;
            return this.coordinates.Item2;
        }
        set
        {
            if (this.coordinates != null)
                this.coordinates = new Tuple<int, int>(x, value);
            else
                this.coordinates = new Tuple<int, int>(-1, value);
            OnPropertyChanged("y");
        }
    }

    private Node _parent;

    private int _Cost;
    public int Cost
    {
        get
        {
            return _Cost;
        }
        set
        {
            _Cost = value;
            OnPropertyChanged("Cost");
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChangedEventArgs args = 
                 new PropertyChangedEventArgs(propertyName);
            this.PropertyChanged(this, args);
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

这是 Row 类:

public class Row : ObservableCollection<Node>
{
    public ObservableCollection<Node> nodes { get; set; }

    public Row()
    {
        this.nodes = new ObservableCollection<Node>();
    }
}

最佳答案

这是更正后的 XAML,问题中的类定义是正确的,需要用此 XAML 替换“AStarListView”。

<ListView Grid.RowSpan="2" Margin="2,2,2,2" Grid.Column="1" x:Name="AStarListView" 
           ItemsSource="{StaticResource TestRow}" >

     <ListView.ItemTemplate>
         <DataTemplate DataType="local:Node">
             <Grid Background="#dddddd" >
                 <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding x}"/>
                 <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding y}"/>
                 <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Cost}"/>
                 <Grid.RowDefinitions>
                     <RowDefinition Height="24"/>
                     <RowDefinition Height="24"/>
                 </Grid.RowDefinitions>
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="24"/>
                     <ColumnDefinition Width="24"/>
                 </Grid.ColumnDefinitions>
             </Grid>
         </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

我的问题是我的 ListView 嵌套太深。内部 ListView 绑定(bind)到节点的“节点”属性,该属性不存在。

关于c# - WPF 数据绑定(bind)不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13569602/

相关文章:

wpf - 如何创建工具提示以显示单个控件的多个验证错误?

c# - 处于 Debug模式时的 Application Insights

c# - MVC 通过 C#/WPF : how to notify view?

wpf - VS2015 Profiler 中的外部代码

c# - 双向绑定(bind)简单的 WPF TextBox 文本

c# - 带有自定义刻度的 slider (标签和速度)

C# 代码在 Pinvoke SendMessage 之后挂起

c# - 是否可以避免序列化/反序列化并与内存映射文件 (MMF) 共享大内存对象?

c# WPF,如何在单击按钮时展开表单?

c# - 用于集合 DP 的 WPF TypeConversionAttribute