1

Topic: WPF all head broke

Problem with the button. The command "Delete" in ViewModel is caught. But the current element from ListBox is necessary. If beforehand to stick with the mouse on TextBlock, and then on the button the current element in ViewModel is accessible. If at once to push the button, a current element null. How to make, that at pushing the button, an element ListBox became leaking? <Border> <Grid> <Grid. RowDefinitions> <RowDefinition Height = "Auto"> </RowDefinition> <RowDefinition> </RowDefinition> </Grid. RowDefinitions> <Label Grid. Row = "0"> Title </Label> <ListBox Grid. Row = "1" ItemsSource = "{Binding}" SelectedItem = "Current" IsSynchronizedWithCurrentItem = "True"> <ListBox. ItemTemplate> <DataTemplate> <Grid> <TextBlock> <TextBlock. InputBindings> <MouseBinding Gesture = "LeftDoubleClick" Command = "{Binding Path=DataContext. EditCommand, RelativeSource = {RelativeSource Mode=FindAncestor, AncestorType = {x:Type ListBox}}} "> </MouseBinding> </TextBlock. InputBindings> <Run Text =" {Binding Path=Property1} "> </Run> <Run Text =" {Binding Path=Property2} "> </Run> <Run Text =" {Binding Path=Property3, Mode=OneWay} "> </Run> </TextBlock> <Button HorizontalAlignment ="Right"VerticalAlignment ="Top"Command =" {Binding Path=DataContext. DeleteCommand, RelativeSource = {RelativeSource Mode=FindAncestor, AncestorType = {x:Type ListBox}}} "> <Image Height ="10"Width ="10"Source =".\image\delete_32.png"> </Image> </Button> </Grid> </DataTemplate> </ListBox. ItemTemplate> </ListBox> </Grid> </Border>

2

Re: WPF all head broke

Hello, s_aa, you wrote: _> the Problem with the button. The command "Delete" in ViewModel is caught. But the current element from ListBox is necessary. If beforehand to stick with the mouse on TextBlock, and then on the button the current element in ViewModel is accessible. If at once to push the button, a current element null. How to make, that at pushing the button, an element ListBox became leaking? _> _> <Border> _> <Grid> _> <Grid. RowDefinitions> _> <RowDefinition Height = "Auto"> </RowDefinition> _> <RowDefinition> </RowDefinition> _> </Grid. RowDefinitions> _> <Label Grid. Row = "0"> Title </Label> _> <ListBox Grid. Row = "1" ItemsSource = "{Binding}" SelectedItem = "Current" IsSynchronizedWithCurrentItem = "True"> _> <ListBox. ItemTemplate> _> <DataTemplate> _> <Grid> _> <TextBlock> _> <TextBlock. InputBindings> _> <MouseBinding Gesture = "LeftDoubleClick" _> Command = "{Binding Path=DataContext. EditCommand, _> RelativeSource = {RelativeSource Mode=FindAncestor, AncestorType = {x:Type ListBox}}} "> _> </MouseBinding> _> </TextBlock. InputBindings> _> <Run Text =" {Binding Path=Property1} "> </Run> _> <Run Text =" {Binding Path=Property2} "> </Run> _> <Run Text =" {Binding Path=Property3, Mode=OneWay} "> </Run> _> </TextBlock> _> <Button HorizontalAlignment ="Right"VerticalAlignment ="Top"_> Command =" {Binding Path=DataContext. DeleteCommand, _> RelativeSource = {RelativeSource Mode=FindAncestor, AncestorType = {x:Type ListBox}}} "> _> <Image Height ="10"Width ="10"Source =".\image\delete_32.png"> </Image> _> </Button> _> </Grid> _> </DataTemplate> _> </ListBox. ItemTemplate> _> </ListBox> _> </Grid> _> </Border> _> Through the trigger likely it does not turn out, but it is possible to make simply through viewModel: class ListBoxItemViewModel {ICommad DeleteCommand {get; set;} private void DeleteCommandExecute () {//here the element deletes itself from the list.}} Bajding is done to collection ListBoxItemViewModel.

3

Re: WPF all head broke

Here at this example look

4

Re: WPF all head broke

In my opinion, the product so initially also should be called - "WPF all head broke", instead of is simple "WPF"! That once again confirms the full stupidity MS team in designing of global things.