1

When i call a Command from my MenuItem its removing a Item from my list, but it is still in my listview. I have to close and reopen the view. But if i call the same command from a Button its removing the item in the listview without "refreshing".

(The MenuItem is the Item called "deleteTermin" at the end)

    <UserControl.Resources>
        <convert:OutlookDurationToHeightConverter x:Key="DurationToHeightConverter"/>
        <convert:OutlookStartTimeToPositonConverter x:Key="DurationToPositonConverter"/>
    </UserControl.Resources>
    
    <UserControl.DataContext>
        <viewmodel:WeekPlanCalendarViewModel/> 
    </UserControl.DataContext>

    <Grid HorizontalAlignment="Stretch">

        <Grid.RowDefinitions>
            <RowDefinition Height="180"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" Grid.Column="0">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
                <Button x:Name="btnLoadMyOutlook"
                    Style="{DynamicResource PanelButtonStyle}"
                    Command="{Binding LoadMyOutlookCommand}"
                    HorizontalAlignment="Left"
                        Height="40" Width="40" VerticalAlignment="Top">
                    <ContentControl Content="{DynamicResource Outlook-Icon}"/>
                </Button>
            </StackPanel>

        </Grid>

        <Grid Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center">
            <DockPanel Background="Transparent">
                <Border BorderBrush="{DynamicResource Border_Backgroud_PrimColor}"
                        Background="{DynamicResource Border_Backgroud_SecColor}"
                        CornerRadius="2"
                        BorderThickness="3"
                        Margin="5"
                        Width="auto">
                    <DockPanel x:Name="wp_pnl" MaxHeight="760">
                        <Border Background="{DynamicResource Light_Prime_Color}"
                            CornerRadius="2">
                            <ListView x:Name="lv_wp_users"
                                      Background="Transparent"
                                      BorderThickness="0"
                                      Margin="-4,-1.5,0,-1"
                                      ScrollViewer.CanContentScroll="True"
                                      ScrollViewer.HorizontalScrollBarVisibility="Visible"
                                      ScrollViewer.VerticalScrollBarVisibility="Hidden"
                                      ItemContainerStyle="{StaticResource LvItemWPStyle}" 
                                      ItemsSource="{Binding GroupedList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                <ListView.Resources>
                                    <Style TargetType="{x:Type ScrollBar}">
                                        <Setter Property="Background" Value="{DynamicResource Backgroud_PrimColor}"/>
                                        <Setter Property="BorderBrush" Value="{DynamicResource Backgroud_PrimColor}"/>
                                        <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type ScrollBar}">
                                                    <Grid x:Name="Bg" SnapsToDevicePixels="true" Margin="2,0,-2,-1">
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="0.0001*"/>
                                                        </Grid.ColumnDefinitions>
                                                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Row="0" CornerRadius="2"/>
                                                        <Track x:Name="PART_Track" IsDirectionReversed="False" IsEnabled="{TemplateBinding IsMouseOver}" Grid.Row="0" Grid.Column="0">
                                                            <Track.DecreaseRepeatButton>
                                                                <RepeatButton Command="{x:Static ScrollBar.PageLeftCommand}" Style="{StaticResource RepeatButtonTransparent}" HorizontalAlignment="Right" Width="17"/>
                                                            </Track.DecreaseRepeatButton>
                                                            <Track.IncreaseRepeatButton>
                                                                <RepeatButton Command="{x:Static ScrollBar.PageLeftCommand}" Style="{StaticResource RepeatButtonTransparent}"/>
                                                            </Track.IncreaseRepeatButton>
                                                            <Track.Thumb>
                                                                <Thumb Style="{StaticResource ScrollBarThumbVertical}" Margin="1"/>
                                                            </Track.Thumb>
                                                        </Track>
                                                    </Grid>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </ListView.Resources>
                                <ListView.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal"/>
                                    </ItemsPanelTemplate>
                                </ListView.ItemsPanel>
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <Border Background="Transparent"
                                            BorderBrush="{DynamicResource Border_Backgroud_PrimColor}"
                                            BorderThickness="3,0,3,0">
                                            <StackPanel >
                                                <Grid Width="200">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*"/>
                                                        <ColumnDefinition Width="*"/>
                                                    </Grid.ColumnDefinitions>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="*"/>
                                                        <RowDefinition Height="720"/>
                                                    </Grid.RowDefinitions>

                                                    <Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
                                                        <TextBlock x:Name="MitarbeiterNameTB" Padding="10,0,0,0" Foreground="White" FontWeight="Bold" FontSize="15" Text="{Binding MitarbeiterName}"/>
                                                    </Grid>

                                                    <Grid Grid.Row="1" Grid.Column="0">
                                                        <ItemsControl x:Name="Calender"
                                                                      VerticalAlignment="Stretch"
                                                                      Grid.IsSharedSizeScope="True"
                                                                      ItemsSource="{Binding}">

                                                            <ItemsControl.ItemsPanel>
                                                                <ItemsPanelTemplate>
                                                                    <WrapPanel />
                                                                </ItemsPanelTemplate>
                                                            </ItemsControl.ItemsPanel>

                                                            <ItemsControl.ItemTemplate>
                                                                <DataTemplate>
                                                                    <Canvas Height="720" >
                                                                        <Border x:Name="TBBorder" 
                                                                                Width="100"
                                                                                MaxHeight="720"
                                                                                BorderBrush="{DynamicResource Border_Backgroud_PrimColor}"
                                                                                BorderThickness="1"
                                                                                Tag="{Binding IdTermin, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                                                Canvas.Top="{Binding TerminStartTime, Converter={StaticResource DurationToPositonConverter}}">
                                                                            <Border.Style>
                                                                                <Style TargetType="Border">
                                                                                    <Setter Property="Background" Value="Blue"/>
                                                                                    <Setter Property="Height" Value="{Binding TerminDuration, Converter={StaticResource DurationToHeightConverter}}"/>

                                                                                    <Style.Triggers>
                                                                                        <DataTrigger Binding="{Binding TerminStatus}" Value="olOutOfOffice">
                                                                                            <Setter Property="Background" Value="Purple"/>
                                                                                        </DataTrigger>
                                                                                    </Style.Triggers>
                                                                                </Style>
                                                                            </Border.Style>

                                                                            <TextBlock Text ="{Binding TerminBezeichnung, UpdateSourceTrigger=PropertyChanged}" 
                                                                                   TextWrapping="Wrap" Grid.Column="6" FontSize="10" FontWeight="Bold" Foreground="White"/>
                                                                            <Border.ContextMenu>
                                                                                <ContextMenu Style="{DynamicResource ContextMenu}"
                                                                                             x:Name="ConMenu" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={ x:Type Border}}}"
                                                                                             DataContext="{Binding PlacementTarget.DataContext,RelativeSource={RelativeSource Self}}">

                                                                                    <MenuItem Header="Termin Info's" Style="{DynamicResource ContextMenuItem}" FontSize="15" FontWeight="Bold">
                                                                                        <StackPanel Width="200">
                                                                                            <TextBlock TextWrapping="Wrap" Text="{Binding TerminBezeichnung, UpdateSourceTrigger=PropertyChanged}" FontSize="12" Tag="TEst1"/>
                                                                                            <StackPanel Orientation="Horizontal" Margin="0,5,0,0">
                                                                                                <TextBlock TextWrapping="Wrap" Text="{Binding TerminStartTime, StringFormat=HH:mm, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
                                                                                                <TextBlock TextWrapping="Wrap" Text=" - " FontSize="12"/>
                                                                                                <TextBlock TextWrapping="Wrap" Text="{Binding TerminEndeTime, StringFormat=HH:mm, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
                                                                                            </StackPanel>
                                                                                            <TextBlock TextWrapping="Wrap" Text="{Binding TerminLocation}" Margin="0,5,0,0" FontSize="12"/>
                                                                                        </StackPanel>
                                                                                    </MenuItem>
                                                                                    <MenuItem Header="Termin entfernen" 
                                                                                              x:Name="deleteTermin"
                                                                                              Style="{DynamicResource ContextMenuItem}" 
                                                                                              FontSize="15" 
                                                                                              FontWeight="Bold" 
                                                                                              Command="{Binding DeleteOneOutlookItemCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                                                              CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}">
                                                                                        <MenuItem.DataContext>
                                                                                            <viewmodel:WeekPlanCalendarViewModel/>
                                                                                        </MenuItem.DataContext>
                                                                                    </MenuItem>
                                                                                </ContextMenu>
                                                                            </Border.ContextMenu>
                                                                        </Border>
                                                                    </Canvas>
                                                                </DataTemplate>
                                                            </ItemsControl.ItemTemplate>
                                                        </ItemsControl>
                                                    </Grid>
                                                </Grid>
                                            </StackPanel>
                                        </Border>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </Border>
                    </DockPanel>
                </Border>
            </DockPanel>
        </Grid>
    </Grid>
