WPF自定义控件颜色属性绑定传递

1.我们知道原生控件的样式、属性都是可以进行各种样式设置,触发器绑定的,比如TextBox控件的触发器绑定

<DataGridTemplateColumn x:Name="dgFrameContentData"                                                 
                                                Width="320*" >
                            <DataGridTemplateColumn.Header>
                                <TextBlock Text ="{DynamicResource MainWindow_dgFrameContentData}" />
                            </DataGridTemplateColumn.Header>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid Background="Transparent" Margin="-2">
                                        <TextBox BorderBrush="Transparent" 
                                         Text="{Binding DataContent, Mode=OneWay, UpdateSourceTrigger=LostFocus}"                                               
                                         HorizontalContentAlignment="Left"
                                         Padding="5,0,0,0"
                                         VerticalContentAlignment="Center"
                                         VerticalAlignment="Stretch"                                         
                                         IsReadOnly="True">

                                            <TextBox.Style>
                                                <Style TargetType="{x:Type TextBox}">
                                                    <Style.Triggers>

                                                        <DataTrigger Binding="{Binding FrameColor}" Value="True">
                                                            <Setter Property="Foreground" Value="Red"/>
                                                        </DataTrigger>

                                                        <DataTrigger Binding="{Binding FrameColor}" Value="False">
                                                            <Setter Property="Foreground" Value="Green"/>
                                                        </DataTrigger>

                                                    </Style.Triggers>
                                                </Style>

                                            </TextBox.Style>

                                        </TextBox>
                                    </Grid>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
View Code

如果是自己编写的控件,也可以这样使用吗,答案是肯定,但是要做很多准备工作,我们以UserControl制作一个控件为例

2.要实现属性一定跟跟原来的属性名称保持一致,因为属性的名称跟自定义控件的原生属性重名,所有使用New关键字进行控制

 public new static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register("Foreground", typeof(Brush), typeof(DatasPickerView),
             new PropertyMetadata(Brushes.Black, OnForegroundPropertyChanged));//最后一行new PropertyMetadata 新增于201812191930 为了颜色改变时 立即通知刷新 姜彦
        

        public new Brush Foreground
        {
            set { SetValue(ForegroundProperty, value); }
            get { return (Brush)GetValue(ForegroundProperty); }
        }

        private static void OnForegroundPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            DatasPickerView VoluationCellView = obj as DatasPickerView;
            //VoluationCellView.btnDatas.Foreground = (Brush)args.NewValue;
            VoluationCellView.Foreground = (Brush)args.NewValue;
        }

自定义控件View端xaml里进行一些控件的属性关联绑定

<Button Name="btnDatas"
                Background="Transparent"
                Content="Data[0]"
                Height="20"
                Margin="0,-3,0,0"
                Foreground="{Binding ElementName=datasPickerView,Path=Foreground}"
                Style="{StaticResource CommonButtonStyle}" Click="btnDatas_Click"
                
                />
View Code

3.如何像1中TextBox那样是使用属性呢?其实简单的将就是TextBox是一个控件的类别,因为是在同一个框架中,因而省去了命名空间引用,而当我们使用的自定义控件的时候,只要将相应的“命名空间+控件名称”作为一个整体即可;也就是TextBox==“命名空间+控件名称”。

<DataGridTemplateColumn Header="Ints"
                                                Width="140"
                                                Visibility="{Binding Source={x:Reference cboxShowFrameStruct},Path=IsChecked,Converter={StaticResource BoolConverter}}">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid Background="Transparent"
                                          Margin="-2">
                                        <View:DatasPickerView Height="20"
                                                              DataCount="{Binding IntCount}"
                                                              Datas="{Binding Ints,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"                                                                
                                                              HorizontalAlignment="Center"
                                                              VerticalAlignment="Center">
                                            <View:DatasPickerView.Style>
                                                <Style TargetType="{x:Type View:DatasPickerView}">
                                                    <Style.Triggers>

                                                        <DataTrigger Binding="{Binding FrameColor}"
                                                                     Value="True">
                                                            <Setter Property="Foreground"
                                                                    Value="Red" />
                                                        </DataTrigger>

                                                        <DataTrigger Binding="{Binding FrameColor}"
                                                                     Value="False">
                                                            <Setter Property="Foreground"
                                                                    Value="Green" />
                                                        </DataTrigger>

                                                    </Style.Triggers>
                                                </Style>
                                            </View:DatasPickerView.Style>
                                            
                                        </View:DatasPickerView>
                                    </Grid>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>                            
                        </DataGridTemplateColumn>

 

posted @ 2018-12-20 15:13  <--青青子衿-->  阅读(5049)  评论(0编辑  收藏  举报
// /**/ // 在页脚Html代码 引入 // function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);