1

我是自定义控件创建的新手。我已经为基于 Selector 类的新自定义控件奠定了一些基础。我的理解是我应该使用这个类,因为我需要控件来拥有一个 Items 集合和处理选择的能力。我相信更改 ItemTemplate 可能会覆盖其中的某些功能,因为我没有在控件级别或应用程序级别收到 SelectionChanged 事件。我想如果我是对的,我可以将 DataTemplate 内部放入某种 SelectionRegion XAML 标记。我没有运气找到这样的东西。在谷歌浏览了一段时间后,我准备问一下。我错过了什么?下面是 ItemTemplate 标记。谢谢你的帮助。

<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");
        }
    }
}
4

2 回答 2

0

这就是我所做的。我使用自定义控件的应用模板功能并将处理程序添加到我想要的控件的选择更改事件中。

这里的简单示例:

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) 的名称。

希望这可以帮助。

于 2013-04-25T15:12:33.887 回答
0

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

于 2013-04-25T17:13:14.037 回答