【问题标题】:How to make a More button on the end of a listbox and handle it?如何在列表框末尾制作更多按钮并处理它?
【发布时间】:2011-10-09 17:29:55
【问题描述】:

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

它有效,但问题是......

如何在代码隐藏中获取此按钮?我需要给这个按钮添加属性并处理点击,如何获取这个按钮?

【问题讨论】:

    标签: c# xaml listbox


    【解决方案1】:

    一种常见的方法是创建自定义控件:

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

    请注意字符串 PART_MoreButton 在TemplatePartAttributeOnApplyTemplate 的覆盖中的使用。此属性告诉控件的使用者,您希望他们提供的任何 ControlTemplate 必须具有类型为 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 为控件的每个实例扩展模板时,将调用覆盖。此时您将可以访问您的按钮并可以注册事件处理程序等。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      相关资源
      最近更新 更多