我无法绑定树视图上的 IsExpanded 属性

Posted

技术标签:

【中文标题】我无法绑定树视图上的 IsExpanded 属性【英文标题】:I can't bind with the IsExpanded property on my treeview 【发布时间】:2021-04-04 04:19:55 【问题描述】:

我正在尝试绑定IsExpanded 属性,但到目前为止没有成功。我需要知道其中一个组何时展开。

 <TreeView Grid.Row="1">

            <!--  Encontrados  -->
            <TreeViewItem
                FontWeight="Bold"
                IsExpanded="True"
                ItemsSource="Binding Banco.Grupos">
                <TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon
                            Width="20"
                            Foreground="Green"
                            Kind="Done" />
                        <TextBlock Text="ENCONTRADOS: " />
                        <TextBlock Text="Binding SaldoEncontrados, StringFormat='0:C', ConverterCulture='PT-BR'" />
                    </StackPanel>
                </TreeViewItem.Header>
                <TreeViewItem.ItemTemplate>
                    
                    <HierarchicalDataTemplate  DataType="x:Type model:Grupo" ItemsSource="Binding Transações">
                                                     
                        <StackPanel Orientation="Horizontal">                               
                            <TextBlock Text="Binding Nome, StringFormat='0: '" />
                            <TextBlock Text="Binding ValorTotal, StringFormat='0:C', ConverterCulture='PT-BR'" />
                        </StackPanel>

                        <HierarchicalDataTemplate.ItemTemplate>                                
                            <DataTemplate>                                    
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto" />
                                        <ColumnDefinition Width="auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="auto" />
                                    </Grid.ColumnDefinitions>

                                    <CheckBox
                                        Grid.Column="0"
                                        FontWeight="Normal"
                                        IsChecked="Binding isEnabled" />

                                    <TextBlock
                                        Grid.Column="1"
                                        FontWeight="Normal"
                                        Text="Binding Transação.DataDaTransação, StringFormat='dd/MM/yyyy'" />

                                    <TextBlock
                                        Grid.Column="2"
                                        Margin="10,0,10,0"
                                        FontWeight="Normal"
                                        Text="Binding Transação.Historico" />

                                    <TextBlock
                                        Grid.Column="3"
                                        HorizontalAlignment="Right"
                                        FontWeight="Normal"
                                        Text="Binding Transação.Valor, StringFormat='0:C', ConverterCulture='PT-BR'" />
                                </Grid>
                            </DataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>
                    </HierarchicalDataTemplate>

                </TreeViewItem.ItemTemplate>
            </TreeViewItem>
            <!--  Não Encontrados  -->
            <TreeViewItem
                FontWeight="DemiBold"
                IsExpanded="False"
                ItemsSource="Binding TransaçõesNãoEncontradas">
                <TreeViewItem.Header>
                    <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                        <materialDesign:PackIcon
                            Width="20"
                            Foreground="Red"
                            Kind="Error" />
                        <TextBlock Text="NÃO ENCONTRADOS: " />
                        <TextBlock Text="Binding SaldoNaoEncontrados, StringFormat='0:C', ConverterCulture='PT-BR'" />
                    </StackPanel>
                </TreeViewItem.Header>
                <TreeViewItem.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto" />
                                <ColumnDefinition x:Name="Coluna1" Width="*" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock
                                Grid.Column="0"
                                FontWeight="Normal"
                                Text="Binding DataDaTransação, StringFormat='dd/MM/yyyy'" />
                            <TextBlock
                                Grid.Column="1"
                                Margin="10,0,10,0"
                                FontWeight="Normal"
                                Text="Binding Historico" />
                            <TextBlock
                                Grid.Column="2"
                                HorizontalAlignment="Right"
                                FontWeight="Normal"
                                Text="Binding Valor, StringFormat='0:C', ConverterCulture='PT-BR'" />
                        </Grid>
                    </DataTemplate>
                </TreeViewItem.ItemTemplate>
            </TreeViewItem>
        </TreeView>

我的小组课:

public class Grupo

   
    public int Id  get; set;        
    public int BancoId  get; set; 
    public string Nome  get; set;                 
    public List<Transações> Transações  get; set;       
    public decimal ValorTotal  get; set;        
    public bool isExpanded  get; set;  = false;              
    public bool isZeroEnabled  get; set; valores


public record Transações(int pk, int fk, bool isEnabled, OfxTransação Transação)

    public bool isEnabled  get; set;  = isEnabled;

XAML:

谁能帮我绑定树视图的 IsExpanded 属性,以便我知道“组”何时展开。

我创建了我的代码示例:https://github.com/Foiolag/TreeviewExample

【问题讨论】:

【参考方案1】:

您可以通过ItemContainerStyle 绑定IsExpanded 属性。确保删除显式IsExpanded="True"

<!--  Encontrados  -->
<TreeViewItem FontWeight="Bold"
              ItemsSource="Binding Banco.Grupos">
    <TreeViewItem.ItemContainerStyle>
      <Style TargetType="TreeViewItem">
        <Setter Property="IsExpanded" Value="Binding IsExpanded, Mode=TwoWay"/>
      </Style>
    </TreeViewItem.ItemContainerStyle>

提示 1: 此样式将被所有子代继承,应用程序还将尝试绑定到您的 Transações 对象上不存在的 IsExpanded 属性。这不会抛出任何异常,只会抛出绑定错误。为了一致性,我宁愿在所有子节点上实现IsExpanded 属性。

提示 2:您的模型类不是可绑定(不通知属性更改)。如果您在视图初始化后更改属性,视图将不会对您的更改做出反应。你必须实现INotifyPropertyChanged 或使用像BindableBase 这样的基类。阅读this。如果属性打算用于只读用途,最好将它们设为只读(删除 setter)。

提示 3:请避免在变量和类名中使用特殊字符?

【讨论】:

【参考方案2】:

项容器的DataContext(在本例中为TreeViewItem)始终是数据项本身。 考虑到这一点,您可以使用 Style 设置绑定:

<Style TargetType="TreeViewItem">
  <Setter Property="IsExpanded" 
          Value="Binding IsExpanded, Mode=TwoWay" />
</Style>

【讨论】:

以上是关于我无法绑定树视图上的 IsExpanded 属性的主要内容,如果未能解决你的问题,请参考以下文章

WPF DataBound 树视图展开/折叠

C#XAML Setter值为True,具体取决于字符串属性值

如何将对象及其属性绑定到树视图

根据Expander的IsExpanded属性值的变化动态设计Control的size

无法对视图创建索引,因为该视图未绑定到架构

将复合对象绑定到树视图 WPF