我有一个包含此字段的表(id,ParentId,title,...),其名称为HesabCoding。
我想将此表绑定到带有SQL过程的TreeView,即GetAll表行。
型号:
public class HesabCoding : NotifyPropertyChangedHelper, IDataErrorInfo
{
private int id;
private string title;
private string fullTitle;
private string hesabCode;
private string fullHesabCode;
private short hesabGroup_FK;
private string hesabGroup;
private int parentId;
public ObservableCollection<HesabCoding> HesabCodings { get; set; }
public HesabCoding()
{
HesabCodings = new ObservableCollection<HesabCoding>();
}
在HesabCoding VM中:
private ObservableCollection<HesabCoding> _HesabCodingCollection;
public ObservableCollection<HesabCoding> HesabCodingCollection
{
get { return this._HesabCodingCollection; }
set
{
this._HesabCodingCollection = value;
base.OnPropertyChanged("HesabCodingCollection");
}
}
public void HesabCodingSelect()
{
string _errorMessage = "";
int tmpSelectedIndex = SelectedIndex;
ObservableCollection<HesabCoding> _HesabCodingModelCollection = new ObservableCollection<HesabCoding>();
_HesabCodingModelCollection = HesabCodingDAL.HesabCodingSelect(false, out _errorMessage);
}
在Xaml中:
<TreeView Name="TrHesabCoding" Grid.Row="1" ItemsSource="{Binding HesabCodingCollection}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding HesabCodings}" DataType="{x:Type myCustomData:HesabCoding}" >
<TextBlock Text="{Binding Title}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
我在SQL中的过程:
SELECT
hc.id, hc.HesabCode, hc.Title, hc.HesabStructure_FK,
hs2.Title AS HesabStructure,
hc.HesabEntity_FK, he.Title AS HesabEntity,
hc.HesabGroup_FK, hg.Title AS HesabGroup,
ISNULL(hc.ParentId, 0) ParentId,
WHERE
(hc.ParentId = @HesabCodingId OR
(@HesabCodingId IS NULL AND hc.ParentId IS NULL))
DAL班级:
public ObservableCollection<HesabCoding> HesabCodingSelect(bool onlyActive,out string errorMessage)
{
using (var dbcontext = new AccountingDataContext(AppData.userDbConnectionString))
{
try
{
errorMessage = null;
var Ret = dbcontext.HesabCodingSelect(onlyActive);
List<HesabCoding> hesabCodingModelList = new List<HesabCoding>();
CastObjectHelper.CastObjectResultToList(Ret, ref hesabCodingModelList);
ObservableCollection<HesabCoding> hesabCodingModelCollection = new ObservableCollection<HesabCoding>(hesabCodingModelList);
return hesabCodingModelCollection;
}
catch (Exception ex)
{
errorMessage = ex.Message;
return null;
}
}
}
如果parentid为Null则为root。
我的问题是我的树视图没有填充级联并填充级别1。