1. .xaml에 ComboBox를 추가한다. 

<ComboBox x:Name="cbb_{변수명}" SelectedValuePath="Key" DisplayMemberPath="Value" />

SelectedValuePath는 선택되는 항목을 아래(2)에서 보이듯이 

dicTemp의 Key값을 보일 것인지, Value값을 보일 것인지 정한다. 

보통 저렇게 지정하는 것 같다.

 

만약 여기서 지정하기 싫다면 

.cs 파일 내에서 이렇게 지정해 줄수도 있다. 

 

   this.cbb_{변수명}.SelectedValuePath = "Key";
   this.cbb_{변수명}.DisplayMemberPath = "Value";

 

 

 

2-0. chat gpt에 물어보니 

   public class 함수명   
      {
          public string Key { get; set; }
          public clsEnum.{Values} Value { get; set; }
          public 함수명(clsEnum.{Values} Key, string value)   생성자
          {
	       Key = key;	
              Value = value;
              
          }
          public override string ToString()
          {
              return Value;   ComboBox에 표시할 텍스트
          }
      }

이런식으로 사용하라는데;;

그냥  Dictionary<string, string> dicTemp = new Dictionary<string, string>(); 이거쓰면 한 줄에 가능. 

 

 

 

 

2. .cs에

Startprocess()부분에 

Dictionary<string, string> dicTemp = new Dictionary<string, string>();

this.cbb_{comboBox명}.SelectionChanged -= Cbb_{comboBox명}_SelectionChanged;
dicTemp.Clear();
dicTemp.Add( .Tostring(), "전체");
dicTemp.Add( .Tostring(), "전체");
dicTemp.Add( .Tostring(), "전체");
this.cbb_{comboBox명}.ItemsSource = new BindingSource(dicTemp, null);
this.cbb_{comboBox명}.SelectedIndex = 0;
this.cbb_{comboBox명}.SelectionChanged += Cbb_{comboBox명}_SelectionChanged;

 

dicTemp에 값들을 넣어준다. 

bindingSource를 해야 값이 들어간다. 

 

 Dictionary<string, string> dicTemp = new Dictionary<string, string>(); 은 

 Dictionary<int, string> dicTemp = new Dictionary<int, string>(); 

이렇게 구성할 수 도 있고, 저장한 데이터 타입에 따라 변경하면 된다. 

 

[ dicTemp의 것을 활용하는 방법 ]

this.{comboBox명}.SelectedItem == null  //dic로 담긴게 있는지
this.{comboBox명}.SelectedValue.Tostring()
(int)this.{comboBox명}.SelectedValue  //dic의 value값(앞인지, 뒤인지 잘모르겠음 아직도)

 

 

 

 


 

 

+ ComboBox의 - PopUp class의 Placement 

Popup이 화면에 표시될 위치를 결정하는 데 사용된다. 

 

  • Absolute: 기준 요소와 무관하게 화면의 절대 좌표를 기준으로 위치를 지정합니다.
  • Relative: 기준 요소의 좌측 상단 모서리를 기준으로 상대적인 위치를 지정합니다.
  • Bottom: 기준 요소의 아래쪽에 Popup을 표시합니다.
  • Center: 기준 요소의 중심에 Popup을 표시합니다.
  • Right: 기준 요소의 오른쪽에 Popup을 표시합니다.
  • AbsolutePoint: 기준 요소와 무관하게 화면의 절대 좌표를 기준으로 위치를 지정합니다. HorizontalOffset와 VerticalOffset 속성을 사용하여 조정할 수 있습니다.
  • RelativePoint: 기준 요소의 좌측 상단 모서리를 기준으로 상대적인 위치를 지정합니다. HorizontalOffset와 VerticalOffset 속성을 사용하여 조정할 수 있습니다.
  • Mouse: 마우스 커서 위치를 기준으로 Popup을 표시합니다. (ComboBox 중 클릭한 위치에!)
  • MousePoint: 마우스 커서 위치를 기준으로 Popup을 표시하고, HorizontalOffset와 VerticalOffset 속성을 사용하여 조정할 수 있습니다.
  • Left: 기준 요소의 왼쪽에 Popup을 표시합니다.
  • Top: 기준 요소의 위쪽에 Popup을 표시합니다.

사용 방법

<ComboBox x:Name="cbb_{변수명}" SelectedValuePath="Key" DisplayMemberPath="Value">
    <ComboBox.Template>
         <ControlTemplate TargetType="ComboBox">
             <Grid>
                 <ToggleButton Name="ToggleButton"
                       Grid.Column="2"
                       Template="{StaticResource ComboBoxToggleButton}" 
                       Focusable="false"
                       IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                       ClickMode="Press">
                 </ToggleButton>
                 <ContentPresenter Name="ContentSite"
                           IsHitTestVisible="False"
                           Content="{TemplateBinding SelectionBoxItem}"
                           ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                           ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                           Margin="3,3,23,3"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Left">
                 </ContentPresenter>                                
                 <Popup Name="Popup"
                Placement="MousePoint"
                IsOpen="{TemplateBinding IsDropDownOpen}"
                AllowsTransparency="True"
                Focusable="False"
                PopupAnimation="Slide">
                     <Grid Name="DropDown"
                   SnapsToDevicePixels="True"
                   MinWidth="{TemplateBinding ActualWidth}"
                   MaxHeight="{TemplateBinding MaxDropDownHeight}">
                         <Border Name="DropDownBorder"
                         BorderThickness="1"
                         Background="LightBlue">
                             <ScrollViewer Margin="4,6,4,6"
                                   SnapsToDevicePixels="True">
                                 <StackPanel IsItemsHost="True"
                                     KeyboardNavigation.DirectionalNavigation="Contained" />
                             </ScrollViewer>
                         </Border>
                     </Grid>
                 </Popup>
             </Grid>
         </ControlTemplate>
     </ComboBox.Template>
</ComboBox>

 

 

<ComboBox> 안에 추가한다.   

<Popup> 라인을 보면 된다. - Placement 속성을 수정하면되는데, 생략하면 유동적으로 생성된다. 

+ Recent posts