0

DataGridComboBoxColumn에 selectedItem으로 특정 항목을 설정하려고합니다. 그러나 많은 연구, 나는 아직 나를 위해 정답을 찾지 못한다.DataGridComboBoxColumn 선택한 항목 설정

내 시나리오 :
나는 프로그래밍 방식 ObservableCollection<>ItemsSource로되어있는 DataGrid을 만들었습니다. 마지막 열로서 DataGridComboBoxColumn을 추가하여 사용자가 선택할 항목을 제공하려고합니다. 이러한 데이터는 이미 데이터베이스에 저장 될 수 있으므로 데이터베이스에 저장된 컬렉션의 값을 "미리 설정"해야합니다.

private void ManipulateColumns(DataGrid grid) 
{ 
    ... 
    DataGridComboBoxColumn currencies = new DataGridComboBoxColumn(); 
    //Here come the possible choices from the database 
    ObservableCollection<string> allCurrencies = new ObservableCollection<string>(Data.AllCurrencys); 
    currencies.ItemsSource = allCurrencies; 
    currencies.Header = "Currency"; 
    currencies.CanUserReorder = false; 
    currencies.CanUserResize = false; 
    currencies.CanUserSort = false; 
    grid.Columns.Add(currencies); 
    currencies.MinWidth = 100; 
    //Set the selectedItem here for the column "Currency" 
    ... 
} 

내가 아니라 DataGridComboBoxColumns에 대한 일반 선택 상자의 선택 항목을 설정하는 많은 자습서를 발견했다. currencies.SetCurrentValue()으로 이미 시도했지만 에서 DependencyProperty을 찾을 수 없습니다.
누군가 나를 도울 수 있습니까?

미리 감사드립니다.
Boldi

답변

0

C# 코드로 DataGrid를 작성하는 것은 지저분합니다. 대신 데이터 바인딩을 사용하는 것이 좋습니다. C#으로 빌드를 계속하려면 행당 값을 설정해야합니다. 열의 모든 행에 대해 기본값을 설정하는 방법은 없습니다. 내 DataGrid가 Book 유형의 컬렉션에 바인딩되어 있다고 가정 해보십시오. DataGrid SelectedItem 속성을 사용하여 선택한 행에 대한 Book 개체를 가져온 다음 통화 속성을 설정할 수 있습니다. 값을 설정하고 해당 행에 대한 객체를 가져온 다음 통화 속성을 설정하는 데 필요한 행을 파악해야합니다. 이것은 완전한 대답은 아니지만 시작하게 할 것입니다. 기본적으로 열이 아닌 DataGrid의 각 항목에 대해이를 설정해야합니다.

public class Book 
{ 
    public decimal price; 
    public string title; 
    public string author; 
    public string currency; 
} 

private void ManipulateColumns(DataGrid grid) 
{ 

    DataGridComboBoxColumn currencies = new DataGridComboBoxColumn(); 
    //Here come the possible choices from the database 
    System.Collections.ObjectModel.ObservableCollection<string> allCurrencies = new System.Collections.ObjectModel.ObservableCollection<string>(); 
    allCurrencies.Add("US"); 
    allCurrencies.Add("asdf"); 
    allCurrencies.Add("zzz"); 
    currencies.ItemsSource = allCurrencies; 
    currencies.Header = "Currency"; 
    currencies.CanUserReorder = false; 
    currencies.CanUserResize = false; 
    currencies.CanUserSort = false; 
    grid.Columns.Add(currencies); 
    currencies.MinWidth = 100; 
    //Set the selectedItem here for the column "Currency" 
    //currencies. 

    ((Book)grid.SelectedItem).currency = "US Dollar"; 
} 
+0

안녕하세요, Brent, 답장을 보내 주셔서 감사합니다. 내 목표는 실제로 모든 행에 대해 콤보 상자의 값을 설정하는 것입니다. 나는 당신이 혼란 스러울지라도 무슨 뜻인지 모르겠다. 나는 이것을 구현하고 피드백을 주려고 노력할 것이다. 그러나 이것이 DataGrid에 데이터를 바인딩하는 일반적인 방법이 아닌가? ObservableCollection을 가져 와서 ItemsSource로 설정하는 것이 옳은 방법이라고 생각했습니다. 그러나 나는 C# 세계에 대해 아주 새로운 것을 인정해야합니다. – Boldi