0

我有一个 TreeView,它的 ItemsSource 绑定到 StaticResource,它是一个类。这对我来说效果很好,但现在我有一个函数可以用可能的不同值更新 TreeView 的最低级别,并且需要立即显示此更新。我已经完成了类似的任务,例如将树中的复选框 IsChecked 绑定到模型中的值,以及将文本块文本绑定到模型中的值。

下面是 XAML 中 TreeView 的参考代码:

<!-- TREE VIEW ON LEFT HAND SIDE. LOADS TREEVIEWPARENTS, WHICH HAVE ORDER ATTRIBUTE CHILDREN -->
<DockPanel Name="test1" Margin="10,10,0,10" VerticalAlignment="Stretch" Grid.Row="3" Grid.RowSpan="6" Grid.Column="0">
    <DockPanel.Resources>
        <local:CheckBoxCommand x:Key="cbc"></local:CheckBoxCommand>
        <src:TreeViewFilter x:Key="MyList" />

        <!-- PARENTS OF THE TREEVIEW -->
        <HierarchicalDataTemplate DataType="{x:Type src:TreeViewParent}" ItemsSource="{Binding Path=OrderAttributes}">
            <TextBlock Text="{Binding Path=NameAndCount}" FontSize="24"/>
        </HierarchicalDataTemplate>

        <!-- CHILDREN OF THE PARENTS. THESE ORDER ATTRIBUTES HAVE CHILDREN OF THEIR OWN -->
        <HierarchicalDataTemplate DataType="{x:Type src:OrderAttribute}" ItemsSource="{Binding Path=OrderAttributes}">
            <StackPanel Name="test" Orientation="Horizontal" VerticalAlignment="Center">
                <CheckBox Command="{StaticResource cbc}"
                            CommandParameter="{Binding Path=NameAndParent}" Visibility="{Binding Path=CheckBoxVisible}" IsChecked="{Binding Path=isChecked}" VerticalAlignment="Center">
                </CheckBox>
                <TextBlock Text="{Binding Path=NameAndCount}" FontSize="16"/>
             </StackPanel>
        </HierarchicalDataTemplate>
    </DockPanel.Resources>

    <TreeView Name="treeView1" BorderThickness="2" ItemsSource="{Binding Source={StaticResource MyList}, NotifyOnSourceUpdated=True}" TreeViewItem.Selected="filterByBatchStatus"/>
</DockPanel>

如您所见,ItemsSource 被绑定到一个 StaticResource MyList,这实际上只是 TreeViewFilter 类的 Name 的一个键。这对我有用的原因是因为树包含的“TreeViewParents”和“OrderAttributes”都是在 TreeViewFilter 类的构造函数中创建的。但现在我希望能够更新树的最低层次结构中的值并让它们可见。

我的猜测是,我可以使用 INotifyPropertyChanged 和触发 propertyChanged 事件或类似的方式来执行此操作,类似于我使用视觉更新进行其他绑定的方式?有任何想法吗?

(另外,绑定中的 NotifyOnSourceUpdated=True 是我刚刚为这个问题搞砸的东西,不知道它是如何工作的)

4

1 回答 1

0

myList 是ObservableCollection吗?此类具有内置通知。

取自这篇格式不佳的 MSDN 文章的实施示例:http: //msdn.microsoft.com/en-us/library/ms748365.aspx

public class NameList : ObservableCollection<PersonName>
{
    public NameList() : base()
    {
        Add(new PersonName("Willa", "Cather"));
        Add(new PersonName("Isak", "Dinesen"));
        Add(new PersonName("Victor", "Hugo"));
        Add(new PersonName("Jules", "Verne"));
    }
  }

  public class PersonName
  {
      private string firstName;
      private string lastName;

      public PersonName(string first, string last)
      {
          this.firstName = first;
          this.lastName = last;
      }

      public string FirstName
      {
          get { return firstName; }
          set { firstName = value; }
      }

      public string LastName
      {
          get { return lastName; }
          set { lastName = value; }
      }
  }

这里有几个链接可以帮助您了解使用带有 observableCollection 的 TreeView 的概念:http: //msdn.microsoft.com/en-us/library/dd759035 (v=vs.95).aspx

迈克·希尔伯格的博客

于 2012-11-12T20:03:19.660 回答