如何选择自定义控件中的项目

时间:2012-12-14 10:59:06

标签: c# wpf custom-controls

我编写了一个自定义控件来显示ItemsSource中计算位置的项目。

Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Control="clr-namespace:MyControls.Control">

    <Style TargetType="{x:Type Control:MyControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Control:MyControl}">
                    <Border x:Name="DrawingArea"
                        BorderThickness="{TemplateBinding Border.BorderThickness}"
                        BorderBrush="{TemplateBinding Border.BorderBrush}"
                        Padding="{TemplateBinding Control.Padding}"
                        Background="{TemplateBinding Panel.Background}"                            
                        SnapsToDevicePixels="true">
                        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <Canvas IsItemsHost="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ClipToBounds="true" />                            
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>     
        </Setter>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    ...
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

MyControl.cs

public class MyControl : MultiSelector, INotifyPropertyChanged    
{
    private bool _layoutHandlingDone = false;
    private Border _drawingArea;
    private Border DrawingArea
    {
        get { return _drawingArea ?? (_drawingArea = (Border) Template.FindName("DrawingArea", this)); }
    }

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

    protected override Size MeasureOverride(Size aviableSize) { ... }       
    protected override Size ArrangeOverride(Size finalSize) { ... }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        if (Template != null && _layoutHandlingDone == false)
        {
            _layoutHandlingDone = true;
            ItemContainerGenerator.StatusChanged += (sender, args) =>
                                                        {
                                                            if(ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
                                                                InvalidateMeasure();
                                                        };
            DrawingArea.SizeChanged += (sender, args) => UpdateLayout();
        }
    }   
}

布置和绘制项目非常有效。现在我希望这些项目可以选择。为此我从MultiSelector派生了我的控件,但似乎这还不够。

MultiSelector提供了哪些功能以及如何在我的控件中使用它?

1 个答案:

答案 0 :(得分:1)

我强烈建议在这里分开一些事情。管理项目集合并将它们安排在容器控件中是两个截然不同的问题,ItemsControl为两者提供支持。

您应该编写专门的Panel类(并覆盖MeasureOverride和ArrangeOverride),而不是重写MeasureOverride和ArrangeOverride,并在ItemsControl的ItemsPanel模板中使用该类。

然后,您可以轻松地从ListBox派生而不是MultiSelector,并且可以免费获得所有选择功能。