2016-10-10 5 views
0

Bitte lassen Sie mich wissen, wie Wert zuweisen mit folgenden Code ComboBox-, wie der Textanzeige zuweisenzuordnen combobox Wert in wpf

`<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>` 
+0

Mögliche Duplikat [Binding WPF ComboBox eine benutzerdefinierte Liste] (http://stackoverflow.com/questions/561166/binding-wpf-combobox-to-a-custom-list) – JumpingJezza

Antwort

0

Sie können einfach DisplayMemberPath -Eigenschaft verwenden, um den Namen der Spalte Ihres DataTable, die angezeigt wird, festlegen.

-Code hinter:

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> 
+0

Danke, es funktioniert, – Malshan

0

Sie haben eine Liste von Strings Name in Ihrem Ansichtsmodell genannt zu erstellen, implementiert INotifyPropertyChanged

+0

die ** Name ** kommt von der DataTable ist ein Spaltenname *** ComboBox.ItemsSource = dt.AsDataView(); *** – Malshan

+0

Ich habe nicht die MVVM verwendet – Malshan

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()); 
     } 
    } 
} 
0
<ComboBox x:Name="cmbSubLocation" 
DisplayMemberPath="SubLocationName" SelectedValuePath="Id" 
VerticalAlignment="Top" Width="108" Height="26" FontSize="13" >     
</ComboBox> 
+0

*** SubLocationName *** und * ** Id *** ist in der DataTable verfügbar – Malshan