【问题标题】:Dynamic Path in Treeview TextBlockTreeview TextBlock 中的动态路径
【发布时间】:2021-12-11 04:41:08
【问题描述】:

我正在尝试通过用户选择(即下拉菜单)动态更改 WPF Treeview TextBlock 中的路径。在用户交互时,路径应采用预定义的值,即Name, Type, Order

<TreeView x:Name="Main" ItemsSource="{Binding Items, NotifyOnSourceUpdated=True}"   >
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type models:Root}" 
                                  ItemsSource="{Binding Path=Children}">
            <TextBlock Text="{Binding Path=Name}" /> <--- Dynamically change this
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

在 C# 中有一个 BindingExpression 辅助类,但是我不清楚如何在 ViewModel 场景中使用它

【问题讨论】:

  • 多重绑定可能会起作用。然而,没有足够的信息来提供详细的答案。
  • 不确定多重绑定如何工作,因为它将显示所有绑定属性。但是我只需要显示选定的一个。除非有办法实现 IF 语句。
  • 这是一种误解。 MultiBinding 的转换器可以选择所需的属性值。
  • 你的回复让人大开眼界stackoverflow.com/questions/28817250/… ..

标签: c# wpf binding treeview


【解决方案1】:

如果您想更改实际绑定路径,您需要以编程方式执行此操作。您不能在 XAML 中动态更改绑定路径。

更好的选择是根据ComboBox 中的选择更改源属性的,或者使用带有触发器的Style。像这样的:

<HierarchicalDataTemplate DataType="{x:Type models:Root}" 
                          ItemsSource="{Binding Path=Children}">
    <ContentControl>
        <ContentControl.Style>
            <Style TargetType="ContentControl">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SelectedValue}" Value="Name">
                        <Setter Property="Content">
                            <Setter.Value>
                                <TextBlock Text="{Binding Name}" />
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                    ...
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
</HierarchicalDataTemplate>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    相关资源
    最近更新 更多