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>
如果是自己編寫的控件,也可以這樣使用嗎,答案是肯定,但是要做很多准備工作,我們以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" />
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>
