gpt4 book ai didi

c# - 如何为子属性在 Style、Setter 和 HierarchicalDataTemplate 上设置 DataContext 绑定(bind)?

转载 作者:行者123 更新时间:2023-12-03 10:35:07 25 4
gpt4 key购买 nike

如果我使用设置窗口的 DataContext

public partial class MainWindow : Window
{
readonly TreeViewViewModel TreeView;
public MainWindow()
{
//...
this.DataContext = TreeView;
}
}

然后以下 XAML 工作正常
<TreeView x:Name="radTreeView" Grid.Row="1"
Margin = "5,5,5,5"
ItemsSource = "{Binding FirstGeneration}"
Padding = "5"
ScrollViewer.HorizontalScrollBarVisibility = "Visible"
ScrollViewer.VerticalScrollBarVisibility = "Visible"
IsTextSearchEnabled="True" >
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem" >
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" >
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" />
<TextBlock VerticalAlignment="Center" Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

但是,我需要 DataContext 是 ViewModel 的属性,以便 TreeViewViewModel TreeViewMainViewModel mainViewModel 的属性并将代码隐藏中的 DataContext 设置为
public partial class MainWindow : Window
{
private MainViewModel mainViewModel;
public MainWindow()
{
//...
this.DataContext = this;
}
}

我可以使用 DataContext = "{Binding MainViewModel.TreeView}" 获得部分绑定(bind)工作但这仅适用于主 TreeView 元素,不适用于没有 DataContext 属性的 Style、Setter、HierarchicalDataTemplate。

如何正确设置所有 XAML TreeView 元素的数据上下文?

以下是我迄今为止尝试过的,但只有 ItemsSource = "{Binding FirstGeneration}"正在使用正确的数据上下文
<TreeView x:Name="radTreeView" Grid.Row="1"
Margin = "5,5,5,5"
DataContext = "{Binding MainViewModel.TreeView}"
ItemsSource = "{Binding FirstGeneration}"
Padding = "5"
ScrollViewer.HorizontalScrollBarVisibility = "Visible"
ScrollViewer.VerticalScrollBarVisibility = "Visible"
IsTextSearchEnabled="True" >
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem" >
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" >
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" />
<TextBlock VerticalAlignment="Center" Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

最佳答案

假设 DataContext radTreeView 的容器是 MainViewModel 的一个实例你可以做

<TreeView ... DataContext="{Binding TreeView}" ItemsSource="{Binding FirstGeneration}">

或不更改 DataContext
<TreeView ... ItemsSource="{Binding TreeView.FirstGeneration}">

关于c# - 如何为子属性在 Style、Setter 和 HierarchicalDataTemplate 上设置 DataContext 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34529350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com