I have a usercontrol "deletecontrol" in the datatemplate of the listview. With my current code when I click on a button on the usercontrol for a selected Item, usercontrol's command gets executed in all items in the list. I implemented INotifyPropertychanged for the visibility properties of all buttons. MainVM is the datacontext for MainWindow and DeleteVM is the the viewmodel for UserControl. I need help with the visibility of buttons to ensure that btnYes and lblConfirm appears only in the selected item of the listview when I click on the btnX to execute DeleteCmd.
DeleteVM.cs
public RelayCommand DeleteCmd { get; private set; }
public DeleteVM()
{
DeleteCmd = new RelayCommand(() => ShowDelete(), () => true);
}
private void ShowDelete()
{System.Windows.Application.Current.Dispatcher.Invoke
(DispatcherPriority.Normal,(Action)delegate()
{
BtnXVisibility = Visibility.Hidden;
BtnYesVisibility = System.Windows.Visibility.Visible;
LblConfirmVisibility = System.Windows.Visibility.Visible;
Background = Brushes.White;
});
}
MainWindow.xaml
<ListView Name="listView" ItemsSource="{Binding Source={StaticResource collection}}" SelectedItem="{Binding Item, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Width="12" Height="12" Source="{Binding ImageString}"/>
<TextBlock Grid.Column="1">
<TextBlock.Text>
<MultiBinding StringFormat=" {0} - {1} - {2}">
<Binding Path="Timestamp" />
<Binding Path="User" />
<Binding Path="Description" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<view:Deletecontrol Grid.Column="2"
HorizontalAlignment="Right"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Deletecontrol.Xaml
<Grid>
<Button Grid.Column="2" Name="btnX" Foreground="Red"
Visibility="{Binding BtnXVisibility,
Mode=TwoWay, Path=SelectedItem,
RelativeSource={RelativeSource AncestorType={x:Type
ListView}}}" Command="{Binding DeleteCmd}"
CommandParameter="{Binding Path=SelectedItem, RelativeSource=
{RelativeSource AncestorType={x:Type ListView}}}" >
</Button.Background> X</Button>
<Label Grid.Column="0" Name="lblConfirm" Visibility="{Binding
LblConfirmVisibility, Mode=OneWay}">Are you sure? </Label>
<Button Grid.Column="1" Margin="0,0,3,0" Width="30"
HorizontalAlignment="Left" Name="btnYes" Visibility="{Binding
BtnYesVisibility, Mode=TwoWay}" Command="{Binding OnDeleteCmd}">
</Button.Background> Yes</Button>
</Grid>