0
votes

Any help is appreciated. I'm very new to WPF. I have a treeview that is being populated by an xml file. When the treeview item is selected, I need it to show up in a text box. I have the treeview inside a popup and I've got it so that when the textbox in question is selected, it will bring up the popup with treeview prompting the user to make a selection on the treeview. Afterwards it should put that treeview selection back into the same textbox. Here's my code:

    <TextBox Name="text"
    Text="{Binding Path=SelectedItem.name, ElementName=dirTree}"
    Style="{StaticResource CustomTextBoxStyle}"
    Grid.Column="1"
    Margin="47,326,110,140"
    TextChanged="text_TextChanged" />

And the treeview portion:

    <Popup PlacementTarget="{Binding ElementName=text}"
                       VerticalOffset="20"
                       HorizontalOffset="-180"
                       Margin="0,0,465,279"
                       Name="popup1"
                       AllowsTransparency="True"
                       Placement="Top">
                    <Popup.Style>
                        <Style TargetType="{x:Type Popup}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=text, Path=IsFocused}"
                                             Value="True">
                                    <Setter Property="IsOpen"
                                            Value="True" />
                                </DataTrigger>
                                <!--<DataTrigger Binding="{Binding ElementName=popupText, Path=IsFocused}"
                                             Value="True">
                                    <Setter Property="IsOpen"
                                            Value="True" />
                                </DataTrigger>-->
                            </Style.Triggers>
                        </Style>
                    </Popup.Style>
                    <Grid>
                        <Border BorderThickness="2"
                                Background="DodgerBlue"
                                BorderBrush="DodgerBlue"
                                Padding="0"
                                CornerRadius="6">

                            <ScrollViewer Height="300"
                                          Name="scrollViewer1"
                                          Width="175"
                                          BorderBrush="Black"
                                          Background="DodgerBlue">
                                <TreeView Name="dirTree"
                                          ItemsSource="{Binding Source={StaticResource xmldata}, XPath=.}"
                                          VirtualizingStackPanel.IsVirtualizing="False"
                                          VirtualizingStackPanel.VirtualizationMode="Standard"
                                          GotFocus="TreeView1_GotFocus"
                                          SelectedItemChanged="{Binding ElementName=dirTree, Path=SelectedItem}"/>
                            </ScrollViewer>
                        </Border>
                    </Grid>

                </Popup>
1
what are you doing here SelectedItemChanged="{Binding ElementName=dirTree, Path=SelectedItem}"/> how does that even compile??, You seem to be trying to bind an Eventhandler(SelectedItemChanged) to its own SelectedItem (object) ??? - sa_ddam213
The trigger for the popup box is the on same textbox that I want the treeview selection populated back into. That's what I'm trying to do. - Nallware

1 Answers

1
votes

Create an event handler on the SelectedItemChanged event from your TreeView and from there, update your TextBlock.Text.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TreeView x:Name="demoTreeView"
              Margin="10"
              SelectedItemChanged="demoTreeView_SelectedItemChanged">
    </TreeView>

    <TextBlock x:Name="demoTextBox" Grid.Row="1"/>
</Grid>

And in the .cs file :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        demoTreeView.Items.Add("test1");
        demoTreeView.Items.Add("test2");
        demoTreeView.Items.Add("test3");
        demoTreeView.Items.Add("test4");
    }

    private void demoTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        demoTextBox.Text = e.NewValue.ToString();
    }

}

Personalty I would recommend you to use MVVM and bindings on Properties in order to have a nice decoupling between the xaml and the code-behind.