在WPF中,树视图和组合框之间没有双向绑定

问题描述 投票:-1回答:1

我有一个具有树视图和组合框的用户控件。树视图具有使用视图模型类绑定的项目源。组合框项目源通过后面的代码给出。我将选择ID路径绑定为Combo之间的两种绑定方式框和树视图。现在,当应用程序运行,控件已填充并显示数据以及何时在Treeview中选择任何项时。.com框项被选中。但是当我从组合框中选择任何项时,对应的项没有在树视图中选择。虽然有两种绑定方式。请帮助我解决此问题。我的目标是当用户在组合框中选择任何项目时,应在树视图中选择相同的项目我的用户控件:

  <UserControl x:Class="gsktid.MyUserControl.TreeView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
             xmlns:local="clr-namespace:gsktid.MyUserControl"
          xmlns:local1="clr-namespace:gsktid.ViewModel" 
          xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">

<StackPanel>
    <TextBlock HorizontalAlignment="Left" Margin="20,0,0,0" Width="Auto" Text="Search:" TextWrapping="Wrap" VerticalAlignment="Center" FontFamily="Arial"/>
    <ComboBox x:Name="cmbSerchBox" Width="120" DisplayMemberPath = "FirstName"  SelectedIndex = "-1"   SelectedValuePath = "Id"  SelectedValue="{Binding  Path=SelectedId,Mode=TwoWay}"   HorizontalAlignment="Left" Margin="8"  >
  Path=SelectedId}"/>
        </i:EventTrigger>-->
    </ComboBox>
    <TreeView Height="500" x:Name="tvMain" ItemsSource="{Binding Root}" BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Visible">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding ImagePath}" MaxHeight="25" MaxWidth="25"/>
                    <TextBlock VerticalAlignment="Center">            
                    <TextBlock.Text>
                        <MultiBinding StringFormat=" {0} ">
                            <Binding Path="FirstName"/>

                        </MultiBinding>
                    </TextBlock.Text>
                    </TextBlock>
                    <StackPanel.Style>
                        <Style>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsSelected}" Value="true">
                                    <Setter Property="StackPanel.Background" Value="LightBlue"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="True"/>
                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
            </Style>
        </TreeView.ItemContainerStyle>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectedItemChanged">
                <i:InvokeCommandAction Command="{Binding SelectedCommand}" 
                                       CommandParameter="{Binding ElementName=tvMain, Path=SelectedItem}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TreeView>


</StackPanel>

我在此用户控件后面的代码是:

using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using gsktid.ViewModel;
using gsktid.Model;
namespace gsktid.MyUserControl
 {

     public partial class TreeView : UserControl
{
    public TreeView()
    {
        InitializeComponent();
        this.DataContext = OrgTreeViewModel.Instance();
        cmbSerchBox.ItemsSource = OrgChartManager.Instance().GetChildrenSerch();

    }
}      

}


这里组合框通过在代码中提供项目源来填充机智数据集。但是Treeview的项目源是通过视图模型数据集填充的。这两个数据集都有一个公用密钥。这就是我在其中选择项目的方式组合框中的树视图对应项被选中。但是反之亦然。当我在组合框中选择一项时,即使树视图中的两种绑定方式都被选中了

我尝试过的:

我已经尝试在组合框和树视图之间进行绑定。作为两种方式进行绑定..但是它只能以一种方式工作。每当我在Treeview中进行选择更改时,它都会反映在组合框中..但是当我进行选择时组合框中的更改不会影响树视图]

c# wpf
1个回答
0
投票

这不是我的意思!我的意思是我们需要一些东西,可以在其中创建一个新的Visual Studio WPF应用程序项目,将代码粘贴到其中,然后运行并查看问题。您无法使用当前问题中的代码执行此操作,对吧?

© www.soinside.com 2019 - 2024. All rights reserved.