如何在项目控件中显示弹出窗口

本文关键字:显示 窗口 控件 项目 | 更新日期: 2023-09-27 18:32:20

我想在单击LabelLabelShift_MouseDown)时显示Popup。我基本上想在单击标签时编辑移位(一个标签一个班次),但我希望当我单击标签以显示弹出窗口(使用编辑按钮编辑弹出窗口)时。所以有人可以告诉我怎么做,因为这段代码不起作用。这是我的代码:

<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;
}

如何在项目控件中显示弹出窗口

您无需在 DataTemplate 中定义Popup。将其添加到窗口或用户控件Resources,例如

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

在鼠标按下处理程序中,只需执行以下操作:

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

尝试p.IsOpen = true;而不是p.StaysOpen = true;