简体   繁体   English

WPF数据绑定未显示

[英]WPF Data Binding not showing

So I'm having what should be a simple XAML Data Binding error. 因此,我遇到了应该是一个简单的XAML数据绑定错误。 I've got the below XAML with two classes (so far) that they used for DataBinding, a Row, which contains an ObservableCollection rows. 我已经获得了下面的XAML,其中有两个类(到目前为止),它们用于DataBinding,即一个Row,其中包含一个ObservableCollection行。 The nodes have a bunch of extra associated information and I'm trying to show these nodes in a grid-like fashion (it's going to be used for a path-finding algorithm.) 节点具有大量额外的关联信息,我试图以类似网格的方式显示这些节点(它将用于寻路算法。)

The problem is that the "Here" TextBlock doesn't show up. 问题是“ Here” TextBlock没有显示。 But I know that the Nodes are getting bound properly because their values show up in the Debugging StackPanel. 但是我知道节点绑定正确,因为它们的值显示在Debugging StackPanel中。

<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>

Here's the (stripped) Node class. 这是(剥离的)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;
}

Here's the Row class: 这是Row类:

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

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

Here's the corrected XAML, the class definitions are correct in the question, need to replace "AStarListView" with this XAML. 这是更正的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>

My problem was that I had the ListViews too deeply nested. 我的问题是ListViews嵌套得太深了。 The inner ListView was binding to the "nodes" property of a Node, which didn't exist. 内部ListView绑定到不存在的Node的“ nodes”属性。

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

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