1、样式文件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UI_Vision">
<Style TargetType="ItemsControl" x:Key="ColorItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Label Content="{Binding ItemName}"
Width ="100"
Height="50"
FontSize="35"
Background="Green"></Label>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="ListBox" x:Key="ColorlistBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ColorlistBoxItem" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Label Name="itemName" Content="{Binding ItemName }" Margin="2" Foreground="White" Width="100" Height="40" Background="Green" FontSize="16" FontWeight="Bold"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
2、将样式文件添加到词典
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UI_Vision">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/ImageBtn.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/DesignerItem.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/Connection.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/Shared.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/TabControl.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/MenuItem.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/SliderControl.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/ParameterTextBox.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/TextBox.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/ComBox.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UI_Vision;component/Style/Resource/ColorLabelItems.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
3、添加成员类和数据源
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UI_Vision.UserControls
{
public class NgItem
{
public string ItemName { set; get; }
public SolidColorBrush BkColor { set; get; }
public NgItem(string inItemName,SolidColorBrush inBrush)
{
ItemName = inItemName;
BkColor = inBrush;
}
}
/// <summary>
/// CamMonitorWnd.xaml 的交互逻辑
/// </summary>
public partial class CamMonitorWnd : UserControl,INotifyPropertyChanged
{
public CamMonitorWnd()
{
InitializeComponent();
this.DataContext = this;
}
public int dispHaObj(HObject inObj)
{
HOperatorSet.DispObj(inObj, HaWnd.HalconWindow);
//HaWnd.HalconWindow.DispObj(inObj);
return 0;
}
public void SetCamID(String StrCam)
{
Camid.Content = StrCam;
}
private ObservableCollection<NgItem> dataList;
public ObservableCollection<NgItem> DataList
{
get { return dataList; }
set
{
dataList = value;
RaisePropertyChanged("DataList");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
4、xaml文件中添加样式引用
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<HA:HSmartWindowControlWPF x:Name=“HaWnd” Grid.Row=“0”>
</HA:HSmartWindowControlWPF>