</UserControl>

And there is the code:

    public void DeleteOneOutlookItem(object obj)
    {
        if (obj == null)
            return;

        Border brd = obj as Border;
        //brd.Visibility = Visibility.Collapsed;

        int IdToDeleteTermin = Convert.ToInt32(brd.Tag);

        string CurrSelectedDate = SelectedDate.Date.ToString("yyyy-MM-dd");

        if (CurrSelectedDate == "0001-01-01")
        {
            CurrSelectedDate = DateTime.Now.ToString("yyyy-MM-dd");
        }


        dbReader.DeleteOneOutlookCalendarItems(IdToDeleteTermin);

        LoadAllLists(CurrSelectedDate);
    }


public class GroupingListWP
    {
        public int? IdTermin { get; set; }
        public string TerminBezeichnung { get; set; }
        public DateTime? TerminStartDate { get; set; }
        public DateTime? TerminStartTime { get; set; }
        public DateTime? TerminEndeDate { get; set; }
        public DateTime? TerminEndeTime { get; set; }
        public double? TerminDuration { get; set; }
        public string TerminStatus { get; set; }
        public string MitarbeiterName { get; set; }
        public int? MitarbeiterPosID { get; set; }
        public string TerminLocation { get; set; }
        public int? IdAufgabe { get; set; }
        public DateTime? Datum { get; set; }
        public string Status { get; set; }
        public string AuftragsBezeichnung { get; set; }
        public string PSP { get; set; }
        public string Ersteller { get; set; }
        public string AuftragVariante { get; set; }
    }

public void LoadAllLists(string date)
{
    GroupedWPList = new List<GroupingListWP>();

    OutlookTerminListeAllUsers = new List<DBCalendarInfos>();
    dbReader.GetAllOutlookItemsFromDB(OutlookTerminListeAllUsers, date);
    OnPropertyChanged(nameof(OutlookTerminListeAllUsers));

    foreach (DBCalendarInfos item in OutlookTerminListeAllUsers)
    {
        GroupedWPList.Add(new GroupingListWP
        {
            IdTermin = item.IdTermin,
            TerminBezeichnung = item.TerminBezeichnung,
            TerminStartDate = item.TerminStartDate,
            TerminStartTime = item.TerminStartTime,
            TerminEndeDate = item.TerminEndeDate,
            TerminEndeTime = item.TerminEndeTime,
            TerminDuration = item.TerminDuration,
            TerminStatus = item.TerminStatus,
            MitarbeiterName = item.MitarbeiterName,
            MitarbeiterPosID = item.MitarbeiterPosID,
            TerminLocation = item.TerminLocation,
            IdAufgabe = null,
            Datum = null,
            Status = null,
            AuftragsBezeichnung = null,
            PSP = null,
            Ersteller = null,
            AuftragVariante = null
        });
    }

    OrdersNotVDM = new List<OrdersNotVDM>();
    dbReader.GetAllOrdersNOTvdm(OrdersNotVDM, date);

    foreach (OrdersNotVDM item in OrdersNotVDM)
    {
            GroupedWPList.Add(new GroupingListWP
            {
                IdTermin = null,
                TerminBezeichnung = null,
                TerminStartDate = null,
                TerminStartTime = null,
                TerminEndeDate = null,
                TerminEndeTime = null,
                TerminDuration = null,
                TerminStatus = null,
                MitarbeiterName = item.MitarbeiterName,
                MitarbeiterPosID = item.MitarbeiterPosID,
                TerminLocation = null,
                IdAufgabe = item.IdAufgabe,
                Datum = item.Datum,
                Status = item.Status,
                AuftragsBezeichnung = item.AuftragsBezeichnung,
                PSP = item.PSP,
                Ersteller = item.Ersteller,
                AuftragVariante = item.AuftragVariante
                
            });
    }

    MitarbeiterList = new List<TeamUsersInfos>();
    dbReader.GetAllTeamUsers(MitarbeiterList);

    foreach (TeamUsersInfos item in MitarbeiterList)
    {
        if (!GroupedWPList.Any(x => x.MitarbeiterName.Contains(item.MitarbeiterName)))
        {
            GroupedWPList.Add(new GroupingListWP
            {
                IdTermin = null,
                TerminBezeichnung = null,
                TerminStartDate = null,
                TerminStartTime = null,
                TerminEndeDate = null,
                TerminEndeTime = null,
                TerminDuration = null,
                TerminStatus = null,
                MitarbeiterName = item.MitarbeiterName,
                MitarbeiterPosID = item.MitarberterPositionID,
                TerminLocation = null,
                IdAufgabe = null,
                Datum = null,
                Status = null,
                AuftragsBezeichnung = null,
                PSP = null,
                Ersteller = null,
                AuftragVariante = null
            });
        }
    }

    GroupedList = GroupedWPList.OrderBy(p => p.MitarbeiterPosID).ThenBy(n => n.MitarbeiterName).GroupBy(u => u.MitarbeiterName).Select(grp => grp.ToList()).ToList();

    OnPropertyChanged(nameof(GroupedList));
}

Have someone a idea what i make wrong?

Best regards Karponi

Karponi
  • 11
  • 2
  • Maybe try to change `GroupedList` type to `ObservableCollection` ? – Jimbot Oct 18 '22 at 06:43
  • Hello Jimbot. I try but sadly same problem. I think i have maybe a problem with my binding. But i cant find the problem. Or the problem is with the MenuItem – Karponi Oct 18 '22 at 08:05
  • 1
    Problem is fixed by this option [https://stackoverflow.com/questions/3583507/wpf-binding-a-contextmenu-to-an-mvvm-command](https://stackoverflow.com/questions/3583507/wpf-binding-a-contextmenu-to-an-mvvm-command) – Karponi Oct 18 '22 at 15:49

0 Answers0