我在树视图中显示一个小模型(一项调查)。节点(=答案)有复选框,加载调查时,成功检查标记的答案(属性" IsSelected" = true)。
我遇到的麻烦是扩展那些" IsSelected" =初始加载时为true。我尝试使用样式来完成此操作,但无法使其正常工作。参见,例如,以下样式:
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded"
Value="{Binding Path=IsSelected}">
</Setter>
</Style>
</TreeView.ItemContainerStyle>
这看起来不错(至少对我来说),但它没有效果 - 我猜想datacontext存在问题,并且Binding无法掌握正确的&#34; IsSelected&#34; - 那应该是我也将复选框绑定到的那个。 如何获得正确的datacontext并扩展这些节点?非常感谢您的帮助或提示!
为了完整性,这里是树视图的完整xaml:
<TreeView ItemsSource="{Binding QuestionTree}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Question}" ItemsSource="{Binding Converter={StaticResource QuestionConverter}}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource="{Binding Options}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:GroupOption}" ItemsSource="{Binding Options}">
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Path=Name}"
IsChecked="{Binding Path=IsSelected}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:MainOption}" ItemsSource="{Binding MainOptions}">
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Path=Name}"
IsChecked="{Binding Path=IsSelected}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded"
Value="{Binding Path=IsSelected}">
</Setter>
</Style>
</TreeView.ItemContainerStyle>
还有我的小模特:
public class Question
{
public string Name { get; set; }
public List<MainOption> MainOptions { get; set; }
public List<Group> Groups { get; set; }
}
public class MainOption
{
public string Name { get; set; }
public int MetaItemId { get; set; }
public bool IsSelected { get; set; }
}
public class Group
{
public string Name { get; set; }
public List<GroupOption> Options { get; set; }
}
public class GroupOption
{
public string Name { get; set; }
public int MetaItemId { get; set; }
public bool IsSelected { get; set; }
}
答案 0 :(得分:0)
如果您已将TreeViewItem.IsSelected
属性的数据绑定到数据属性,则可以从TreeViewItem.IsExpanded
属性设置TreeViewItem.IsSelected
属性。试试这个:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding Path=IsSelected,
RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}">
</Setter>
</Style>
虽然这当然会将两个属性联系在一起,这可能是您不想要的。
答案 1 :(得分:0)
我终于让它工作了......傻了我..我为LEAVES设置了“IsExpanded”而不是NODES ..将属性“ContainsSelectedItems”添加到NODE-classes问题和组
public bool ContainsSelectedItems
{
get
{
if (this.MainOptions != null && this.MainOptions.Any(x => x.IsSelected == true))
{
return true;
};
if (this.Groups != null && this.Groups.SelectMany(x => x.Options).Any(y => y.IsSelected == true))
{
return true;
};
return false;
}
}
并在xaml中使用此属性
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding ContainsSelectedItems}" />
</Style>
</TreeView.ItemContainerStyle>
作品!