1、 创建一个用户控件,编写XAML以及.cs
2、 Xaml中可以定义样式以及转换,具体转换函数在.cs中写
<Grid.Resources>
<Style TargetType="{x:Type TextBlock}" x:Key="textBlockStyle">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Foreground" Value="SlateBlue" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Cursor" Value="Hand" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BitmapEffect" >
<Setter.Value>
<DropShadowBitmapEffect Color="Blue" Direction="-90" ShadowDepth="1" Softness="0.1" Opacity="0.9"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
<local:Bool2String x:Key="Bool2String"/>
</Grid.Resources>
class Bool2String : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var str = string.Empty;
if (value is bool)
{
var tmp = (bool)value;
str = tmp ? "Red" : "Black";
}
return str;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
3、 定义控件中元素propdp(按Tab键),根据TAB键引导修改数据类型、名称以及初始值,用于外界绑定
4、
//定义元素,此处由于绑定list,类型为List的DataType
public ObservableCollection<DataType> DataTypes
{
get { return (ObservableCollection<DataType>)GetValue(DataTypesProperty); }
set { SetValue(DataTypesProperty, value); }
}
public static readonly DependencyProperty DataTypesProperty =
DependencyProperty.Register("DataTypes", typeof(ObservableCollection<DataType>), typeof(UCComSimilarityLv), new PropertyMetadata(null));
//定义命令,此处为类型为string的命令
public RelayCommand<string> DelCmd
{
get { return (RelayCommand<string>)GetValue(DelCmdProperty); }
set { SetValue(DelCmdProperty, value); }
}
public static readonly DependencyProperty DelCmdProperty =
DependencyProperty.Register("DelCmd", typeof(RelayCommand<string>), typeof(UCItem), new PropertyMetadata(null));
5、 数据绑定,此处不必写DataContext = this;(因为数据来自外部)
6、 测试控件
7、 使用:主界面绑定即可
控件.cs
public bool IsAlarm
{
get { return (bool)GetValue(IsAlarmProperty); }
set { SetValue(IsAlarmProperty, value); }
}
public static readonly DependencyProperty IsAlarmProperty =
DependencyProperty.Register("IsAlarm", typeof(bool), typeof(UCRecordItem), new PropertyMetadata(false));
控件.xaml
<TextBlock Text="{Binding IsAlarm,Converter={StaticResource CommomCvt},
ConverterParameter=True:黑名单:通过,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=UserControl},
StringFormat='{}{0:yyyy-MM-dd HH:mm:ss}'}"
Foreground ="{Binding IsAlarm,
Converter={StaticResource CommomCvt},
ConverterParameter=True:#e75043:White}"/>
主界面
<GroupBox Header="识别记录" Foreground="White" FontSize="18"
BorderBrush="CadetBlue" BorderThickness="2" Margin="7,8"
Grid.Column="1">
<ItemsControl ItemsSource="{Binding Records}"
Style="{StaticResource ItemControlsStyle2}"
Margin="8">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:UCRecordItem OldImage="{Binding OldImage}"
SnapImage="{Binding SnapImage}"
Similarity="{Binding Similarity}"
OccurTime="{Binding OccurTime}"
IsAlarm="{Binding IsAlarm}"/>
</DataTemplate>