无法将包装面板和列表框移动到主窗口

时间:2015-12-29 15:49:20

标签: wpf popup

enter image description here

我有一个popUp,在这个popUp里面有一个包装面板和一个列表框(即使没有wrappanel情况也一样)我希望它在主窗口内但我无法移动它!它返回边缘,如何解决?

   <Window x:Class="MyHeroEditor.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyHeroEditor"
    Title="MainWindow" Height="635.075" Width="796.643"
 WindowState="Maximized"
    Background="Bisque">
<Window.Resources>
    <Style TargetType="ListBox"  x:Key="listbox"  >
        <Setter Property="Height" Value="250"/>
        <Setter Property="Width" Value="250" />
    </Style>
    <Style TargetType="Button"  x:Key="Button"  >
        <Setter Property="Height" Value="40"/>
        <Setter Property="Width" Value="55" />
    </Style>
    <Style TargetType="WrapPanel"  x:Key="Panel"  >
        <Setter Property="Height" Value="250"/>
        <Setter Property="Width" Value="250" />
    </Style>
    <Style TargetType="Image"  x:Key="Image"  >
        <Setter Property="Height" Value="70"/>
        <Setter Property="Width" Value="70" />
    </Style>
</Window.Resources>
<Window.DataContext>
    <local:Collections/>
</Window.DataContext>

    <Canvas Name="Hero" Drop="HeroDrop" AllowDrop="True" Margin="0,0,-8,-4" 
       >
      <WrapPanel  
         x:Name="HelmetPanel" Style="{StaticResource Panel}" Height="Auto"  Width="Auto" />
     <Button
           Click="Export" Style="{StaticResource Button}" Canvas.Left="264"
        Canvas.Top="389" >Export
    </Button>
<Button
           Click="Open" Content="Open" Style="{StaticResource Button}" Canvas.Left="15" Canvas.Top="389" />
    <Button
           Click="Save" Style="{StaticResource Button}" Canvas.Left="179" Canvas.Top="389"  >Save
        </Button>
        <Button 
             AllowDrop="True" Style="{StaticResource Button}" Click="Helmet_Button_Click" Canvas.Left="415" Canvas.Top="26" >Шлемы
        </Button>
        <Button 
            AllowDrop="True" Style="{StaticResource Button}" Click="Armature_Button_Click" Canvas.Left="524" Canvas.Top="26" >Броня
        </Button>
        <Button 
             AllowDrop="True" Style="{StaticResource Button}" Click="Weapon_Button_Click" Canvas.Left="615" Canvas.Top="26" >Оружие
    </Button>
    <Button
        Click="Create" Content="Create" Style="{StaticResource Button}" Canvas.Left="90" Canvas.Top="389" />
    <Button 
         AllowDrop="True" Style="{StaticResource Button}" Click="Gloves_Button_Click" Canvas.Left="727" Canvas.Top="26" Content="Перчатки" Width="59" />
    <Button 
         AllowDrop="True" Style="{StaticResource Button}" Click="Boots_Button_Click" Canvas.Left="824" Canvas.Top="26" Content="Сапоги" />
   <Popup
            Name="HelmetsPopUp" StaysOpen="False" Placement="Mouse">
        <ListBox x:Name="listhelmets"  Style="{StaticResource listbox}"  ItemsSource="{Binding ListHelmets}"
        IsSynchronizedWithCurrentItem="True" PreviewMouseDown="helmet_MouseDown"  MouseDown="helmet_MouseDown" 
        DragLeave="helmet_DragLeave" MouseUp="Listhelmets_OnMouseUp"
        SelectedValuePath="protection" >
            <ListBox.ItemTemplate >
                <DataTemplate >
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Image}" Style="{StaticResource Image}"/>
                        <TextBox Height="30" Width="30" Text="{Binding protection}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Popup>
    <Popup
            Name="ArmaturePopUp" StaysOpen="False" Placement="Mouse" >
        <ListBox x:Name="listarmature" Style="{StaticResource listbox}" ItemsSource="{Binding ListArmature}"
        IsSynchronizedWithCurrentItem="True" MouseDown="armature_MouseDown" MouseUp="Listarmature_OnMouseUp"
    PreviewMouseMove="armature_PreviewMouseMove" SelectedValuePath="protection" DragLeave="armature_DragLeave"
     Canvas.Left="271" Canvas.Top="51">
            <ListBox.ItemTemplate >
                <DataTemplate >
                    <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Image}" Style="{StaticResource Image}"/>
                        <TextBox Height="30" Width="30" Text="{Binding protection}"/>
                        </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            </ListBox>
      </Popup>
    <Popup
            Name="WeaponPopUp" StaysOpen="False" Placement="Mouse"  >
        <WrapPanel
      Name="WeaponPanel"  Style="{StaticResource Panel}">
            <ListBox x:Name="listweapons" Style="{StaticResource listbox}" ItemsSource="{Binding ListWeapon}"
                IsSynchronizedWithCurrentItem="True" MouseDown="weapon_MouseDown" MouseUp="Listweapons_OnMouseUp"
                DragLeave="weapons_DragLeave" 
                    PreviewMouseMove="weapons_PreviewMouseMove" SelectedValuePath="attack">
                <ListBox.ItemTemplate >
                    <DataTemplate >
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Image}" Style="{StaticResource Image}"/>
                            <TextBox Height="30" Width="30" Text="{Binding _attack}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </WrapPanel>
    </Popup>
    <TextBox x:Name="TextBox2"  Canvas.Left="29" Canvas.Top="312" Height="55" Width="130"  />
    <Button
        Click="Reset" Style="{StaticResource Button}" Content="reset" Canvas.Left="219" Canvas.Top="327"  />
    <WrapPanel Height="261" Width="264" Canvas.Left="29" Canvas.Top="15">
        <ListBox x:Name="listHero" Height="237" Width="100" Style="{StaticResource listbox}" ItemsSource="{Binding ListHero}"
             AllowDrop="True" >
            <ListBox.ItemTemplate >
                <DataTemplate >
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Image}" Style="{StaticResource Image}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </WrapPanel>
  <Popup Name="GlovesPopUp" StaysOpen="False">
    <Canvas>
    <ListBox x:Name="listgloves" Height="237" Width="131" Style="{StaticResource listbox}" ItemsSource="{Binding ListGloves}"
    AllowDrop="True" Canvas.Left="723" Canvas.Top="-519" >
        <ListBox.ItemTemplate >
            <DataTemplate >
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Image}" Style="{StaticResource Image}"/>
                    <TextBox Height="30" Width="30" Text="{Binding protection}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Canvas>
    </Popup>
    <Popup Name="BootsPopUp" StaysOpen="False">
    <ListBox x:Name="listboots" Height="237" Width="137" Style="{StaticResource listbox}" ItemsSource="{Binding ListBoots}"
             AllowDrop="True" Canvas.Left="634" Canvas.Top="104" >
        <ListBox.ItemTemplate >
            <DataTemplate >
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Image}" Style="{StaticResource Image}"/>
                    <TextBox Height="30" Width="30" Text="{Binding _attack}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        </ListBox>
    </Popup>
