WPF 扩展器按钮的样式使其位于扩展器标头内
我正在使用 Expander
控件,并设置了标题样式,如下图所示:
http://www.hughgrice.com/Expander.jpg
我遇到的问题是我希望扩展器按钮包含在标题中,以便标题模板末尾的行与 < 对齐code>Expander 内容,即我最终希望得到类似于下图的内容:
http ://www.hughgrice.com/Expander.gif
提前致谢。
I am using the Expander
control and have styled the header as shown in the picture below:
http://www.hughgrice.com/Expander.jpg
The problem I have is that I want the expander button to be contained within the header so that the line for the end of the header template aligns with the Expander
content i.e. I ultimatly want to end up with something similar to the image below:
http://www.hughgrice.com/Expander.gif
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现您想要将扩展器按钮实际移动到 HeaderTemplate 中,而不仅仅是重新设计它的样式。使用 FindAncestor 可以轻松完成此操作:
首先添加一个 ToggleButton 并使用 FindAncestor 绑定其 IsChecked 属性,如下所示:
这会在标题模板内添加一个展开按钮,但不会隐藏 Expander 提供的原始按钮。为此,我建议您更换 Expander 的 ControlTemplate。
下面是 Expander 的 ControlTemplate 的完整副本,其中 ToggleButton 替换为简单的 ContentPresenter:
可以按如下方式使用:
更简单的替代方案是在 headerTemplate 中设置负边距以覆盖 Expander 按钮。您的 DataTemplat 将只包含如下内容,而不是上面显示的 ControlTemplate:
调整负边距以获得您想要的外观。这个解决方案更简单,但较差,因为如果您切换到不同的系统主题,所需的边距可能会改变,并且您的扩展器可能不再看起来很好。
I see that you want to actually move the expander button into your HeaderTemplate, not just restyle it. This is easily done with FindAncestor:
First add a ToggleButton and bind its IsChecked property using FindAncestor, along these lines:
This adds an expand button inside the header template but does not hide the original button provided by the Expander. To do this I recommend you replace the Expander's ControlTemplate.
Here is a complete copy of Expander's ControlTemplate with the ToggleButton replaced with a simple ContentPresenter:
It might be used as follows:
A simpler alternative would be to just set a negative margin in yourHeaderTemplate to cover the expander button. Instead of the ControlTemplate shown above, your DataTemplat would just contain something like this:
Adjust the negative margin to get the look you want. This solution is simpler but inferior in that if you switch to a different system theme the required margin may change and your expander may no longer look good.
您将需要编辑 Expander 的模板,而不是 HeaderTemplate。 HeaderTemplate 不包含展开按钮,只包含其中的内容。
默认的控件模板看起来像这样:
我删除了大部分属性,但保留了重要的内容。基本上,您需要在 ToggleButton 周围添加自定义设置。这就是包含展开按钮和标题内容的内容。
如果您有 Expression Blend,它会使此过程变得更加容易,因为您只需编辑原始模板的副本即可。 Visual Studio 还没有真正具备这种能力。
You will need to edit the Expander's Template, not the HeaderTemplate. The HeaderTemplate doesn't contain the expand button, just the content inside of it.
The default control template looks something like this:
I took out most of the attributes but left in the important stuff. Basically, you will want to add your customizations around the ToggleButton. That is what contains the expand button and the header content.
If you have Expression Blend, it makes this process much easier because you can simply edit a copy of the original template. Visual Studio doesn't really have this ability yet.