WPFツリービューがあります。コードは次のとおりです。
<DockPanel Grid.Row="2" Margin="0,6,0,0" Grid.RowSpan="5" Height="491" VerticalAlignment="Top">
<DockPanel.Resources>
<src:TreeViewFilter x:Key="MyList" />
<HierarchicalDataTemplate DataType="{x:Type src:TreeViewParent}" ItemsSource="{Binding Path=OrderAttributes}">
<TextBlock Text="{Binding Path=Name}" FontSize="16"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type src:OrderAttribute}" ItemsSource="{Binding Path=OrderAttributes}">
<TextBlock Text="{Binding Path=NameAndCount}" />
</HierarchicalDataTemplate>
</DockPanel.Resources>
<TreeView Name="treeView1" Height="490" Width="235" VerticalAlignment="Top" ItemsSource="{Binding Source={StaticResource MyList}, UpdateSourceTrigger=PropertyChanged}" TreeViewItem.Selected="treeViewFilter" />
</DockPanel>
Treeviewは静的リソースにバインドしますMyListおよびTreeViewParentsオブジェクトはTreeViewFilterクラスで作成されます:(データを取得するためにいくつかの不要なSQLコードを削除しました)。
public class TreeViewFilter : ObservableCollection<TreeViewParent>
{
//three tree view parents that wont change
public TreeViewParent allOrders;
public TreeViewParent batchStatus;
public TreeViewParent shippedOrders;
static TreeViewFilter currentInstance1; //maybe set to null, can only create one instance!
public TreeViewFilter()
{
currentInstance1 = this;
//Create and fill out all orders tree filter
Add(allOrders = new TreeViewParent("All Orders", 0));
//Create and fill out batch status tree filter
Add(batchStatus = new TreeViewParent("Batch Status", 0));
int untouchedCount, batchReadyCount, errorCount;
OrderAttribute untouched = new OrderAttribute("Untouched", "Batch Status", 3, untouchedCount);
OrderAttribute batchReady = new OrderAttribute("Batch Ready", "Batch Status", 3, batchReadyCount);
OrderAttribute error = new OrderAttribute("Error", "Batch Status", 3, errorCount);
batchStatus.OrderAttributes.Add(untouched);
batchStatus.OrderAttributes.Add(batchReady);
batchStatus.OrderAttributes.Add(error);
OrderManager currentInstance = OrderManager.getCurrentInstance();
}
public static TreeViewFilter getCurrentInstance()
{
return currentInstance1;
}
}
次に、ツリービューの親が注文属性にバインドします。注文属性には、独自の注文属性のコレクションを含めることもできます。(階層フィルタリングツリービュー)
TreeViewParentとOrderAttributeコードは次のとおりです:(どちらも類似しています)
public class Base
{
public int classIdentifier;
}
public class TreeViewParent:Base {static TreeViewParent currentInstance;
public TreeViewParent(string name, int classIdent)
{
this._name = name;
this._orderAttributes = new ObservableCollection<OrderAttribute>();
classIdentifier = classIdent;
currentInstance = this;
}
public string _name;
public string Name { get { return _name; } }
ObservableCollection<OrderAttribute> _orderAttributes;
public ObservableCollection<OrderAttribute> OrderAttributes
{
get { return _orderAttributes; }
}
public static TreeViewParent getCurrentInstance()
{
return currentInstance;
}
}
public class OrderAttribute:Base {public string parentFilter; static OrderAttribute currentInstance;
public OrderAttribute(string name, string parent, int classIdent)
{
_name = name;
parentFilter = parent;
classIdentifier = classIdent;
_orderAttributes = new ObservableCollection<OrderAttribute>();
currentInstance = this;
}
public OrderAttribute(string name, string parent, int classIdent, int count)
{
_name = name;
parentFilter = parent;
classIdentifier = classIdent;
_count = count;
currentInstance = this;
}
string _name;
public int _count = 0;
public string Name { get { return _name; } }
public string NameAndCount
{
get
{
if (_count == 0)
{
return _name;
}
else
{
return _name + " (" + _count + ")";
}
}
}
ObservableCollection<OrderAttribute> _orderAttributes;
public ObservableCollection<OrderAttribute> OrderAttributes { get { return _orderAttributes; } }
public static OrderAttribute getCurrentInstance()
{
return currentInstance;
}
}
依存関係オブジェクトを使用して、プログラムの実行とカウントの変更に応じて、更新されたNameAndCountデータを動的に表示および変更するにはどうすればよいですか?