0

Please let me know how to assign value to combobox with following code, as assign the Display text

`<ComboBox x:Name="ComboBox" HorizontalAlignment="Left" Margin="256,41,0,-6"
    VerticalAlignment="Top" Width="108" Height="26" FontSize="13" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>`
Malshan
  • 15
  • 7
  • Possible duplicate of [Binding WPF ComboBox to a Custom List](http://stackoverflow.com/questions/561166/binding-wpf-combobox-to-a-custom-list) – JumpingJezza Oct 12 '16 at 00:39

4 Answers4

0

You have to create a list of strings called Name in your viewModel that implements INotifyPropertyChanged

FCin
  • 3,804
  • 4
  • 20
  • 49
0

You can simply use DisplayMemberPath property to set the name of the column of your DataTable which will be displayed.

Code behind:

ComboBox.ItemsSource = dt.AsDataView();

XAML:

<ComboBox x:Name="ComboBox" DisplayMemberPath="Name" HorizontalAlignment="Left" Margin="256,41,0,-6"
    VerticalAlignment="Top" Width="108" Height="26" FontSize="13">
</ComboBox>
Alex
  • 1,433
  • 9
  • 18
0

XAML:

<ComboBox x:Name="ComboBox" ItemsSource="{Binding myListofItems}" HorizontalAlignment="Left" Margin="256,41,0,-6" VerticalAlignment="Top" Width="108" Height="26" FontSize="13" />

C#:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private List<string> _myListOfItems = new List<string> ();
    public List<string> myListOfItems
    {
        get { return (_myListOfItems); }
        set
        {
            _myListOfItems = value;
            OnPropertyChanged ("myListOfItems");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) {
            handler (this, new PropertyChangedEventArgs (propertyName));
        }
    }

    public MainWindow()
    {
        InitializeComponent ();
        this.DataContext = this;

        // Start Populating your List here
        // Example:
        for (int i = 0; i < 10; i++) {
            myListOfItems.Add (i.ToString ());
        }
    }
}
Steven Borges
  • 401
  • 1
  • 3
  • 16
0
<ComboBox x:Name="cmbSubLocation" 
DisplayMemberPath="SubLocationName" SelectedValuePath="Id"
VerticalAlignment="Top" Width="108" Height="26" FontSize="13" >                   
</ComboBox>
Malshan
  • 15
  • 7