faisalthayyil Ответов: 1

Двусторонняя привязка не происходит между древовидным представлением и полем со списком в WPF


I have a user control which has a tree view and a combo box.Tree view has items source bound using view model class.Combo box item source is given through code behind.I have bound Selected Id path as two way binding between Combo box and tree view.Now when application run, when control has populated and displayed data and when ever I select any item in Treeview..combox box item is getting selected.But when I select any item from combo box , corresponding item is not getting selected in the treeview..though it two way binding..please help me to fix this issue ..my objective is when ever user select any item in combo box , the same item should be selected in tree view My User control :
<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();

    }
}  





 ----------------------------------------------------------
Here Combobox is poulated wit dataset by giving the item source in code behind.But Treeview 's item source is populated through view model data set.Both data set is having one common key .That is how the when I select the item in the tree view corresponding item in the combo box is getting selected.But vice versa is not happening.When I select one item in the combo box , the corresponding item in the tree view is getting selected even though it two way binding</small>

What I have tried:

I have tried to bind between combo box and Tree view..as two way binding  ..but it is working one way .when   ever I makes selection changes in Treeview it is reflecting in the combo  box..but when I makes selection changes in the combo box it is not affecting tree view

1 Ответов

Рейтинг:
1

Gerry Schmitz

Синхронизируйте TreeView с событием (обработчиком) поля со списком "SelectionChanged". Оберните TreeView с помощью метода, который можно вызвать, чтобы "синхронизировать" его с некоторым аргументом.