欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wpf cassette UserControl

程序员文章站 2022-06-08 22:32:00
...
<UserControl x:Class="WpfApp02.CassetUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp02"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <local:BoolToBrushConverter x:Key="BoolToBrushConverter"/>
    </UserControl.Resources>
    <Grid  Height="610" Width="120">
        <ListBox x:Name="lstSlots">           
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DockPanel Background="Gray" Width="100" Height="20">
                        <TextBlock DockPanel.Dock="Left" Text="{Binding Slot}" Width="20" VerticalAlignment="Center" Foreground="White"/>
                        <Rectangle Fill="{Binding Present, Converter={StaticResource BoolToBrushConverter}}" Margin="3" Stroke="Black" />
                    </DockPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 WpfApp02
{
    /// <summary>
    /// CassetUC.xaml 的交互逻辑
    /// </summary>
    public partial class CassetUC : UserControl
    {
        public CassetUC()
        {
            InitializeComponent();
         
            for (int i = 0; i < 25; i++)
            {
                waferPresentInfos.Add(new WaferPresentInfo() { Slot = (i + 1).ToString(), Present = false });
            }

            lstSlots.ItemsSource = waferPresentInfos;
            
        }

        public ObservableCollection<WaferPresentInfo> waferPresentInfos = new ObservableCollection<WaferPresentInfo>();


    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfApp02
{
    public class BoolToBrushConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool b = (bool)value;

            if (b)
            {
                return new SolidColorBrush(Colors.DarkGreen);
            }
            else
            {
                return new SolidColorBrush(Colors.LightGray);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp02
{
    public class WaferPresentInfo : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _Slot = "1";
        public string Slot
        {
            get { return _Slot; }
            set
            {
                _Slot = value;
                if (PropertyChanged!=null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Slot"));
                }
            }
        }

        public bool _Present = false;

        public bool Present
        {
            get { return _Present; }
            set
            {
                _Present = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Present"));
                }
            }
        }


    }
}
<Window x:Class="WpfApp02.TestWin04"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp02"
        mc:Ignorable="d"
        Title="TestWin04" Height="800" Width="810.377">
    <Window.Resources>
       

        <Style  TargetType="ListBoxItem">
            <Setter Property="Margin" Value="0"/>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <local:CassetUC  x:Name="ucCasset" HorizontalAlignment="Left"/>
            <Button x:Name="test" Content="test" Click="test_Click"/>
        </StackPanel>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
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.Shapes;

namespace WpfApp02
{
    /// <summary>
    /// TestWin04.xaml 的交互逻辑
    /// </summary>
    public partial class TestWin04 : Window
    {
        public TestWin04()
        {
            InitializeComponent();
            
        }

        private void test_Click(object sender, RoutedEventArgs e)
        {
            ucCasset.waferPresentInfos[5].Present = true;
            ucCasset.waferPresentInfos[10].Present = true;
        }
    }
}

相关标签: WPF