wpf 树视图所选项目

发布于 2024-12-11 06:21:16 字数 1124 浏览 1 评论 0 原文

我有一个树视图:

<TreeView>
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=TucActivity}">
      <TextBlock>
        <TextBlock.Text>
          <MultiBinding StringFormat="{}{0} {1}">
            <Binding Path="DisplayedStartTime"></Binding>
            <Binding Path="Name"></Binding>
          </MultiBinding>
        </TextBlock.Text>
      </TextBlock> 
      <HierarchicalDataTemplate.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Path=Message}" />
        </DataTemplate>
      </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

绑定到 Observable Collection 对象:

MainTreeView.ItemsSource = ((App)Application.Current).TucOC;

我希望每次更新 ((App)Application.Current).TucOC 时 所选项目(也是焦点)将是可观察集合中的项目。

我想在一个地方执行此操作,因为 ((App)Application.Current).TucOC 是 更新了代码中的多个位置。

最好的选择是什么?

I have a treeview:

<TreeView>
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=TucActivity}">
      <TextBlock>
        <TextBlock.Text>
          <MultiBinding StringFormat="{}{0} {1}">
            <Binding Path="DisplayedStartTime"></Binding>
            <Binding Path="Name"></Binding>
          </MultiBinding>
        </TextBlock.Text>
      </TextBlock> 
      <HierarchicalDataTemplate.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Path=Message}" />
        </DataTemplate>
      </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

bounded to Observable Collection object:

MainTreeView.ItemsSource = ((App)Application.Current).TucOC;

I want that every time the ((App)Application.Current).TucOC is updated
the selected item (and also the focus) will be the one in the observable collection.

I would like to do it in one place since the ((App)Application.Current).TucOC is
updated in multiple places in the code.

What's the best option to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2024-12-18 06:21:16

如果您使用的是 MVVM,我会在您的 ViewModel 类上创建一个属性,该属性的类型为 ObservableCollection 中保存的类型,以保存您当前选定的项目树视图源。看起来像这样:

private object _selectedTuc;
public object SelectedTuc
{
    get
    {
        return _selectedTuc;
    }
    set
    {
        _selectedTuc = value;
        OnPropertyChanged("SelectedTuc");
    }
}

然后,在树视图中,将此属性绑定到树视图的 SelectedItem:

<TreeView ItemsSource="{Binding TucOC, Mode=OneWay}" SelectedItem="{Binding SelectedTuc, Mode=TwoWay}">...</TreeView>

注意,在 SelectedItem 的绑定上,您指定了 TwoWay 的 Mode 值 - 这允许从 UI 更新您的 SelectedTuc 属性,如下所示每当 SelectedTuc 属性发生变化时,UI 就会更新。

如果您不使用 MVVM 或类似的东西,您将需要创建一个实用方法,每次 ObservableCollection 中的选定项或索引发生更改时,该方法都会更新 TreeView 的 SelectedItem。然而,这不是我推荐的方式。

If you're using a development pattern like MVVM, I would create a property on your ViewModel class that's of the type held in the ObservableCollection, to hold the currently selected item for your treeview source. That would look something like this:

private object _selectedTuc;
public object SelectedTuc
{
    get
    {
        return _selectedTuc;
    }
    set
    {
        _selectedTuc = value;
        OnPropertyChanged("SelectedTuc");
    }
}

Then, in your treeview, you bind this property to the treeview's SelectedItem:

<TreeView ItemsSource="{Binding TucOC, Mode=OneWay}" SelectedItem="{Binding SelectedTuc, Mode=TwoWay}">...</TreeView>

Notice on the binding for SelectedItem you specify a Mode value of TwoWay - this allows for your SelectedTuc property to be updated from the UI, as well as the UI being updated whenever the SelectedTuc property changes.

If you're not using MVVM or something like it, you're going to need to create a utility method that will update the TreeView's SelectedItem every time the selected item or index in your ObservableCollection changes. This is not, however, the way I would recommend doing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文