0

I have a DataGrid, one of the columns is a DataGridComboBoxColumn:

 <DataGrid AutoGenerateColumns="False" CanUserAddRows="True">
     <DataGrid.Columns>
           <DataGridTextColumn Header="User Name" Width="Auto" Binding="{Binding UName}"/>
           <DataGridComboBoxColumn Header="Country" Width="Auto" ????/>
     </DataGrid.Columns>
 </DataGrid>

I have a Country class:

public class Country{
  public string name {get; set;}
  public string des {get; set;}
  public Country() {}
  public Country(string n, string d) {
      this.name = n;
      this.des = d;
  }
}    

they have a name and a description, I want the names(of multiple Country objects) to populate the combo-box(and the des the tooltip of each combo-box option), how do I do that?

Note: later I would like to give the user the ability to add a new Country, so I would like the combo-box to change as well(to include the new Country), meaning the solution needs to allow that.

Note2: I am not using the MVVM pattern. edit: meaning it is not a duplicate.

Stacker
  • 29
  • 1
  • 8
  • how does this solve my problem? I am not using the MVVM pattern, so this: ConnectionViewModel vm = new ConnectionViewModel(); DataContext = vm; doesn't really help me. – Stacker Oct 21 '15 at 10:31
  • Add a name to your column .In code behind,Create a list of countries & assign to its ItemsSource. – Nikita Shrivastava Oct 21 '15 at 10:48
  • this shows up as the object, I want just the names, also: I have a class named "User" which is the itemSource for the rest of the columns in the datagrid(in the example above - the user name), each user has a Name and Country, will doing what you suggested make it so that when a user changes the Country in the combobox, the value of the Country attribute in the User instance will change as well? – Stacker Oct 21 '15 at 10:58
  • Set the selectedValuePath,DisplayValuePath & SelectedValueBinding for the column – Nikita Shrivastava Oct 21 '15 at 10:59

1 Answers1

1

Solution: 1. XAML:

<DataGrid x:Name="DataGrid" AutoGenerateColumns="False" CanUserAddRows="True">
        <DataGrid.Columns>
            <DataGridTextColumn     Header="User UName" Width="Auto" Binding="{Binding UName}"/>
            <DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Country" Width="Auto" DisplayMemberPath="name" SelectedItemBinding="{Binding CountryData}"/>
        </DataGrid.Columns>
    </DataGrid>

2. Code behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        InitData();
        ComboBoxColumn.ItemsSource = CountriesCollection;
        DataGrid.ItemsSource = UsersCollection;
    }

    private void InitData()
    {
        UsersCollection = new ObservableCollection<UserData>(new List<UserData>
        {
            new UserData
            {
                UName = "Greg",
            },
            new UserData
            {
                UName = "Joe",
            },
             new UserData
            {
                UName = "Iv",
            }
        });
        CountriesCollection = new ObservableCollection<Country>(new List<Country>
        {
            new Country("Ger", "1500"),
            new Country("Fra", "1500"),
            new Country("Ru", "1500"),
            new Country("Bel", "1500"),
        });
    }

    public ObservableCollection<Country> CountriesCollection { get; set; }

    public ObservableCollection<UserData> UsersCollection { get; set; }
}

3. User model:

 public class UserData
{
    public string UName { get; set; }

    public object CountryData { get; set; }
}

4. tool tip support: replace a desired combo box column with next xaml code:

 <DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Country" DisplayMemberPath="CountryName"
                                    ItemsSource="{StaticResource CountriesArray}" Width="Auto"
                                    SelectedItemBinding="{Binding CountryData, UpdateSourceTrigger=PropertyChanged}">
                <DataGridComboBoxColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="ToolTip">
                            <Setter.Value>
                                <ContentControl Content="{Binding }">
                                    <ContentControl.ContentTemplate>
                                        <DataTemplate DataType="{x:Type soDataGridProjectsHelpAttempt:UserData}">
                                            <DataTemplate.Resources>
                                                <system:String x:Key="NoAnyEntriesKey">
                                                    No any entry presented
                                                </system:String>
                                            </DataTemplate.Resources>
                                            <TextBlock Text="{Binding CountryData.Description, FallbackValue={StaticResource NoAnyEntriesKey}}"></TextBlock>
                                        </DataTemplate>
                                    </ContentControl.ContentTemplate>
                                </ContentControl>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGridComboBoxColumn.CellStyle>
            </DataGridComboBoxColumn>

and take in account you nedd to extend Country model with description property.

regards,

Ilan
  • 2,762
  • 1
  • 13
  • 24
  • Thank you. In the Country Object I have a name field(which now is in view in the datagridcombobox) and a short description, how can I bind the description of each country in the datagridcombobox to a tooltip? (the desired effect is: a user hovers over an option in the combobox and sees the description field of that Country object as a tooltip) – Stacker Oct 22 '15 at 13:14
  • @Stacker check this out. – Ilan Oct 22 '15 at 14:11
  • I'm using WPF, I get "UserData is not supported in WPF" error on "DataType="{x:Type soDataGridProjectsHelpAttempt:UserData}"" – Stacker Oct 22 '15 at 14:25
  • But, I managed to do it(with your help): I just didn't use the DataTemplate, I set the value to the correct binding(CountryData.Description), thank you. – Stacker Oct 22 '15 at 14:30