簡體   English   中英

如何在列表框的末端制作一個“更多”按鈕並進行處理?

[英]How to make a More button on the end of a listbox and handle it?

我在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>

我的列表框添加了這種樣式

Style="{StaticResource ListBoxMore}"

它有效,但問題是...

我怎樣才能在代碼背后找到這個按鈕? 我需要向該按鈕添加屬性並處理單擊,如何獲得此按鈕?

一種常見的方法是創建一個自定義控件:

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.
        }
    }
}

注意,在TemplatePartAttributeOnApplyTemplate的重寫中都使用了字符串OnApplyTemplate 此屬性告訴控件的使用者,您希望他們提供的任何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>

當WPF為控件的每個實例擴展模板時,將調用重寫。 此時,您將可以訪問按鈕並可以注冊事件處理程序等。希望這對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM