[英]How to make a More button on the end of a listbox and handle it?
I added this resource on App.xaml
我在App.xaml
上添加了此资源
<Style x:Key="ListBoxMore" TargetType="ListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
<StackPanel>
<ItemsPresenter/>
<Button Content="More" Name="moreButton"></Button>
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And my listbox adds this style 我的列表框添加了这种样式
Style="{StaticResource ListBoxMore}"
And it works, but the question is... 它有效,但问题是...
How can I get this button in the code-behind? 我怎样才能在代码背后找到这个按钮? I need to add properties to this button and handle the click, how do I get this button? 我需要向该按钮添加属性并处理单击,如何获得此按钮?
A common approach is to create a custom control: 一种常见的方法是创建一个自定义控件:
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication2
{
[TemplatePart(Name=PartMoreButtonName, Type=typeof(Button))]
public class MyListBox : ListBox
{
private const string PartMoreButtonName = "PART_MoreButton";
private Button _moreButton;
public override void OnApplyTemplate()
{
// unhook any handlers from _moreButton to prevent a memory leak
_moreButton = GetTemplateChild(PartMoreButtonName) as Button;
// do stuff with _moreButton, register handlers, etc.
}
}
}
Notice the use of the string PART_MoreButton in both the TemplatePartAttribute
and in the override of OnApplyTemplate
. 注意,在TemplatePartAttribute
和OnApplyTemplate
的重写中都使用了字符串OnApplyTemplate
。 This attribute tells consumers of your control that you expect any ControlTemplate
that they provide must have a control of type Button
with the name PART_MoreButton
: 此属性告诉控件的使用者,您希望他们提供的任何ControlTemplate
必须具有名称为PART_MoreButton
Button
类型的PART_MoreButton
:
<StackPanel>
<l:MyListBox>
<l:MyListBox.Style>
<Style TargetType="ListBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer">
<StackPanel>
<ItemsPresenter/>
<Button x:Name="PART_MoreButton" Content="More"></Button>
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</l:MyListBox.Style>
</l:MyListBox>
</StackPanel>
When WPF inflates the template for each instance of your control, the override is called. 当WPF为控件的每个实例扩展模板时,将调用重写。 At this point you will have access to your button and can register event handlers, etc. Hope this helps! 此时,您将可以访问按钮并可以注册事件处理程序等。希望这对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.