TreeView in WPF

0

Continuing with the solution of Problem of bindig in WPF with MVVM , in which I already got the DALs answered ( MOCData , EFData , AdoNetData and XMLData ), now I try to be able to change the ListView of MainWindow by TreeView , but I run into the problem that the buttons that manipulate the data are binded to ListView .

<StackPanel Grid.Column="0" >
                <Button x:Name="btnBuscar" Height="40" Width="180" Margin="20" Content="Buscar" 
                        Style="{StaticResource ButtonStyle}"
                        CommandParameter="{Binding ElementName=ListViewPersonas, Path=SelectedItems}"  
                        Command="{Binding Buscar}"/>
                <Button x:Name="btnAnadir"  Height="40" Width="180" Margin="20" Content="Añadir"  
                        Style="{StaticResource ButtonStyle}"

                         CommandParameter="{Binding ElementName=ListViewPersonas, Path=SelectedItems}"  

                        Command="{Binding Anadir}"/>
                <!--Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.Anadir}"/>-->
                <Button x:Name="btnNuevo"   Height="40" Width="180" Margin="20" Content="Editar" 
                        Style="{StaticResource ButtonStyle}"
                        CommandParameter="{Binding ElementName=ListViewPersonas, Path=SelectedItems}"
                        Command="{Binding Editar}"/>
                <!--Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.Editar}"/>-->
                <Button x:Name="btnBorrar"  Height="40" Width="180" Margin="20" Content="Borrar"  
                        Style="{StaticResource ButtonStyle}"
                        CommandParameter="{Binding ElementName=ListViewPersonas, Path=SelectedItems}"  
                        Command="{Binding Borrar}" />
                <Button x:Name="btnOpen"    Height="40" Width="180" Margin="20" Content="Visual"  
                       Style="{StaticResource BigFontButton}"
                        CommandParameter="" Command="{Binding Visual}"/>
            </StackPanel>

by means of the CommandParameter.

CommandParameter="{Binding ElementName=ListViewPersonas, Path=SelectedItems}" 

I have tried in CodeBehind the following:

private void CambiarView(Object sender, RoutedEventArgs e)
    {
        String str = sender.ToString();

        if (str.Contains( "List"))
        {
            ScrollList.Visibility = Visibility.Visible;
            ScrollTree.Visibility = Visibility.Collapsed;

        }
        else
        {
            ScrollList.Visibility = Visibility.Collapsed;
            ScrollTree.Visibility = Visibility.Visible;
            btnAnadir.CommandParameter = "{Binding ElementName=TreeViewPersonas, Path=SelectedValuePath}";
            btnBorrar.CommandParameter = "{Binding ElementName=TreeViewPersonas, Path=SelectedValuePath}";
            btnBuscar.CommandParameter = "{Binding ElementName=TreeViewPersonas, Path=SelectedValuePath}";
        }         
    }

but the solution does not work for me. How can I determine the itemSelected in a TreeView ?

Thank you in advance and greetings. César

    
asked by CesarIriso 10.10.2018 в 09:14
source

1 answer

0

SOLVED: In CodeBehind when changing the View

private void CambiarView(Object sender, RoutedEventArgs e)
    {
        String str = sender.ToString();

        if (str.Contains( "List"))
        {
            ScrollList.Visibility = Visibility.Visible;
            ScrollTree.Visibility = Visibility.Collapsed;

            Binding bindingBuscar = new Binding();

            bindingBuscar.ElementName = "ListViewPersonas";
            bindingBuscar.Path = new PropertyPath("SelectedItem");
            bindingBuscar.Mode = BindingMode.OneWay;
            btnBuscar.SetBinding(Button.CommandParameterProperty, bindingBuscar);
        }

A binding for each button and objective (ListView and TreeView), a bit redundant but at the moment it works well. I'll have to find a way to reduce the code with a List of Buttons or something like that.

Regards. Cesar.

    
answered by 14.10.2018 в 11:19