我想知道我想要实现的目标是否可行。
我有一个带有自定义树视图项的分层TreeView,使用绑定。见下文
public class BookingAdapter extends RecyclerView.Adapter<BookingAdapter.ViewHolder> {
private ArrayList<BookEntry> entry;
Context context;
String id;
public BookingAdapter(ArrayList<BookEntry> entry, Context context) {
this.entry = entry;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.booking_card, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final BookEntry currentEntry = entry.get(position);
holder.name.setText(currentEntry.getName());
holder.description.setText(currentEntry.getDescription());
holder.capacity.setText(currentEntry.getCapacity());
holder.tableId.setText(currentEntry.getId());
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id=entry.get(position).getId();
final boolean isChecked=holder.check.isChecked();
if(isChecked)
{
Bundle bundle=new Bundle();
bundle.putString("tableIds",id);
TableAssignConfirm confirm=new TableAssignConfirm();
confirm.setArguments(bundle);
}
}
});
}
@Override
public int getItemCount() {
return entry.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, description, capacity,tableId;
final CheckBox check;
public ViewHolder(final View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
description = (TextView) itemView.findViewById(R.id.description);
capacity = (TextView) itemView.findViewById(R.id.capacity);
tableId=(TextView)itemView.findViewById(R.id.tableId);
check=(CheckBox)itemView.findViewById(R.id.book);
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isCheck=check.isChecked();
if(isCheck)
Log.i("isCheck","I am Check");
Log.i("id",id);
}
});
}
}
}
赞助商,研究和文档是我在树视图的三个级别绑定的类列表。请参阅我的ViewModel下面的赞助商属性:
<TreeView Grid.Row="4" x:Name="myTreeView"
ItemsSource="{Binding Sponsors}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Studies}">
<StackPanel Orientation="Horizontal">
<Image Style="{StaticResource ExpandingImageStyle}">
<Image.Resources>
<BitmapImage x:Key="Icon_Closed" UriSource="/SearchEngine;component/Resources/TreeView/FolderClosed.png"/>
<BitmapImage x:Key="Icon_Open" UriSource="/SearchEngine;component/Resources/TreeView/FolderOpen.png"/>
</Image.Resources>
</Image>
<TextBlock FontSize="13" Text="{Binding Sponsor}" ToolTip="{Binding Path}" Padding="5 0 0 0" Style="{StaticResource FolderContextMenuStyle}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Documents}">
<StackPanel Orientation="Horizontal">
<Image Style="{StaticResource ExpandingImageStyle}">
<Image.Resources>
<BitmapImage x:Key="Icon_Closed" UriSource="/SearchEngine;component/Resources/TreeView/FolderClosed.png"/>
<BitmapImage x:Key="Icon_Open" UriSource="/SearchEngine;component/Resources/TreeView/FolderOpen.png"/>
</Image.Resources>
</Image>
<TextBlock FontSize="13" Text="{Binding Study}" ToolTip="{Binding Path}" Padding="5 0 0 0" Style="{StaticResource FolderContextMenuStyle}" />
</StackPanel>
<!-- Documents template -->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageSource}" Stretch="Uniform" Height="16" />
<TextBlock Text="{Binding Document}" Padding="5 0 0 0" ToolTip="{Binding Path}" FontSize="13" Style="{StaticResource DocumentContextMenuStyle}" />
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</TreeView>
我的三个班级:
public List<TreeSponsor> Sponsors
{
get
{
return _sponsors;
}
set
{
_sponsors = value;
OnPropertyChanged("Sponsors");
}
}
我已将一个IsSelected属性添加到TreeDocument类中,我想将其设置为True或False,具体取决于是否在树视图中选择了Document。此特定节点始终是树视图的最后(第三个)级别。我怎么做到这一点?也许使用触发器?
编辑:
好的,我设法用以下代码做我想做的事情,当我从树视图中选择一个文档时,如果我点击树视图项,它会更新属性。
public class TreeSponsor
{
public string Sponsor { get; set; }
public string Path { get; set; }
public string ImageSource { get; set; }
public List<TreeStudy> Studies { get; set; }
public TreeSponsor(string sponsor, string path)
//public TreeSponsor()
{
Sponsor = sponsor;
Path = path;
ImageSource = "/SearchEngine;component/Resources/TreeView/FolderClosed.png";
Studies = new List<TreeStudy>();
}
}
public class TreeStudy
{
public string Study { get; set; }
public string Path { get; set; }
public string LastDateChange { get; set; }
public string ImageSource { get; set; }
public List<TreeDocument> Documents { get; set; }
public TreeStudy(string study, string path, string date)
{
Study = study;
Path = path;
LastDateChange = date;
ImageSource = "/SearchEngine;component/Resources/TreeView/FolderClosed.png";
Documents = new List<TreeDocument>();
}
}
public class TreeDocument
{
public string Study { get; set; }
public string Document { get; set; }
public string Path { get; set; }
public string ImageSource { get; set; }
public bool IsSelected { get; set; }
public TreeDocument(string study, string document, string path)
{
Study = study;
Document = document;
Path = path;
ImageSource = "/SearchEngine;component/Resources/MessageBox/error.png";
}
}
然而,尽管绑定twoway,我似乎无法通过代码设置IsSelected属性。使用我在代码后面的代码,如果选择了树视图节点的第一级(赞助商),我想通过将其IsSelected属性设置为true来选择第一个Document(第三级)。但选择永远不会改变。
<Setter Property="IsSelected" Value="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />