mcorluka
mcorluka

Reputation: 55

How to show popup in ItemsControl

I want to show Popup when I click on Label (LabelShift_MouseDown). I basically want to edit shift when click on label(one label one shift) but I want when I click label to show popup (edit popup with edit buttons). So can somebody tell me how to do it because this code doesn't work. Here is my code:

<ItemsControl ItemsSource="{Binding Path=ScheduleItem}" Tag="{Binding .}" Margin="0,10,0,0">
   <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <Canvas IsItemsHost="True" />
      </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemContainerStyle>
      <Style TargetType="{x:Type ContentPresenter}">
         <Setter Property="Canvas.Left" Value="{Binding Path=Start, Converter={StaticResource timeToPositionConverter}}" />
         <Setter Property="Canvas.Top" Value="{Binding Path=Index}" />
      </Style>
   </ItemsControl.ItemContainerStyle>
   <ItemsControl.ItemTemplate>
      <DataTemplate DataType="TimeLineEntry">
         <Label Width="{Binding Duration}" Height="20" Tag="{Binding .}" BorderThickness="1" BorderBrush="DarkGray" MouseDown="LabelShift_MouseDown">
            <Label.Background>
               <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                  <GradientStop Color="#FF3B4DFF" Offset="0.996" />
                  <GradientStop Color="#FF6674F8" Offset="0" />
                  <GradientStop Color="#FFC7CEFF" Offset="0.791" />
               </LinearGradientBrush>
            </Label.Background>
            <Popup>
               <StackPanel>
                  <TextBox Text="Text" />
                  <Button Content="Update" />
                  <Button Content="Delete" Style="{StaticResource DeleteButton}"/>
               </StackPanel>
            </Popup>
         </Label>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

private void LabelShift_MouseDown(object sender, MouseButtonEventArgs e)
{
   Popup p = (sender as Label).Content as Popup;
   p.StaysOpen = true;
}

Upvotes: 1

Views: 1368

Answers (2)

Florian Gl
Florian Gl

Reputation: 6014

Try p.IsOpen = true; instead of p.StaysOpen = true;.

Upvotes: 1

Nitin Purohit
Nitin Purohit

Reputation: 18580

You need not to define the Popup inside the DataTemplate. Add it in Resources of your window or usercontrol like

       <Popup x:Key="myPopup">
           <StackPanel>
              <TextBox Text="Text" />
              <Button Content="Update" />
              <Button Content="Delete" Style="{StaticResource DeleteButton}"/>
           </StackPanel>
        </Popup>

And in the MouseDown handler just do:

      Popup popup = Resources["myPopup"] as Popup;
      popup.PlacementTarget = sender as UIElement;;
      popup.IsOpen = true

Upvotes: 4

Related Questions