728x90
반응형
WPF & C# - UserControl ListBoxItem ( 사용자 정의 컨트롤 / 리스트박스 아이템 ) |
UserControl 삭제하기 ( 사용자 정의 컨트롤 / 삭제하는 방법 / Close() / Dispose)
listItem.xaml - 사용자 정의 컨트롤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <UserControl x:Class="WpfApp13.listItem" 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:WpfApp13" mc:Ignorable="d" d:DesignHeight="28.67" d:DesignWidth="137.119"> <Grid> <StackPanel Orientation="Horizontal" Height="28.67"> <Ellipse x:Name="ell" Fill="#FFF4F4F5" Height="28.67" Width="28.67"/> <TextBox x:Name="tbx" Height="28.67" Text="TextBox" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" /> </StackPanel> </Grid> </UserControl> | cs |
listItem.xaml.cs - 사용자 정의 컨트롤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public partial class listItem : UserControl { public listItem() { InitializeComponent(); } public string str { get { return tbx.Text; } set { tbx.Text = value; } } public Color clr { get { return (ell.Fill as SolidColorBrush).Color; } set { ell.Fill = new SolidColorBrush(value); } } } | cs |
MainWindow.xaml.cs
1 2 3 4 | <StackPanel Margin="10"> <Button x:Name="btn" Content="Button" Height="30" Margin="5" Click="btn_Click"/> <ListBox x:Name="lbx" Margin="5" SelectionChanged="lbx_SelectionChanged"></ListBox> </StackPanel> | cs |
MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | private Color[] circleColors = { Colors.Red, Colors.Black, Colors.LawnGreen, Colors.Blue, Colors.Cyan, Colors.Magenta, Colors.Blue, Colors.BlueViolet, Colors.Brown, Colors.BurlyWood, }; int i = 0; private void btn_Click(object sender, RoutedEventArgs e) { lll con = new lll(); con.str = i++.ToString("000"); con.clr = circleColors[i % circleColors.Length]; lbx.Items.Add(con); } private void lbx_SelectionChanged(object sender, SelectionChangedEventArgs e) { // 조건에 맞는 객체 찾기 // http://insurang.tistory.com/359 lll lstitem = lbx.SelectedItems.OfType<lll>().FirstOrDefault(); btn.Background = new SolidColorBrush(lstitem.clr); btn.Content = lstitem.str; } | cs |
728x90
반응형