欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF 自定义控件 实践

程序员文章站 2022-03-04 10:50:44
...

主要是在思考自定义控件 数据绑定问题 

创建自定义控件后 会生成样式 通过依赖属性来和台前界面属性 动态绑定

主要看怎么绑定值 希望给你们一个借鉴

WPF 自定义控件 实践

  <Style TargetType="{x:Type local:DataTableItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:DataTableItem}">
                    <Grid x:Name="PART_Grid" Background="{Binding BgBackground,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="23*"/>
                            <ColumnDefinition Width="175*"/>
                            <ColumnDefinition Width="17*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock  x:Name="PART_lalText" Text="{Binding Text,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}" Width="168"  TextTrimming="CharacterEllipsis" Grid.Column="1" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Center" Height="20" >
                            <TextBlock.ToolTip>
                                <ToolTip x:Name="PART_Tooltext"   Content="{Binding Text,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}" >

                                </ToolTip>
                            </TextBlock.ToolTip>
                        </TextBlock>
                        <Rectangle x:Name="PART_rt" Fill="{Binding RtFile,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding Vis,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}"   HorizontalAlignment="Left" Margin="0" Width="2"/>
                        <Path x:Name="PART_Path" Stroke="#FF1AB394" Visibility="{Binding Vis,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}" Data="M0.5,5.8330002 L5.5,10.5 M13.5,0.50000018 L5,10.5" HorizontalAlignment="Left" Height="11" Margin="7.336,10,0,0" Stretch="Fill"  VerticalAlignment="Top" Width="14"/>
                        <Path x:Name="PART_Pathadd" Visibility="{Binding Vis,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}"  Data="M389.14278,485.376 C374.80678,485.376 360.47078,484.352 346.64678,481.792 228.88678,461.312 142.87078,360.96 142.87078,242.688 142.87078,109.056 253.46278,3.4790039E-06 389.14278,3.4790039E-06 524.82278,3.4790039E-06 635.41478,109.056 635.41478,242.688 635.92678,376.32 525.33478,485.376 389.14278,485.376 z M389.14278,61.440003 C287.25478,61.440003 204.31078,142.848 204.31078,242.688 204.31078,330.752 268.82278,406.016 357.39878,420.864 368.15078,422.4 378.90278,423.424 389.65478,423.424 491.54278,423.424 574.48678,342.016 574.48678,242.176 574.48678,142.848 491.54278,61.440003 389.14278,61.440003 z M30.742784,890.368 C14.358784,890.368 0.53478391,877.056 0.02278391,860.672 0.02278391,856.576 -2.5372161,754.176 46.614784,648.704 92.694784,549.888 193.55878,430.08 413.20678,422.912 430.10278,422.4 444.43878,435.712 444.95078,452.608 445.46278,469.504 432.15078,483.84 415.25478,484.352 264.21478,489.472 159.25478,553.472 102.42278,674.816 59.414784,766.976 61.462784,858.112 61.462784,859.136 61.974784,876.032 48.662784,890.368&#xA;31.766784,890.88 31.254784,890.368 31.254784,890.368 30.742784,890.368 z M695.31878,888.832 C616.47078,888.832 542.74278,849.92 498.19878,784.384 488.47078,770.56 492.56678,751.104 506.39078,741.888 520.21478,732.16 539.67078,736.256 548.88678,750.08 581.65478,798.72 636.43878,827.392 694.80678,827.392 792.08678,827.392 870.93478,748.032 870.93478,651.264 870.93478,554.496 791.57478,475.136 694.80678,475.136 630.80678,475.136 571.92678,509.952 540.69478,565.76 532.50278,580.608 513.55878,585.728&#xA;498.71078,577.536 483.86278,569.344 478.74278,550.4 486.93478,535.552 528.91878,460.288 608.79078,413.184 694.80678,413.184 825.87878,413.184 932.37478,519.68 932.37478,650.752 932.37478,781.824 826.39078,888.832 695.31878,888.832 z M708.63078,764.928 C691.73478,764.928 677.91078,751.104 677.91078,734.208 L677.91078,568.832 C677.91078,551.936 691.73478,538.112 708.63078,538.112 725.52678,538.112 739.35078,551.936 739.35078,568.832 L739.35078,734.208 C739.35078,751.104 725.52678,764.928 708.63078,764.928 z M791.06278,681.984 L625.68678,681.984 C608.79078,681.984 594.96678,668.16 594.96678,651.264 594.96678,634.368 608.79078,620.544 625.68678,620.544 L791.06278,620.544 C807.95878,620.544 821.78278,634.368 821.78278,651.264 821.78278,668.16 808.47078,681.984 791.06278,681.984 z" Margin="0,6.5,0.5,6.5" Stretch="Fill" Grid.Column="2" VerticalAlignment="Center" Height="16" Fill="#FF1AB394" Width="17"/>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    public class DataTableItem : Control
    {
        static DataTableItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DataTableItem), new FrameworkPropertyMetadata(typeof(DataTableItem)));
        }
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }



        public Brush RtFile
        {
            get { return (Brush)GetValue(RtFileProperty); }
            set { SetValue(RtFileProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RtFile.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RtFileProperty =
            DependencyProperty.Register("RtFile", typeof(Brush), typeof(DataTableItem), new FrameworkPropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00FFFFFF"))));



        public Visibility Vis
        {
            get { return (Visibility)GetValue(VisProperty); }
            set { SetValue(VisProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Vis.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VisProperty =
            DependencyProperty.Register("Vis", typeof(Visibility), typeof(DataTableItem), new FrameworkPropertyMetadata(Visibility.Collapsed));




        public Brush BgBackground
        {
            get { return (Brush)GetValue(BgBackgroundProperty); }
            set { SetValue(BgBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BgBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BgBackgroundProperty =
            DependencyProperty.Register("BgBackground", typeof(Brush), typeof(DataTableItem), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00FFFFFF"))));



        //private Visibility vis = Visibility.Collapsed;
        //public Visibility Vis { get => vis; set => vis = value; }

        //private Brush rtFile;
        //public Brush RtFile { get => rtFile; set => rtFile = value; }

        //private Brush bgBackground;
        //public Brush BgBackground { get => bgBackground; set => bgBackground = value; }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(DataTableItem), new FrameworkPropertyMetadata("",
                     FrameworkPropertyMetadataOptions.AffectsMeasure,
                     new PropertyChangedCallback(OnValueChanged))
            { BindsTwoWayByDefault = true });

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
    
        }
        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((DataTableItem)d).IsStringNull();
        }
        public void IsStringNull()
        {
            if (Text?.Trim() != "")
            {
                Vis = Visibility.Visible;
                RtFile = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF1AB394"));
                BgBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFDBFFFC"));
                //OnApplyTemplate();
            }
        }
    }

 

相关标签: WPF