WPF - 选择器 - 自定义控件 - SelectionChanged事件未触发

时间:2013-04-24 20:44:45

标签: wpf custom-controls selector itemtemplate selectionchanged

我是自定义控件创建的新手。我已经为基于Selector类的新自定义控件奠定了基础。我的理解是我应该使用这个类,因为我需要控件来拥有一个Items集合以及处理选择的能力。我相信更改ItemTemplate可能会覆盖部分此功能,因为我没有在控件级别或应用程序级别收到SelectionChanged事件。我想如果我是正确的,有一些SelectionRegion XAML标签,我可以将DataTemplate内置。我没有幸运找到这样的东西。在浏览Google一段时间之后,我准备好问一下。我错过了什么?下面是ItemTemplate标记。谢谢你的帮助。如果你能告诉我为什么TextBlock中的Text包含在括号中,即使数据不是这样,也要多谢。

<Setter Property="ItemTemplate">
    <Setter.Value>
        <DataTemplate>
            <Border BorderBrush="Black" BorderThickness="1">
                <TextBlock Text="{Binding}" Foreground="Black" Background="White" MinHeight="12" MinWidth="50"/>
            </Border>
        </DataTemplate>
    </Setter.Value>
</Setter>

根据评论者的要求,到目前为止,这是控件的完整XAML:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SourceMedicalWPFCustomControlLibrary">
    <Style TargetType="{x:Type local:MultiStateSelectionGrid}">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="1">
                        <TextBlock Text="{Binding Code}" Foreground="Black" Background="White" MinHeight="12" MinWidth="50" Padding="2" ToolTip="{Binding Description}"/>
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MultiStateSelectionGrid}">
                    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0" Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0" Content="{TemplateBinding Content}"/>
                        <ItemsPresenter/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

还有贫穷的代码隐藏:

namespace SourceMedicalWPFCustomControlLibrary
{
    public class MultiStateSelectionGridState
    {
        public Brush Background { get; set; }
        public Brush Foreground { get; set; }
        public Brush Border { get; set; }
        public string Text { get; set; }

        public MultiStateSelectionGridState()
        {
            Background = Brushes.White;
            Foreground = Brushes.Black;
            Border = Brushes.Black;
            Text = String.Empty;
        }
    };

    public class MultiStateSelectionGrid : Selector
    {
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(object), typeof(MultiStateSelectionGrid),
            new FrameworkPropertyMetadata(null,
                  FrameworkPropertyMetadataOptions.AffectsRender |
                  FrameworkPropertyMetadataOptions.AffectsParentMeasure));

        public object Content
        {
            get { return (object)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        public static readonly DependencyProperty StatesProperty =
            DependencyProperty.Register("States", typeof(List<MultiStateSelectionGridState>), typeof(MultiStateSelectionGrid),
            new FrameworkPropertyMetadata(new List<MultiStateSelectionGridState>(),
                FrameworkPropertyMetadataOptions.AffectsRender));

        public List<MultiStateSelectionGridState> States
        {
            get { return (List<MultiStateSelectionGridState>)GetValue(StatesProperty); }
            set { SetValue(StatesProperty, value); }
        }

        static MultiStateSelectionGrid()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiStateSelectionGrid), new FrameworkPropertyMetadata(typeof(MultiStateSelectionGrid)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.SelectionChanged += new SelectionChangedEventHandler(MultiStateSelectionGrid_SelectionChanged);
        }

        void MultiStateSelectionGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageBox.Show("Hi");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这就是我的工作。我使用自定义控件的apply template函数,并为我想要的控件选择chnaged事件添加一个句柄。

这里的简单示例:

public event EventHandler<SelectionChangedEventArgs> YourControlSelectionChanged;

private void Selector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (ListSelectionChanged != null) {
        ListSelectionChanged(sender, e);
    }
}

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
    //find or declare your control here, the x:name in xaml should be YourControl
    YourControl== this.Template.FindName("YourControl", this) as YourControlType
    YourControl.SelectionChanged += ResultListBox_SelectionChanged;
}

然后,您可以绑定到您在xaml中的自定义控件类中声明的公共事件(YourControlSelectionChanged)的名称。

希望这会有所帮助。

答案 1 :(得分:0)

通过阅读不同控件的一些完整代码示例,我相信我的答案是我做错了。相反,我需要在ControlTemplate中拥有像ListBox这样的Selector的控件。那么,@ JKing的建议将帮助我达到我需要的地方。然而,实际问题的答案是上述从使用Selector作为基类到在控件的模板中使用选择器的变化。谢谢你的帮助。