</Canvas>
</Window>

2 个答案:

答案 0 :(得分:1)

尝试使用XAML编辑器而不是Visual Editor,因为Visual在某些情况下会出现故障。

您应该在这些元素之间移动控件:

<Window x:Class="WPFSO.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid>**Add your controls here**</Grid>
    </Grid>
</Window>

我的猜测是你在第二个(内部)网格之外还有类似问题。

答案 1 :(得分:1)

您可以使用Popup [或Tooltip] 控件的以下属性来指定打开时的位置:

样品:

<Canvas Margin="5" Background="Red" Width="200" Height="150" >

  <Ellipse Name="ellipse1"
           Canvas.Top="60" Canvas.Left="50"
           Height="85" Width="60" 
           Fill="Black"/>

  <Popup IsOpen="True" PlacementTarget="{Binding ElementName=ellipse1}">
    <TextBlock Background="LightBlue" FontSize="18">This is a Popup</TextBlock>
  </Popup>
</Canvas>
  • Placement(其值类型为PlacementModePlacementMode枚举值,用于确定控件打开时Popup控件的方向,并指定控件的方式与屏幕边界交互。默认为Bottom 。)

样品:

<Canvas Width="200" Height="150">
  <Image Name="image1"
         Canvas.Left="75" 
         Source="Water_lilies.jpg" Height="200" Width="200"/>
  <Popup IsOpen="True" PlacementTarget="{Binding ElementName=image1}"
         Placement="Bottom">
    <TextBlock FontSize="14" Background="LightGreen">Placement=Bottom</TextBlock>

  </Popup>
  <Popup IsOpen="True" PlacementTarget="{Binding ElementName=image1}"
         Placement="Top">
    <TextBlock FontSize="14" Background="LightGreen">Placement=Top</TextBlock>

  </Popup>
  <Popup IsOpen="True" PlacementTarget="{Binding ElementName=image1}"
         Placement="Left">
    <TextBlock FontSize="14" Background="LightGreen">Placement=Left</TextBlock>

  </Popup>
  <Popup IsOpen="True" PlacementTarget="{Binding ElementName=image1}"
         Placement="Right">
    <TextBlock FontSize="14" Background="LightGreen">Placement=Right</TextBlock>

  </Popup>
</Canvas>

enter image description here

其他样本和信息:

在: enter image description here

在: enter image description here