在 WPF 中使用 DataTrigger 时的绑定问题
我的 WPF 项目中的绑定存在问题。相关的 XAML 如下:
<dg:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Name="txtnoitems" Text="No Records Found" Visibility="Collapsed"></TextBlock>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}" Value="False">
<Setter TargetName="txtnoitems" Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
我想做的是当 DataGrid.HasItems
为 false
时显示 TextBlock
。由于某种原因这不起作用。
谁能看到我的问题可能是什么?
I'm having an issue with the bindings in my WPF project. The relevant XAML is below:
<dg:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Name="txtnoitems" Text="No Records Found" Visibility="Collapsed"></TextBlock>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}" Value="False">
<Setter TargetName="txtnoitems" Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
What I am trying to do is have the TextBlock
appear when the DataGrid.HasItems
is false
. This doesn't work for some reason.
Can anyone see what my issue might be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
根据评论中的假设进行编辑:
如果正如 @bathineni 提到的,当集合中不存在任何项目时,您希望在
DataGrid
上显示一个框,您可能需要使用除了RowDetailsTemplate
以外的机制来实现此目的。我能想到的两个示例是设置 DataGrid 的样式,使其包含突出显示的 TextBlock 来显示此消息,另一个示例是创建一个包含 DataGrid 和要在顶部显示的消息的网格。
示例一:
设置 DataGrid 样式
您可以找到 这是非常好的教程,它将向您展示如何设计 DataGrid 内部的样式,类似于 Samuel 在这里的样式,总共有四个。
在 DataGrid 模板中,将所有内容包围在 Grid 对象中并创建要在其中显示的消息。
例如:(请原谅较长的代码片段)
在上面的示例中,您可以看到
noItemsBorder
对象,它只是覆盖了ScrollContentPresenter x:Name="当
对象,另一种方法是在 Visibility 属性上使用 Binding,但为了方便起见为清楚起见,我根据您的问题使用了触发器。DataGrid.HasItems
为 false(通过使用触发器显示)时,PART_ScrollContentPresenter"示例二:
一次性 TextBlock 覆盖
这与上面的示例类似,但您无需修改 DataGrid 样式,而只需将两个控件封装在 Grid 内。
虽然这可行,但需要将此代码复制并粘贴到您想要使用它的任何地方,这并不理想。
代码如下:
原始帖子:
我不确定您的 HasItems 使用什么,但请确保它是引发适当的 PropertyChanged 事件的属性。
此代码按我的预期工作:
DataGrid XAML
(注意:我还删除了触发器绑定中的 {RelativeSource},并将
RowDetailsVisibilityMode
属性设置为DataGrid)某些项目类 (用作子项目)
用法:
Edit as per assumption in comments:
If as @bathineni mentioned you wanted to display a box over the
DataGrid
when no items were present in the collection, you would probably need to use a mechanism other than theRowDetailsTemplate
to acheive this.Two example I can think of would be to style the DataGrid so that it contained a highlighted TextBlock to display this message, and the other would be to create a grid which contained the DataGrid and the Message you wanted to display on top.
Example One:
Style the DataGrid
You can find a very nice tutorial here that will show you how to style the DataGrid internals similar to how Samuel has here, there are four of these in total.
In the DataGrid Template, surround everything in a Grid object and create the message to be displayed within.
For Example: (please excuse the long code snippet)
In the above example, you can see the
noItemsBorder
object, this just overlays theScrollContentPresenter x:Name="PART_ScrollContentPresenter"
object whenDataGrid.HasItems
is false (displayed by using a trigger), another way would be to use a Binding on the Visibility property, but for the sake of clarity I have used a Trigger as per your question.Example Two:
One-use TextBlock overlay
This will be similar to the example above, but rather than modifying the DataGrid style, you would just encapsulate the two controls inside of a Grid.
Although this will work, this code will need to be copy and pasted everywhere you want to use it which is not ideal.
Code as below:
Original Post:
I'm not sure what you're using for your HasItems, but make sure it is a property which raises the appropriate PropertyChanged event.
This code works as expected for me:
DataGrid XAML
(note: that I have also removed the {RelativeSource} in the trigger binding, and set the
RowDetailsVisibilityMode
property to the DataGrid)Some Item Class (used as the child items)
Usage: