예제

 

 

결과(버그 존재)

문제: Selected item이 object타입이라서 해당 객체 이름이 나왔다. 

미션: 국가 또는 도시 만 나오게 해야한다. -> selectedItem은 모든객체를 받을수있도록 Object타입이다. 그래서 맞는 객체로 캐스팅 해줘야한다.

예제)

Combo box항목 -> text box

 

소스

<Window
        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:WpfApplication1"
        xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="WpfApplication1.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="465.085" Width="746.864">
    <Grid Margin="0,0,12,1">
        <ListBox x:Name="bxGdp" HorizontalAlignment="Left" Height="176" VerticalAlignment="Top" Width="104" Margin="157,58,0,0" SelectionChanged="bxGDP_SelectionChanged">
            <ListBoxItem>FRANCE</ListBoxItem>
            <ListBoxItem>USA</ListBoxItem>
            <ListBoxItem>KOREA</ListBoxItem>
            <ListBoxItem>JAPAN</ListBoxItem>
            <ListBoxItem>CHINA</ListBoxItem>
            <ListBoxItem>ITALY</ListBoxItem>
            <ListBoxItem>RUSSIA</ListBoxItem>
            <ListBoxItem>SINGAPORE</ListBoxItem>
            <ListBoxItem>TAIWAN</ListBoxItem>
            <ListBoxItem>GANA</ListBoxItem>
            <ListBoxItem>GERMAN</ListBoxItem>
        </ListBox>
        <Label x:Name="label" Content="GPD순위" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="168,27,0,0" Width="81"/>
        <Label x:Name="label_Copy" Content="살기좋은 도시" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="351,27,0,0" Width="98"/>
        <Label x:Name="label_Copy1" Content="행복한 나라" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="546,27,0,0" Width="98" RenderTransformOrigin="2.373,0.342"/>
        <Label x:Name="label_Copy2" Content="SelectedIndex: " HorizontalAlignment="Left" VerticalAlignment="Top" Margin="9,276,0,0" Width="116" RenderTransformOrigin="-1.284,7.415" FontSize="15"/>
        <Label x:Name="label_Copy3" Content="SelectedItem:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="9,349,0,0" Width="114" RenderTransformOrigin="-1.284,7.415" FontSize="15"/>
        <TextBox x:Name="txtSIndex1" HorizontalAlignment="Left" Height="44" TextWrapping="Wrap" VerticalAlignment="Top" Width="131" Margin="142,276,0,0"/>
        <TextBox x:Name="txtSIndex2" HorizontalAlignment="Left" Height="44" TextWrapping="Wrap" VerticalAlignment="Top" Width="131" Margin="331,276,0,0"/>
        <TextBox x:Name="txtSIndex3" HorizontalAlignment="Left" Height="44" TextWrapping="Wrap" VerticalAlignment="Top" Width="131" Margin="540,276,0,0"/>
        <TextBox x:Name="txtSItem1" HorizontalAlignment="Left" Height="44" TextWrapping="Wrap" VerticalAlignment="Top" Width="131" Margin="142,349,0,0" TextOptions.TextFormattingMode="Display"/>
        <TextBox x:Name="txtSItem2" HorizontalAlignment="Left" Height="44" TextWrapping="Wrap" VerticalAlignment="Top" Width="131" Margin="331,349,0,0"/>
        <TextBox x:Name="txtSItem3" HorizontalAlignment="Left" Height="44" TextWrapping="Wrap" VerticalAlignment="Top" Width="131" Margin="540,349,0,0"/>
        <ListBox x:Name="bxCity" HorizontalAlignment="Left" Height="176" VerticalAlignment="Top" Width="166" Margin="312,58,0,0" SelectionChanged="bxCity_SelectionChanged">
            <ListBoxItem Content="PARI, FRANCE"/>
            <ListBoxItem Content="NEW YORK, USA"/>
            <ListBoxItem Content="SEOUL, KOREA"/>
            <ListBoxItem Content="TOKYO, JAPAN"/>
            <ListBoxItem Content="BEIJING, CHINA"/>
            <ListBoxItem Content="NAPOLY, ITALY"/>
            <ListBoxItem Content="MOSKBA, RUSSIA"/>
            <ListBoxItem Content="NULL, SINGAPORE"/>
            <ListBoxItem Content="TAIWAN, TAIWAN"/>
            <ListBoxItem Content="GANA, GANA"/>
            <ListBoxItem Content="GERMAN, GERMAN"/>
        </ListBox>
        <ListBox x:Name="bxNation" HorizontalAlignment="Left" Height="176" VerticalAlignment="Top" Width="104" Margin="540,58,0,0" SelectionChanged="bxNation_SelectionChanged">
            <ListBoxItem Content="ITALY"/>
            <ListBoxItem Content="FRANCE"/>
            <ListBoxItem Content="RUSSIA"/>
            <ListBoxItem Content="SINGAPORE"/>
            <ListBoxItem Content="USA"/>
            <ListBoxItem Content="KOREA"/>
            <ListBoxItem Content="JAPAN"/>
            <ListBoxItem Content="CHINA"/>
            <ListBoxItem Content="GERMAN"/>
            <ListBoxItem Content="TAIWAN"/>
            <ListBoxItem Content="GANA"/>
   
        </ListBox>

    </Grid>
</Window>

 

cs 코드

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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            txtSIndex1.Text = "";
            txtSIndex2.Text = "";
            txtSIndex3.Text = "";
            txtSItem1.Text = "";
            txtSItem2.Text = "";
            txtSItem3.Text = "";
        }

        private void bxGDP_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lst = sender as ListBox;
            txtSIndex1.Text = lst.SelectedIndex.ToString();

            txtSItem1.Text= lst.SelectedItem.ToString();
        }
        private void bxCity_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lst = sender as ListBox;
            txtSIndex2.Text = lst.SelectedIndex.ToString();

            txtSItem2.Text = lst.SelectedItem.ToString();
        }
        private void bxNation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lst = sender as ListBox;

            txtSIndex3.Text = lst.SelectedIndex.ToString();
            txtSItem3.Text = lst.SelectedItems.ToString();


        }

   
    }
}

 


문제 해결


 

 

소스

<Window x:Class="WpfApplication3.MainWindow"
        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:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="366.61" Width="553.474">
    <Grid>
        <Grid Height="274" Width="412" Margin="106,33,13,25">
            <Grid.RowDefinitions>
                <RowDefinition Height="100*"/>
                <RowDefinition Height="19*"/>
                <RowDefinition Height="36"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <ListBox x:Name="gdp_list" HorizontalAlignment="Left" Height="156" VerticalAlignment="Top" Width="106" Margin="10,21,0,0" SelectionChanged="gdp_list_SelectionChanged">
                <ListBoxItem Content="미국"/>
                <ListBoxItem Content="러시아"/>
                <ListBoxItem Content="중국"/>
                <ListBoxItem Content="영국"/>
                <ListBoxItem Content="독일"/>
                <ListBoxItem Content="프랑스"/>
                <ListBoxItem/>
                <ListBoxItem Content="UAE"/>
                <ListBoxItem Content="사우디"/>
                <ListBoxItem Content="일본"/>
            </ListBox>
            <ListBox x:Name="welfare_list" HorizontalAlignment="Left" Height="156" VerticalAlignment="Top" Width="106" Margin="17,21,0,0" Grid.Column="1" SelectionChanged="welfare_list_SelectionChanged">
                <ListBoxItem Content="빈"/>
                <ListBoxItem Content="서울"/>
                <ListBoxItem Content="멜버른"/>
                <ListBoxItem Content="도쿄"/>
                <ListBoxItem Content="뉴욕"/>
                <ListBoxItem Content="런던"/>
            </ListBox>
            <ListBox x:Name="happy_list" HorizontalAlignment="Left" Height="156" VerticalAlignment="Top" Width="106" Margin="19,21,0,0" Grid.Column="2" SelectionChanged="happy_list_SelectionChanged">
                <ListBoxItem Content="콩고"/>
                <ListBoxItem Content="가나"/>
                <ListBoxItem Content="인도네시아"/>
                <ListBoxItem Content="우간다"/>
                <ListBoxItem Content="브라질"/>
            </ListBox>
            <TextBox x:Name="txt_index3" TextWrapping="Wrap" Grid.Column="2" Margin="10,10,7,5" Grid.Row="1"/>
            <TextBox x:Name="txt_index1" TextWrapping="Wrap" Margin="5,8,12,7" Grid.Row="1"/>
            <TextBox x:Name="txt_index2" TextWrapping="Wrap" Margin="11,7,7,8" Grid.Row="1" Grid.Column="1"/>
            <TextBox x:Name="txt_item1" TextWrapping="Wrap" Margin="6,7,11,6" Grid.Row="2"/>
            <TextBox x:Name="txt_item2" TextWrapping="Wrap" Margin="11,7,7,6" Grid.Row="2" Grid.Column="1"/>
            <TextBox x:Name="txt_item3" TextWrapping="Wrap" Margin="10,8,7,5" Grid.Row="2" Grid.Column="2"/>
            

        </Grid>
        <Label x:Name="label" Content="GDP 순위" HorizontalAlignment="Left" VerticalAlignment="Top" Height="33" Width="68" Margin="133,14,0,0"/>
        <Label x:Name="label_Copy" Content="살기좋은 도시" HorizontalAlignment="Left" VerticalAlignment="Top" Height="33" Width="103" Margin="257,15,0,0"/>
        <Label x:Name="label_Copy1" Content="행복한 나라" HorizontalAlignment="Left" VerticalAlignment="Top" Height="33" Width="75" Margin="427,14,0,0"/>
        <Label x:Name="label_Copy2" Content="Selected Index" HorizontalAlignment="Left" VerticalAlignment="Top" Height="33" Width="108" Margin="6,242,0,0"/>
        <Label x:Name="label_Copy3" Content="Selected Item" HorizontalAlignment="Left" VerticalAlignment="Top" Height="33" Width="84" Margin="6,274,0,0"/>
    </Grid>
</Window>

 

 

cs코드

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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void gdp_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lst = sender as ListBox;
            //모든 객체이름을 뿌리는게 아닌 캐스트를 통한 특정 객체 이름만 뿌리기
            ListBoxItem gdp = (ListBoxItem)lst.SelectedItem;
            txt_item1.Text = gdp.Content.ToString();
            //인덱스(선택된 항목의 모든 객체의 인덱스)
            txt_index1.Text = lst.SelectedIndex.ToString();
        }

        private void welfare_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lst = sender as ListBox;
            //모든 객체이름을 뿌리는게 아닌 캐스트를 통한 특정 객체 이름만 뿌리기
            ListBoxItem walfare = (ListBoxItem)lst.SelectedItem;
            txt_item2.Text = walfare.Content.ToString();
            //인덱스(선택된 항목의 모든 객체의 인덱스)
            txt_index2.Text = lst.SelectedIndex.ToString();
        }

        private void happy_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lst = sender as ListBox;
            //모든 객체이름을 뿌리는게 아닌 캐스트를 통한 특정 객체 이름만 뿌리기
            ListBoxItem happy = (ListBoxItem)lst.SelectedItem;
            txt_item3.Text = happy.Content.ToString();
            //인덱스(선택된 항목의 모든 객체의 인덱스)
            txt_index3.Text = lst.SelectedIndex.ToString();
        }
    }
}

 

 

+ Recent posts