Being a beginner in WPF and MVVM, I have two questions: I build a user control, which is used in a window, and containing two StackPanels.
- In the first SP I have a ListView which has edit button and binded to the ViewModel. The edit button is disabled until an ListItem is selected. When an ListItem is selected, all edit buttons are enabeld. How can this be fixed so that only the edit button of the selected ListItem should be enabled. PS. my edit button is a custom control just to get image + text.
- The second SP, which will include the edit form of the selected ListItem, the goal is that the edit form becomes visible only when I click on the edit button.
Thank you in advance
Edit Button (Column)
<GridViewColumn Header="Edit" Width="auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<cc:MyEditDeleteButton x:Name="BoolToVisibility"
ImageSource="../Images/Edit.png"
Content="Edit"
Command="{Binding EditCommand}" DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"
CommandParameter="{Binding Path=SelectedArticle.Id}"
Width="auto"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
The second StackPanel
<StackPanel Visibility="{Binding ElementName=BoolToVisibility,Path=IsChecked, Converter={ StaticResource BooleanToVisibilityConverter}}">
<Label Content="blablabla"></Label>
</StackPanel>
The used converter:
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>
The class of my custom control -Edit Button- inherit from ToggleButton
public class MyEditDeleteButton : ToggleButton
{
static MyEditDeleteButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyEditDeleteButton), new FrameworkPropertyMetadata(typeof(MyEditDeleteButton)));
}
public ImageSource ImageSource
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
// Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(MyEditDeleteButton), new UIPropertyMetadata(null));
}
So, 2 issues:
When an Listitem is selected, edit button will be enabled in the entire column
En the visibility of the second stackpanel when the edit button is clicked.
Update.
Thanks guys for your reply. the problem is not completely solved, maybe I'm doing something wrong. here's what I did:
@Ed Plunkett. I don't see how I can claim the _EditItem field.
private Item _editItem;
do you mean the item from the ViewModel? In my view model I have a constructor which has no parameter
Edit item = new Item (item);
<StackPanel>
<StackPanel>
... ListView with the Edit Button
</StackPanel>
<StackPanel Visibility="{Binding Mode=OneWay, Source={StaticResource StackCollapsed}}">
<Label Content="blablabla"></Label>
</StackPanel>
</StackPanel>
And in the App.xaml:
<Application.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<Style x:Key="StackCollapsed" TargetType="StackPanel">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=(viewModels:ArticleViewModel.IsEditable), Converter={ StaticResource BooleanToVisibilityConverter}}" Value="{x:Null}">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Application.Resources>
I still see the StackPanel and it visibility has never changed.