为什么我的 WPF 弹出窗口没有显示在它应该显示的位置
我正在尝试遵循本指南: http://www.jarloo.com/ excel-like-autofilter-in-wpf/ 在按下数据网格列标题中的按钮时添加一个小弹出窗口。我已在列标题中添加了一个过滤器按钮和图标,并将弹出窗口的放置目标设置为此按钮,但弹出窗口始终显示在整个窗口的左下角。
知道为什么吗?
数据网格列
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Images\bios.png" Width="16" Height="16"/>
<TextBlock Text="Model" TextWrapping="Wrap" Padding="3"/>
<Button Name="btnModelFilter" Margin="3,0,0,0" Click="btnModelFilter_Click">
<Button.Template>
<ControlTemplate>
<Image Source="Images\filter.png" Width="10" Height="10"/>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
弹出窗口
<Popup Name="popModel" Placement="Bottom" PlacementTarget="{Binding ElementName=btnModelFilter}" StaysOpen="False" Width="200">
<Border Background="White" BorderBrush="Gray" BorderThickness="1,1,1,1">
<StackPanel Margin="5,5,5,15">
<StackPanel Orientation="Horizontal" Margin="0,0,0,15">
<Button Margin="0,0,0,0" Name="btnSelectAll" Click="btnSelectAll_Click">
<Button.Template>
<ControlTemplate>
<TextBlock Text="Select All" Foreground="Blue" Cursor="Hand" />
</ControlTemplate>
</Button.Template>
</Button>
<Button Margin="10,0,0,0" Name="btnUnselectAll" Click="btnUnselectAll_Click">
<Button.Template>
<ControlTemplate>
<TextBlock Text="Select None" Foreground="Blue" Cursor="Hand" />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<ListBox x:Name="lstModels" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Item}" Checked="ApplyFilters" Unchecked="ApplyFilters" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Border>
</Popup>
I am trying to follow this guide : http://www.jarloo.com/excel-like-autofilter-in-wpf/ to add a small popup when a button in a datagrid column header is pressed. I have added a filter button and icon to the column header and set the popup's placement target as this button but the popup always displays at the bottom left of the whole window.
Any idea's why?
DataGrid Column
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Images\bios.png" Width="16" Height="16"/>
<TextBlock Text="Model" TextWrapping="Wrap" Padding="3"/>
<Button Name="btnModelFilter" Margin="3,0,0,0" Click="btnModelFilter_Click">
<Button.Template>
<ControlTemplate>
<Image Source="Images\filter.png" Width="10" Height="10"/>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
Popup
<Popup Name="popModel" Placement="Bottom" PlacementTarget="{Binding ElementName=btnModelFilter}" StaysOpen="False" Width="200">
<Border Background="White" BorderBrush="Gray" BorderThickness="1,1,1,1">
<StackPanel Margin="5,5,5,15">
<StackPanel Orientation="Horizontal" Margin="0,0,0,15">
<Button Margin="0,0,0,0" Name="btnSelectAll" Click="btnSelectAll_Click">
<Button.Template>
<ControlTemplate>
<TextBlock Text="Select All" Foreground="Blue" Cursor="Hand" />
</ControlTemplate>
</Button.Template>
</Button>
<Button Margin="10,0,0,0" Name="btnUnselectAll" Click="btnUnselectAll_Click">
<Button.Template>
<ControlTemplate>
<TextBlock Text="Select None" Foreground="Blue" Cursor="Hand" />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<ListBox x:Name="lstModels" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Item}" Checked="ApplyFilters" Unchecked="ApplyFilters" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Border>
</Popup>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于按钮的 Name 属性是在 DataTemplate 中定义的,因此名称的范围不会超出该 DataTemplate,因此找不到该按钮并且 PopUp 显示在 0,0。
此范围规则是合乎逻辑的,因为想象一下,如果多次重复使用 DataTemplate,则具有相同的按钮名称会引发编译器错误。
您可以将 PopUp 定义为带有键的样式,并将其直接包含在 Header DataTemplate 中。
since the Name attribute of your button is defined within a DataTemplate, the scope of the name does not go beyond that DataTemplate, hence the button is not found and PopUp is displayed at 0,0.
This scope rule is logical because imagine if you re-use the DataTemplate several times, then having same button name would raise a compiler error.
You might define your PopUp as a style with a key, and include it in your Header DataTemplate directly.
假设您的弹出窗口位于
btnModelFilter
的同一堆栈面板中,我认为您的 StackPanel(btnModelFilter
的父级)应该是弹出窗口的放置目标,并且 不是btnModelFilter
。Assuming that your popup is in the same stack panel of your
btnModelFilter
, I think your StackPanel (parent of thebtnModelFilter
) should be the placement target of the popup and not thebtnModelFilter
.