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