0

ItemsSource がクラスである StaticResource にバインドされている TreeView があります。これは私にとってはうまくいきましたが、現在、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 に答える