WPF 指定itemscontrol数据样式

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值