WPF RegisterAttached ListBoxItem(附加属性传递到Item)

 /// <summary>
    /// Controls的附加属性
    /// </summary>
    public class ControlsAttached : DependencyObject
    {
        public static CornerRadius GetCornerRadius(DependencyObject obj)
        {
            return (CornerRadius)obj.GetValue(CornerRadiusProperty);
        }

        public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
        {
            obj.SetValue(CornerRadiusProperty, value);
        }

        // Using a DependencyProperty as the backing store for CornerRadius.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(ControlsAttached), new PropertyMetadata(new CornerRadius(0)));

        public static Thickness GetBorderThickness(DependencyObject obj)
        {
            return (Thickness)obj.GetValue(BorderThicknessProperty);
        }

        public static void SetBorderThickness(DependencyObject obj, Thickness value)
        {
            obj.SetValue(BorderThicknessProperty, value);
        }

        // Using a DependencyProperty as the backing store for BorderThickness.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BorderThicknessProperty =
            DependencyProperty.RegisterAttached("BorderThickness", typeof(Thickness), typeof(ControlsAttached), new PropertyMetadata(new Thickness(0)));

        public static int GetControlTag(DependencyObject obj)
        {
            return (int)obj.GetValue(ControlTagProperty);
        }

        public static void SetControlTag(DependencyObject obj, int value)
        {
            obj.SetValue(ControlTagProperty, value);
        }

        // Using a DependencyProperty as the backing store for ControlTag.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ControlTagProperty =
            DependencyProperty.RegisterAttached("ControlTag", typeof(int), typeof(ControlsAttached), new PropertyMetadata(0));

        public static Visibility GetIsShowControl(DependencyObject obj)
        {
            return (Visibility)obj.GetValue(IsShowControlProperty);
        }
        public static void SetIsShowControl(DependencyObject obj, Visibility value)
        {
            obj.SetValue(IsShowControlProperty, value);
        }

        /// <summary>
        /// 是否显示控件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="value"></param>
        // Using a DependencyProperty as the backing store for IsShowControl.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsShowControlProperty =
            DependencyProperty.RegisterAttached("IsShowControl", typeof(Visibility), typeof(ControlsAttached), new PropertyMetadata(Visibility.Visible));


        public static double GetAttachHeight(DependencyObject obj)
        {
            return (double)obj.GetValue(AttachHeightProperty);
        }

        public static void SetAttachHeight(DependencyObject obj, double value)
        {
            obj.SetValue(AttachHeightProperty, value);
        }

        // Using a DependencyProperty as the backing store for AttachHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AttachHeightProperty =
            DependencyProperty.RegisterAttached("AttachHeight", typeof(double), typeof(ControlsAttached), new PropertyMetadata(double.NaN));


        public static Brush GetMouseOverBackground(DependencyObject obj)
        {
            return (Brush)obj.GetValue(MouseOverBackgroundProperty);
        }

        public static void SetMouseOverBackground(DependencyObject obj, Brush value)
        {
            obj.SetValue(MouseOverBackgroundProperty, value);
        }

        // Using a DependencyProperty as the backing store for MouseOverBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MouseOverBackgroundProperty =
            DependencyProperty.RegisterAttached("MouseOverBackground", typeof(Brush), typeof(ControlsAttached), new PropertyMetadata(Brushes.Transparent));


        public static Brush GetMouseOverBorderBrush(DependencyObject obj)
        {
            return (Brush)obj.GetValue(MouseOverBorderBrushProperty);
        }

        public static void SetMouseOverBorderBrush(DependencyObject obj, Brush value)
        {
            obj.SetValue(MouseOverBorderBrushProperty, value);
        }

        // Using a DependencyProperty as the backing store for MouseOverBorderBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MouseOverBorderBrushProperty =
            DependencyProperty.RegisterAttached("MouseOverBorderBrush", typeof(Brush), typeof(ControlsAttached), new PropertyMetadata(Brushes.Transparent));


        public static Brush GetPressBackground(DependencyObject obj)
        {
            return (Brush)obj.GetValue(PressBackgroundProperty);
        }

        public static void SetPressBackground(DependencyObject obj, Brush value)
        {
            obj.SetValue(PressBackgroundProperty, value);
        }

        // Using a DependencyProperty as the backing store for PressBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PressBackgroundProperty =
            DependencyProperty.RegisterAttached("PressBackground", typeof(Brush), typeof(ControlsAttached), new PropertyMetadata(Brushes.Transparent));




        public static Brush GetPressBorderBrush(DependencyObject obj)
        {
            return (Brush)obj.GetValue(PressBorderBrushProperty);
        }

        public static void SetPressBorderBrush(DependencyObject obj, Brush value)
        {
            obj.SetValue(PressBorderBrushProperty, value);
        }

        // Using a DependencyProperty as the backing store for PressBorderBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PressBorderBrushProperty =
            DependencyProperty.RegisterAttached("PressBorderBrush", typeof(Brush), typeof(ControlsAttached), new PropertyMetadata(Brushes.Transparent));




        public static int GetControlEnable(DependencyObject obj)
        {
            return (int)obj.GetValue(ControlEnableProperty);
        }

        public static void SetControlEnable(DependencyObject obj, int value)
        {
            obj.SetValue(ControlEnableProperty, value);
        }

        // Using a DependencyProperty as the backing store for ControlEnable.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ControlEnableProperty =
            DependencyProperty.RegisterAttached("ControlEnable", typeof(int), typeof(ControlsAttached), new PropertyMetadata(0));



        public static VerticalAlignment GetVerticalAlignment(DependencyObject obj)
        {
            return (VerticalAlignment)obj.GetValue(VerticalAlignmentProperty);
        }

        public static void SetVerticalAlignment(DependencyObject obj, VerticalAlignment value)
        {
            obj.SetValue(VerticalAlignmentProperty, value);
        }

        // Using a DependencyProperty as the backing store for VerticalAlignment.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VerticalAlignmentProperty =
            DependencyProperty.RegisterAttached("VerticalAlignment", typeof(VerticalAlignment), typeof(ControlsAttached), new PropertyMetadata(VerticalAlignment.Stretch));

        public static HorizontalAlignment GetHorizontalAlignment(DependencyObject obj)
        {
            return (HorizontalAlignment)obj.GetValue(HorizontalAlignmentProperty);
        }

        public static void SetHorizontalAlignment(DependencyObject obj, HorizontalAlignment value)
        {
            obj.SetValue(HorizontalAlignmentProperty, value);
        }

        // Using a DependencyProperty as the backing store for HorizontalAlignment.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HorizontalAlignmentProperty =
            DependencyProperty.RegisterAttached("HorizontalAlignment", typeof(HorizontalAlignment), typeof(ControlsAttached), new PropertyMetadata(HorizontalAlignment.Stretch));




    }
    <Style x:Key="ListBoxItemStyle"
               TargetType="{x:Type ListBoxItem}">
            <Setter Property="HorizontalContentAlignment"
                    Value="Stretch"></Setter>
            <Setter Property="Foreground"
                    Value="White"></Setter>
            <Setter Property="SnapsToDevicePixels"
                    Value="True"></Setter>
            <Setter Property="FocusVisualStyle"
                    Value="{x:Null}"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Grid>
                            <Border Name="Border"
                                    BorderBrush="Transparent"
                                    CornerRadius="0"
                                    Margin="1 1"
                                    Height="30">
                                <Border.Background>
                                    <SolidColorBrush Color="Transparent"
                                                     x:Name="BorderBg"></SolidColorBrush>
                                </Border.Background>
                                <ContentPresenter  VerticalAlignment="{Binding Path=(local:ControlsAttached.VerticalAlignment),RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}}"
                                                   Margin="3 0" />
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected"
                                     Value="True">
                                <Setter Property="Background"
                                        TargetName="Border"
                                        Value="#9ab6bd" />
                                <Setter Property="Foreground"
                                        Value="Black"></Setter>
                            </Trigger>
                            <Trigger Property="IsMouseOver"
                                     Value="True">
                                <Setter TargetName="Border"
                                        Property="BorderBrush"
                                        Value="#acacac"></Setter>
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetName="BorderBg"
                                                            Storyboard.TargetProperty="Color"
                                                            To="#4f7f8d"
                                                            Duration="0:0:0.3"></ColorAnimation>

                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                                <Trigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetName="BorderBg"
                                                            Storyboard.TargetProperty="Color"
                                                            Duration="0:0:0.3">
                                            </ColorAnimation>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.ExitActions>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
 <ListBox ItemsSource="{Binding datas}"
                         SelectedItem="{Binding data}"
                         DisplayMemberPath="Name"
                         Foreground="White"
                         Style="{DynamicResource MyListBoxStyle}"
                         local:ControlsAttached.VerticalAlignment="Bottom"
                         ItemContainerStyle="{DynamicResource ListBoxItemStyle}">
                </ListBox>
(local:ControlsAttached.VerticalAlignment)
一定要加()否则绑定不生效!!!
实现这个是为了再ListBox设置子项对齐的方式,不会因为子项对齐方式不一样而新建Style
posted @ 2019-01-18 18:22  ligl007  阅读(551)  评论(0编辑  收藏  举报